|
1
|
|
|
package scan_test |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"net/http" |
|
5
|
|
|
"testing" |
|
6
|
|
|
"time" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/stefanoj3/dirstalk/pkg/common/test" |
|
9
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan" |
|
10
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan/producer" |
|
11
|
|
|
"github.com/stretchr/testify/assert" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
func TestScanningWithEmptyProducerWillProduceNoResults(t *testing.T) { |
|
15
|
|
|
logger, _ := test.NewLogger() |
|
16
|
|
|
|
|
17
|
|
|
prod := producer.NewDictionaryProducer([]string{}, []string{}, 1) |
|
18
|
|
|
client := &http.Client{Timeout: time.Microsecond} |
|
19
|
|
|
sut := scan.NewScanner( |
|
20
|
|
|
client, |
|
21
|
|
|
prod, |
|
22
|
|
|
producer.NewReProducer(prod), |
|
23
|
|
|
logger, |
|
24
|
|
|
) |
|
25
|
|
|
|
|
26
|
|
|
results := sut.Scan(test.MustParseUrl(t, "http://localhost/"), 10) |
|
27
|
|
|
|
|
28
|
|
|
for r := range results { |
|
29
|
|
|
t.Fatalf("No results expected, got %s", r.Target.Path) |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
func TestScannerWillLogAnErrorWithInvalidDictionary(t *testing.T) { |
|
34
|
|
|
logger, loggerBuffer := test.NewLogger() |
|
35
|
|
|
|
|
36
|
|
|
prod := producer.NewDictionaryProducer( |
|
37
|
|
|
[]string{"\n"}, |
|
38
|
|
|
[]string{"/home"}, |
|
39
|
|
|
1, |
|
40
|
|
|
) |
|
41
|
|
|
|
|
42
|
|
|
client := &http.Client{Timeout: time.Microsecond} |
|
43
|
|
|
sut := scan.NewScanner( |
|
44
|
|
|
client, |
|
45
|
|
|
prod, |
|
46
|
|
|
producer.NewReProducer(prod), |
|
47
|
|
|
logger, |
|
48
|
|
|
) |
|
49
|
|
|
|
|
50
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
|
51
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), |
|
52
|
|
|
) |
|
53
|
|
|
defer testServer.Close() |
|
54
|
|
|
|
|
55
|
|
|
results := sut.Scan(test.MustParseUrl(t, testServer.URL), 10) |
|
56
|
|
|
|
|
57
|
|
|
for r := range results { |
|
58
|
|
|
t.Fatalf("No results expected, got %s", r.Target.Path) |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
assert.Contains(t, loggerBuffer.String(), "failed to build request") |
|
62
|
|
|
assert.Contains(t, loggerBuffer.String(), "invalid method") |
|
63
|
|
|
assert.Equal(t, 0, serverAssertion.Len()) |
|
64
|
|
|
} |
|
65
|
|
|
|