|
1
|
|
|
package producer_test |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"net/http" |
|
5
|
|
|
"sort" |
|
6
|
|
|
"testing" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/stefanoj3/dirstalk/pkg/common/test" |
|
9
|
|
|
|
|
10
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan" |
|
11
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan/producer" |
|
12
|
|
|
"github.com/stretchr/testify/assert" |
|
13
|
|
|
) |
|
14
|
|
|
|
|
15
|
|
|
func TestNewReProducer(t *testing.T) { |
|
16
|
|
|
t.Parallel() |
|
17
|
|
|
|
|
18
|
|
|
methods := []string{http.MethodGet, http.MethodPost} |
|
19
|
|
|
dictionary := []string{"/home", "/about"} |
|
20
|
|
|
|
|
21
|
|
|
dictionaryProducer := producer.NewDictionaryProducer(methods, dictionary, 1) |
|
22
|
|
|
|
|
23
|
|
|
sut := producer.NewReProducer(dictionaryProducer) |
|
24
|
|
|
|
|
25
|
|
|
result := scan.Result{ |
|
26
|
|
|
Target: scan.Target{ |
|
27
|
|
|
Path: "/home", |
|
28
|
|
|
Method: http.MethodGet, |
|
29
|
|
|
Depth: 1, |
|
30
|
|
|
}, |
|
31
|
|
|
Response: &http.Response{ |
|
32
|
|
|
StatusCode: http.StatusOK, |
|
33
|
|
|
Request: &http.Request{ |
|
34
|
|
|
Method: http.MethodGet, |
|
35
|
|
|
URL: test.MustParseUrl(t, "http://mysite/contacts"), |
|
36
|
|
|
}, |
|
37
|
|
|
}, |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
reproducerFunc := sut.Reproduce() |
|
41
|
|
|
reproducerChannel := reproducerFunc(result) |
|
42
|
|
|
|
|
43
|
|
|
targets := make([]scan.Target, 0, 10) |
|
44
|
|
|
for tar := range reproducerChannel { |
|
45
|
|
|
targets = append(targets, tar) |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
sort.Slice(targets, func(i, j int) bool { |
|
49
|
|
|
return targets[i].Path < targets[j].Path && targets[i].Method < targets[j].Method |
|
50
|
|
|
}) |
|
51
|
|
|
|
|
52
|
|
|
assert.Len(t, targets, 4) |
|
53
|
|
|
|
|
54
|
|
|
expectedTargets := []scan.Target{ |
|
55
|
|
|
{ |
|
56
|
|
|
Path: "/home/home", |
|
57
|
|
|
Method: http.MethodGet, |
|
58
|
|
|
Depth: 0, |
|
59
|
|
|
}, |
|
60
|
|
|
{ |
|
61
|
|
|
Path: "/home/about", |
|
62
|
|
|
Method: http.MethodGet, |
|
63
|
|
|
Depth: 0, |
|
64
|
|
|
}, |
|
65
|
|
|
{ |
|
66
|
|
|
Path: "/home/home", |
|
67
|
|
|
Method: http.MethodPost, |
|
68
|
|
|
Depth: 0, |
|
69
|
|
|
}, |
|
70
|
|
|
{ |
|
71
|
|
|
Path: "/home/about", |
|
72
|
|
|
Method: http.MethodPost, |
|
73
|
|
|
Depth: 0, |
|
74
|
|
|
}, |
|
75
|
|
|
} |
|
76
|
|
|
assert.Equal(t, expectedTargets, targets) |
|
77
|
|
|
|
|
78
|
|
|
// reproducing again on the same result should not yield more targets |
|
79
|
|
|
reproducerChannel = reproducerFunc(result) |
|
80
|
|
|
|
|
81
|
|
|
targets = make([]scan.Target, 0) |
|
82
|
|
|
for tar := range reproducerChannel { |
|
83
|
|
|
targets = append(targets, tar) |
|
84
|
|
|
} |
|
85
|
|
|
assert.Len(t, targets, 0) |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
func TestReProducerShouldProduceNothingForDepthZero(t *testing.T) { |
|
89
|
|
|
t.Parallel() |
|
90
|
|
|
|
|
91
|
|
|
methods := []string{http.MethodGet, http.MethodPost} |
|
92
|
|
|
dictionary := []string{"/home", "/about"} |
|
93
|
|
|
|
|
94
|
|
|
dictionaryProducer := producer.NewDictionaryProducer(methods, dictionary, 1) |
|
95
|
|
|
|
|
96
|
|
|
sut := producer.NewReProducer(dictionaryProducer) |
|
97
|
|
|
|
|
98
|
|
|
result := scan.Result{ |
|
99
|
|
|
Target: scan.Target{ |
|
100
|
|
|
Path: "/home", |
|
101
|
|
|
Method: http.MethodGet, |
|
102
|
|
|
Depth: 0, |
|
103
|
|
|
}, |
|
104
|
|
|
Response: &http.Response{ |
|
105
|
|
|
StatusCode: http.StatusOK, |
|
106
|
|
|
Request: &http.Request{ |
|
107
|
|
|
Method: http.MethodGet, |
|
108
|
|
|
URL: test.MustParseUrl(t, "http://mysite/contacts"), |
|
109
|
|
|
}, |
|
110
|
|
|
}, |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
reproducerFunc := sut.Reproduce() |
|
114
|
|
|
reproducerChannel := reproducerFunc(result) |
|
115
|
|
|
|
|
116
|
|
|
targets := make([]scan.Target, 0) |
|
117
|
|
|
for tar := range reproducerChannel { |
|
118
|
|
|
targets = append(targets, tar) |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
assert.Len(t, targets, 0) |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
func TestReProducerShouldProduceNothingFor404Response(t *testing.T) { |
|
125
|
|
|
t.Parallel() |
|
126
|
|
|
|
|
127
|
|
|
methods := []string{http.MethodGet, http.MethodPost} |
|
128
|
|
|
dictionary := []string{"/home", "/about"} |
|
129
|
|
|
|
|
130
|
|
|
dictionaryProducer := producer.NewDictionaryProducer(methods, dictionary, 1) |
|
131
|
|
|
|
|
132
|
|
|
sut := producer.NewReProducer(dictionaryProducer) |
|
133
|
|
|
|
|
134
|
|
|
result := scan.Result{ |
|
135
|
|
|
Target: scan.Target{ |
|
136
|
|
|
Path: "/home", |
|
137
|
|
|
Method: http.MethodGet, |
|
138
|
|
|
Depth: 3, |
|
139
|
|
|
}, |
|
140
|
|
|
Response: &http.Response{ |
|
141
|
|
|
StatusCode: http.StatusNotFound, |
|
142
|
|
|
Request: &http.Request{ |
|
143
|
|
|
Method: http.MethodGet, |
|
144
|
|
|
URL: test.MustParseUrl(t, "http://mysite/contacts"), |
|
145
|
|
|
}, |
|
146
|
|
|
}, |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
reproducerFunc := sut.Reproduce() |
|
150
|
|
|
reproducerChannel := reproducerFunc(result) |
|
151
|
|
|
|
|
152
|
|
|
targets := make([]scan.Target, 0) |
|
153
|
|
|
for tar := range reproducerChannel { |
|
154
|
|
|
targets = append(targets, tar) |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
assert.Len(t, targets, 0) |
|
158
|
|
|
} |
|
159
|
|
|
|