feat: implement intitle method

pull/3/head
sundowndev 2020-02-27 19:47:17 +01:00
parent b8acb0aa51
commit ecf6a7262a
4 changed files with 157 additions and 115 deletions

19
doc.go Normal file
View File

@ -0,0 +1,19 @@
/*
Package dorkgen is a Go package to generate dork requests for popular search engines such as Google, DuckDuckGo and Bing.
It allows you to define requests programmatically and convert them into string.
You can use it as following:
package main
import "github.com/sundowndev/dorkgen"
func main() {
dork := &dorkgen.GoogleSearch{}
// dork := &dorkgen.DuckDuckGo{}
// dork := &dorkgen.Bing{}
dork.Site("example.com").Intext("text").ToString()
// returns: site:example.com "text"
}
*/
package dorkgen

View File

@ -1,8 +1,8 @@
package dorkgen
// EngineFactory is the main interface for
// engineFactory is the main interface for
// search engine implementations.
type EngineFactory interface {
type engineFactory interface {
Site(string) *GoogleSearch
ToString() string
ToURL() string

View File

@ -14,11 +14,12 @@ const (
relatedTag = "related:"
extTag = "ext:"
excludeTag = "-"
intitleTag = "intitle:"
)
// GoogleSearch is the Google implementation for Dorkgen
type GoogleSearch struct {
EngineFactory
engineFactory
tags []string
}
@ -109,3 +110,9 @@ func (e *GoogleSearch) Group(value string) *GoogleSearch {
e.tags = append(e.tags, "("+value+")")
return e
}
// Intitle ...
func (e *GoogleSearch) Intitle(value string) *GoogleSearch {
e.tags = append(e.tags, concat(intitleTag, value, true))
return e
}

View File

@ -8,87 +8,90 @@ import (
var dork *GoogleSearch
func TestToUrl(t *testing.T) {
func TestInit(t *testing.T) {
assert := assert.New(t)
t.Run("should convert to URL correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Site("example.com").
ToURL()
assert.Equal(t, result, "https://www.google.com/search?q=site%3Aexample.com", "they should be equal")
}
assert.Equal(result, "https://www.google.com/search?q=site%3Aexample.com", "they should be equal")
})
func TestSite(t *testing.T) {
t.Run("should handle site tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Site("example.com").
ToString()
assert.Equal(t, result, "site:example.com", "they should be equal")
}
assert.Equal(result, "site:example.com", "they should be equal")
})
func TestIntext(t *testing.T) {
t.Run("should handle intext tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Intext("text").
ToString()
assert.Equal(t, result, "\"text\"", "they should be equal")
}
assert.Equal(result, "\"text\"", "they should be equal")
})
func TestInurl(t *testing.T) {
t.Run("should handle inurl tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Inurl("index.php").
ToString()
assert.Equal(t, result, "inurl:\"index.php\"", "they should be equal")
}
assert.Equal(result, "inurl:\"index.php\"", "they should be equal")
})
func TestFiletype(t *testing.T) {
t.Run("should handle rrrrr tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Filetype("pdf").
ToString()
assert.Equal(t, result, "filetype:\"pdf\"", "they should be equal")
}
assert.Equal(result, "filetype:\"pdf\"", "they should be equal")
})
func TestCache(t *testing.T) {
t.Run("should handle cache tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Cache("www.google.com").
ToString()
assert.Equal(t, result, "cache:\"www.google.com\"", "they should be equal")
}
assert.Equal(result, "cache:\"www.google.com\"", "they should be equal")
})
func TestRelated(t *testing.T) {
t.Run("should handle related tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Related("www.google.com").
ToString()
assert.Equal(t, result, "related:\"www.google.com\"", "they should be equal")
}
assert.Equal(result, "related:\"www.google.com\"", "they should be equal")
})
func TestExt(t *testing.T) {
t.Run("should handle ext tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Ext("(doc | pdf | xls | txt | xml)").
ToString()
assert.Equal(t, result, "ext:(doc | pdf | xls | txt | xml)", "they should be equal")
}
assert.Equal(result, "ext:(doc | pdf | xls | txt | xml)", "they should be equal")
})
func TestExclude(t *testing.T) {
t.Run("should handle exclude tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
@ -98,10 +101,10 @@ func TestExclude(t *testing.T) {
Exclude("md5sums").
ToString()
assert.Equal(t, result, "-html -htm -php -md5sums", "they should be equal")
}
assert.Equal(result, "-html -htm -php -md5sums", "they should be equal")
})
func TestOr(t *testing.T) {
t.Run("should handle or tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
@ -110,10 +113,10 @@ func TestOr(t *testing.T) {
Site("twitter.com").
ToString()
assert.Equal(t, result, "site:facebook.com OR site:twitter.com", "they should be equal")
}
assert.Equal(result, "site:facebook.com OR site:twitter.com", "they should be equal")
})
func TestGroup(t *testing.T) {
t.Run("should handle group tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
@ -121,5 +124,18 @@ func TestGroup(t *testing.T) {
Group((&GoogleSearch{}).Intext("1").Or().Intext("2").ToString()).
ToString()
assert.Equal(t, result, "site:linkedin.com (\"1\" OR \"2\")", "they should be equal")
assert.Equal(result, "site:linkedin.com (\"1\" OR \"2\")", "they should be equal")
})
t.Run("should handle rrrrr tag correctly", func(t *testing.T) {
dork = &GoogleSearch{}
result := dork.
Site("linkedin.com").
Group((&GoogleSearch{}).Intext("1").Or().Intext("2").ToString()).
Intitle("jordan").
ToString()
assert.Equal(result, "site:linkedin.com (\"1\" OR \"2\") intitle:\"jordan\"", "they should be equal")
})
}