dorkgen/googlesearch/googlesearch_test.go

179 lines
4.2 KiB
Go
Raw Normal View History

package googlesearch_test
2020-02-24 19:21:12 +00:00
import (
2020-06-15 08:50:03 +00:00
"fmt"
"github.com/sundowndev/dorkgen/googlesearch"
2020-09-05 14:47:12 +00:00
"net/url"
2020-02-24 19:21:12 +00:00
"testing"
2020-06-15 08:50:03 +00:00
assertion "github.com/stretchr/testify/assert"
2020-02-24 19:21:12 +00:00
)
var dork *googlesearch.GoogleSearch
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
func TestInit(t *testing.T) {
2020-06-15 08:50:03 +00:00
assert := assertion.New(t)
2020-02-24 19:43:58 +00:00
2020-02-27 18:47:17 +00:00
t.Run("should convert to URL correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:43:58 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Site("example.com").
2020-09-05 14:48:10 +00:00
URL()
2020-02-24 19:43:58 +00:00
2020-02-27 18:47:17 +00:00
assert.Equal(result, "https://www.google.com/search?q=site%3Aexample.com", "they should be equal")
})
2020-02-24 19:21:12 +00:00
2020-06-15 08:50:03 +00:00
t.Run("should convert to string correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-06-15 08:50:03 +00:00
result := fmt.Sprint(dork.Site("example.com"))
assert.Equal(result, "site:example.com", "they should be equal")
})
2020-02-27 18:47:17 +00:00
t.Run("should handle site tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Site("example.com").
2020-06-15 08:50:03 +00:00
String()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
assert.Equal(result, "site:example.com", "they should be equal")
})
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
t.Run("should handle intext tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Intext("text").
2020-06-15 08:50:03 +00:00
String()
2020-02-24 19:21:12 +00:00
2020-03-16 17:45:28 +00:00
assert.Equal(result, "intext:\"text\"", "they should be equal")
2020-02-27 18:47:17 +00:00
})
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
t.Run("should handle inurl tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Inurl("index.php").
2020-06-15 08:50:03 +00:00
String()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
assert.Equal(result, "inurl:\"index.php\"", "they should be equal")
})
2020-02-24 19:21:12 +00:00
2020-03-16 17:45:28 +00:00
t.Run("should handle filetype tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Filetype("pdf").
2020-06-15 08:50:03 +00:00
String()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
assert.Equal(result, "filetype:\"pdf\"", "they should be equal")
})
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
t.Run("should handle cache tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Cache("www.google.com").
2020-06-15 08:50:03 +00:00
String()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
assert.Equal(result, "cache:\"www.google.com\"", "they should be equal")
})
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
t.Run("should handle related tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Related("www.google.com").
2020-06-15 08:50:03 +00:00
String()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
assert.Equal(result, "related:\"www.google.com\"", "they should be equal")
})
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
t.Run("should handle ext tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Ext("(doc | pdf | xls | txt | xml)").
2020-06-15 08:50:03 +00:00
String()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
assert.Equal(result, "ext:(doc | pdf | xls | txt | xml)", "they should be equal")
})
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
t.Run("should handle exclude tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Exclude(googlesearch.New().Plain("html")).
Exclude(googlesearch.New().Plain("htm")).
Exclude(googlesearch.New().Plain("php")).
Exclude(googlesearch.New().Plain("md5sums")).
2020-06-15 08:50:03 +00:00
String()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
assert.Equal(result, "-html -htm -php -md5sums", "they should be equal")
})
2020-02-24 19:21:12 +00:00
2020-09-05 14:53:58 +00:00
t.Run("should handle 'OR' tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 19:21:12 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Site("facebook.com").
Or().
Site("twitter.com").
2020-06-15 08:50:03 +00:00
String()
2020-02-27 18:47:17 +00:00
assert.Equal(result, "site:facebook.com | site:twitter.com", "they should be equal")
2020-02-27 18:47:17 +00:00
})
2020-09-05 14:53:58 +00:00
t.Run("should handle 'AND' tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-09-05 14:53:58 +00:00
result := dork.
Intitle("facebook").
And().
Intitle("twitter").
String()
assert.Equal(result, "intitle:\"facebook\" + intitle:\"twitter\"", "they should be equal")
})
2020-02-27 18:47:17 +00:00
t.Run("should handle group tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-27 18:47:17 +00:00
result := dork.
Site("linkedin.com").
2020-09-05 15:30:14 +00:00
Group(googlesearch.New().Intext("1").Or().Intext("2")).
2020-06-15 08:50:03 +00:00
String()
2020-02-27 18:47:17 +00:00
assert.Equal(result, "site:linkedin.com (intext:\"1\" | intext:\"2\")", "they should be equal")
2020-02-27 18:47:17 +00:00
})
2020-02-24 21:00:37 +00:00
2020-03-16 17:45:28 +00:00
t.Run("should handle group tag correctly", func(t *testing.T) {
dork = googlesearch.New()
2020-02-24 21:00:37 +00:00
2020-02-27 18:47:17 +00:00
result := dork.
Site("linkedin.com").
2020-09-05 15:30:14 +00:00
Group(googlesearch.New().Intext("1").Or().Intext("2")).
2020-02-27 18:47:17 +00:00
Intitle("jordan").
2020-06-15 08:50:03 +00:00
String()
2020-02-24 21:00:37 +00:00
assert.Equal(result, "site:linkedin.com (intext:\"1\" | intext:\"2\") intitle:\"jordan\"", "they should be equal")
2020-02-27 18:47:17 +00:00
})
2020-09-05 14:47:12 +00:00
t.Run("should return URL values", func(t *testing.T) {
dork = googlesearch.New()
2020-09-05 14:47:12 +00:00
result := dork.
Site("linkedin.com").
2020-09-05 15:30:14 +00:00
Group(googlesearch.New().Intext("1").Or().Intext("2")).
2020-09-05 14:47:12 +00:00
Intitle("jordan").
QueryValues()
assert.Equal(url.Values{
"q": []string{"site:linkedin.com (intext:\"1\" | intext:\"2\") intitle:\"jordan\""},
2020-09-05 14:47:12 +00:00
}, result, "they should be equal")
})
2020-02-24 21:00:37 +00:00
}