1
|
|
|
package tree_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"net/http" |
5
|
|
|
"testing" |
6
|
|
|
|
7
|
|
|
"github.com/stefanoj3/dirstalk/pkg/common/test" |
8
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan" |
9
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan/summarizer/tree" |
10
|
|
|
"github.com/stretchr/testify/assert" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
var testResult string |
14
|
|
|
|
15
|
|
|
func TestNewResultTreePrinter(t *testing.T) { |
16
|
|
|
results := []scan.Result{ |
17
|
|
|
scan.NewResult( |
18
|
|
|
scan.Target{ |
19
|
|
|
Method: http.MethodPost, |
20
|
|
|
Path: "/", |
21
|
|
|
}, |
22
|
|
|
&http.Response{ |
23
|
|
|
StatusCode: http.StatusCreated, |
24
|
|
|
Request: &http.Request{ |
25
|
|
|
URL: test.MustParseURL(t, "http://mysite/"), |
26
|
|
|
}, |
27
|
|
|
}, |
28
|
|
|
), |
29
|
|
|
scan.NewResult( |
30
|
|
|
scan.Target{ |
31
|
|
|
Method: http.MethodPost, |
32
|
|
|
Path: "/home", |
33
|
|
|
}, |
34
|
|
|
&http.Response{ |
35
|
|
|
StatusCode: http.StatusCreated, |
36
|
|
|
Request: &http.Request{ |
37
|
|
|
URL: test.MustParseURL(t, "http://mysite/home"), |
38
|
|
|
}, |
39
|
|
|
}, |
40
|
|
|
), |
41
|
|
|
scan.NewResult( |
42
|
|
|
scan.Target{ |
43
|
|
|
Method: http.MethodPost, |
44
|
|
|
Path: "/home/123/", |
45
|
|
|
}, |
46
|
|
|
&http.Response{ |
47
|
|
|
StatusCode: http.StatusCreated, |
48
|
|
|
Request: &http.Request{ |
49
|
|
|
URL: test.MustParseURL(t, "http://mysite/home/123"), |
50
|
|
|
}, |
51
|
|
|
}, |
52
|
|
|
), |
53
|
|
|
scan.NewResult( |
54
|
|
|
scan.Target{ |
55
|
|
|
Method: http.MethodPost, |
56
|
|
|
Path: "/about", |
57
|
|
|
}, |
58
|
|
|
&http.Response{ |
59
|
|
|
StatusCode: http.StatusCreated, |
60
|
|
|
Request: &http.Request{ |
61
|
|
|
URL: test.MustParseURL(t, "http://mysite/about"), |
62
|
|
|
}, |
63
|
|
|
}, |
64
|
|
|
), |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
actual := tree.NewResultTreeProducer().String(results) |
68
|
|
|
|
69
|
|
|
expected := `/ |
70
|
|
|
├── about |
71
|
|
|
└── home |
72
|
|
|
└── 123 |
73
|
|
|
` |
74
|
|
|
|
75
|
|
|
assert.Equal(t, expected, actual) |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
func BenchmarkResultTree(b *testing.B) { |
79
|
|
|
results := []scan.Result{ |
80
|
|
|
scan.NewResult( |
81
|
|
|
scan.Target{ |
82
|
|
|
Method: http.MethodPost, |
83
|
|
|
Path: "/", |
84
|
|
|
}, |
85
|
|
|
&http.Response{ |
86
|
|
|
StatusCode: http.StatusCreated, |
87
|
|
|
Request: &http.Request{ |
88
|
|
|
URL: test.MustParseURL(b, "/"), |
89
|
|
|
}, |
90
|
|
|
}, |
91
|
|
|
), |
92
|
|
|
scan.NewResult( |
93
|
|
|
scan.Target{ |
94
|
|
|
Method: http.MethodPost, |
95
|
|
|
Path: "/home", |
96
|
|
|
}, |
97
|
|
|
&http.Response{ |
98
|
|
|
StatusCode: http.StatusCreated, |
99
|
|
|
Request: &http.Request{ |
100
|
|
|
URL: test.MustParseURL(b, "http://mysite/home"), |
101
|
|
|
}, |
102
|
|
|
}, |
103
|
|
|
), |
104
|
|
|
scan.NewResult( |
105
|
|
|
scan.Target{ |
106
|
|
|
Method: http.MethodPost, |
107
|
|
|
Path: "/home/123", |
108
|
|
|
}, |
109
|
|
|
&http.Response{ |
110
|
|
|
StatusCode: http.StatusCreated, |
111
|
|
|
Request: &http.Request{ |
112
|
|
|
URL: test.MustParseURL(b, "http://mysite/home/123"), |
113
|
|
|
}, |
114
|
|
|
}, |
115
|
|
|
), |
116
|
|
|
scan.NewResult( |
117
|
|
|
scan.Target{ |
118
|
|
|
Method: http.MethodPost, |
119
|
|
|
Path: "/about", |
120
|
|
|
}, |
121
|
|
|
&http.Response{ |
122
|
|
|
StatusCode: http.StatusCreated, |
123
|
|
|
Request: &http.Request{ |
124
|
|
|
URL: test.MustParseURL(b, "http://mysite/about"), |
125
|
|
|
}, |
126
|
|
|
}, |
127
|
|
|
), |
128
|
|
|
scan.NewResult( |
129
|
|
|
scan.Target{ |
130
|
|
|
Method: http.MethodPost, |
131
|
|
|
Path: "/about", |
132
|
|
|
}, |
133
|
|
|
&http.Response{ |
134
|
|
|
StatusCode: http.StatusCreated, |
135
|
|
|
Request: &http.Request{ |
136
|
|
|
URL: test.MustParseURL(b, "http://mysite/about"), |
137
|
|
|
}, |
138
|
|
|
}, |
139
|
|
|
), |
140
|
|
|
scan.NewResult( |
141
|
|
|
scan.Target{ |
142
|
|
|
Method: http.MethodPost, |
143
|
|
|
Path: "/about", |
144
|
|
|
}, |
145
|
|
|
&http.Response{ |
146
|
|
|
StatusCode: http.StatusCreated, |
147
|
|
|
Request: &http.Request{ |
148
|
|
|
URL: test.MustParseURL(b, "http://mysite/about"), |
149
|
|
|
}, |
150
|
|
|
}, |
151
|
|
|
), |
152
|
|
|
scan.NewResult( |
153
|
|
|
scan.Target{ |
154
|
|
|
Method: http.MethodPost, |
155
|
|
|
Path: "/about/1", |
156
|
|
|
}, |
157
|
|
|
&http.Response{ |
158
|
|
|
StatusCode: http.StatusCreated, |
159
|
|
|
Request: &http.Request{ |
160
|
|
|
URL: test.MustParseURL(b, "/about/1"), |
161
|
|
|
}, |
162
|
|
|
}, |
163
|
|
|
), |
164
|
|
|
scan.NewResult( |
165
|
|
|
scan.Target{ |
166
|
|
|
Method: http.MethodPost, |
167
|
|
|
Path: "/about/12", |
168
|
|
|
}, |
169
|
|
|
&http.Response{ |
170
|
|
|
StatusCode: http.StatusCreated, |
171
|
|
|
Request: &http.Request{ |
172
|
|
|
URL: test.MustParseURL(b, "/about/12"), |
173
|
|
|
}, |
174
|
|
|
}, |
175
|
|
|
), |
176
|
|
|
scan.NewResult( |
177
|
|
|
scan.Target{ |
178
|
|
|
Method: http.MethodPost, |
179
|
|
|
Path: "/about/123", |
180
|
|
|
}, |
181
|
|
|
&http.Response{ |
182
|
|
|
StatusCode: http.StatusCreated, |
183
|
|
|
Request: &http.Request{ |
184
|
|
|
URL: test.MustParseURL(b, "/about/123"), |
185
|
|
|
}, |
186
|
|
|
}, |
187
|
|
|
), |
188
|
|
|
scan.NewResult( |
189
|
|
|
scan.Target{ |
190
|
|
|
Method: http.MethodPost, |
191
|
|
|
Path: "/about/1/2/3", |
192
|
|
|
}, |
193
|
|
|
&http.Response{ |
194
|
|
|
StatusCode: http.StatusCreated, |
195
|
|
|
Request: &http.Request{ |
196
|
|
|
URL: test.MustParseURL(b, "/about/1/2/3"), |
197
|
|
|
}, |
198
|
|
|
}, |
199
|
|
|
), |
200
|
|
|
scan.NewResult( |
201
|
|
|
scan.Target{ |
202
|
|
|
Method: http.MethodPost, |
203
|
|
|
Path: "/about/1/2/a", |
204
|
|
|
}, |
205
|
|
|
&http.Response{ |
206
|
|
|
StatusCode: http.StatusCreated, |
207
|
|
|
Request: &http.Request{ |
208
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a"), |
209
|
|
|
}, |
210
|
|
|
}, |
211
|
|
|
), |
212
|
|
|
scan.NewResult( |
213
|
|
|
scan.Target{ |
214
|
|
|
Method: http.MethodPost, |
215
|
|
|
Path: "/about/1/2/b", |
216
|
|
|
}, |
217
|
|
|
&http.Response{ |
218
|
|
|
StatusCode: http.StatusCreated, |
219
|
|
|
Request: &http.Request{ |
220
|
|
|
URL: test.MustParseURL(b, "/about/1/2/b"), |
221
|
|
|
}, |
222
|
|
|
}, |
223
|
|
|
), |
224
|
|
|
scan.NewResult( |
225
|
|
|
scan.Target{ |
226
|
|
|
Method: http.MethodPost, |
227
|
|
|
Path: "/about/1/2/b/c/d/e", |
228
|
|
|
}, |
229
|
|
|
&http.Response{ |
230
|
|
|
StatusCode: http.StatusCreated, |
231
|
|
|
Request: &http.Request{ |
232
|
|
|
URL: test.MustParseURL(b, "/about/1/2/b/c/d/e"), |
233
|
|
|
}, |
234
|
|
|
}, |
235
|
|
|
), |
236
|
|
|
scan.NewResult( |
237
|
|
|
scan.Target{ |
238
|
|
|
Method: http.MethodPost, |
239
|
|
|
Path: "/about/1/2/b/c/f/e", |
240
|
|
|
}, |
241
|
|
|
&http.Response{ |
242
|
|
|
StatusCode: http.StatusCreated, |
243
|
|
|
Request: &http.Request{ |
244
|
|
|
URL: test.MustParseURL(b, "/about/1/2/b/c/f/e"), |
245
|
|
|
}, |
246
|
|
|
}, |
247
|
|
|
), |
248
|
|
|
scan.NewResult( |
249
|
|
|
scan.Target{ |
250
|
|
|
Method: http.MethodPost, |
251
|
|
|
Path: "/about/1/2/a/c/f/e", |
252
|
|
|
}, |
253
|
|
|
&http.Response{ |
254
|
|
|
StatusCode: http.StatusCreated, |
255
|
|
|
Request: &http.Request{ |
256
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e"), |
257
|
|
|
}, |
258
|
|
|
}, |
259
|
|
|
), |
260
|
|
|
scan.NewResult( |
261
|
|
|
scan.Target{ |
262
|
|
|
Method: http.MethodPost, |
263
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/i/l/m/n/o/p/q", |
264
|
|
|
}, |
265
|
|
|
&http.Response{ |
266
|
|
|
StatusCode: http.StatusCreated, |
267
|
|
|
Request: &http.Request{ |
268
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/i/l/m/n/o/p/q"), |
269
|
|
|
}, |
270
|
|
|
}, |
271
|
|
|
), |
272
|
|
|
scan.NewResult( |
273
|
|
|
scan.Target{ |
274
|
|
|
Method: http.MethodPost, |
275
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/1/l/m/n/o/p/q", |
276
|
|
|
}, |
277
|
|
|
&http.Response{ |
278
|
|
|
StatusCode: http.StatusCreated, |
279
|
|
|
Request: &http.Request{ |
280
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/1/l/m/n/o/p/q"), |
281
|
|
|
}, |
282
|
|
|
}, |
283
|
|
|
), |
284
|
|
|
scan.NewResult( |
285
|
|
|
scan.Target{ |
286
|
|
|
Method: http.MethodPost, |
287
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/2/l/m/n/o/p/q", |
288
|
|
|
}, |
289
|
|
|
&http.Response{ |
290
|
|
|
StatusCode: http.StatusCreated, |
291
|
|
|
Request: &http.Request{ |
292
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/2/l/m/n/o/p/q"), |
293
|
|
|
}, |
294
|
|
|
}, |
295
|
|
|
), |
296
|
|
|
scan.NewResult( |
297
|
|
|
scan.Target{ |
298
|
|
|
Method: http.MethodPost, |
299
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/3/l/m/n/o/p/q", |
300
|
|
|
}, |
301
|
|
|
&http.Response{ |
302
|
|
|
StatusCode: http.StatusCreated, |
303
|
|
|
Request: &http.Request{ |
304
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/3/l/m/n/o/p/q"), |
305
|
|
|
}, |
306
|
|
|
}, |
307
|
|
|
), |
308
|
|
|
scan.NewResult( |
309
|
|
|
scan.Target{ |
310
|
|
|
Method: http.MethodPost, |
311
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/4/l/m/n/o/p/q", |
312
|
|
|
}, |
313
|
|
|
&http.Response{ |
314
|
|
|
StatusCode: http.StatusCreated, |
315
|
|
|
Request: &http.Request{ |
316
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/4/l/m/n/o/p/q"), |
317
|
|
|
}, |
318
|
|
|
}, |
319
|
|
|
), |
320
|
|
|
scan.NewResult( |
321
|
|
|
scan.Target{ |
322
|
|
|
Method: http.MethodPost, |
323
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/4/1/m/n/o/p/q", |
324
|
|
|
}, |
325
|
|
|
&http.Response{ |
326
|
|
|
StatusCode: http.StatusCreated, |
327
|
|
|
Request: &http.Request{ |
328
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/4/1/m/n/o/p/q"), |
329
|
|
|
}, |
330
|
|
|
}, |
331
|
|
|
), |
332
|
|
|
scan.NewResult( |
333
|
|
|
scan.Target{ |
334
|
|
|
Method: http.MethodPost, |
335
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/4/2/m/n/o/p/q", |
336
|
|
|
}, |
337
|
|
|
&http.Response{ |
338
|
|
|
StatusCode: http.StatusCreated, |
339
|
|
|
Request: &http.Request{ |
340
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/4/2/m/n/o/p/q"), |
341
|
|
|
}, |
342
|
|
|
}, |
343
|
|
|
), |
344
|
|
|
scan.NewResult( |
345
|
|
|
scan.Target{ |
346
|
|
|
Method: http.MethodPost, |
347
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/4/2/m/n/o/p/u", |
348
|
|
|
}, |
349
|
|
|
&http.Response{ |
350
|
|
|
StatusCode: http.StatusCreated, |
351
|
|
|
Request: &http.Request{ |
352
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/4/2/m/n/o/p/u"), |
353
|
|
|
}, |
354
|
|
|
}, |
355
|
|
|
), |
356
|
|
|
scan.NewResult( |
357
|
|
|
scan.Target{ |
358
|
|
|
Method: http.MethodPost, |
359
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/4/2/m/n/o/p/z", |
360
|
|
|
}, |
361
|
|
|
&http.Response{ |
362
|
|
|
StatusCode: http.StatusCreated, |
363
|
|
|
Request: &http.Request{ |
364
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/4/2/m/n/o/p/z"), |
365
|
|
|
}, |
366
|
|
|
}, |
367
|
|
|
), |
368
|
|
|
scan.NewResult( |
369
|
|
|
scan.Target{ |
370
|
|
|
Method: http.MethodPost, |
371
|
|
|
Path: "/about/1/2/a/c/f/e/g/h/4/2/m/n/o/p/z/1", |
372
|
|
|
}, |
373
|
|
|
&http.Response{ |
374
|
|
|
StatusCode: http.StatusCreated, |
375
|
|
|
Request: &http.Request{ |
376
|
|
|
URL: test.MustParseURL(b, "/about/1/2/a/c/f/e/g/h/4/2/m/n/o/p/z/1"), |
377
|
|
|
}, |
378
|
|
|
}, |
379
|
|
|
), |
380
|
|
|
scan.NewResult( |
381
|
|
|
scan.Target{ |
382
|
|
|
Method: http.MethodPost, |
383
|
|
|
Path: "/somepage", |
384
|
|
|
}, |
385
|
|
|
&http.Response{ |
386
|
|
|
StatusCode: http.StatusCreated, |
387
|
|
|
Request: &http.Request{ |
388
|
|
|
URL: test.MustParseURL(b, "/somepage"), |
389
|
|
|
}, |
390
|
|
|
}, |
391
|
|
|
), |
392
|
|
|
scan.NewResult( |
393
|
|
|
scan.Target{ |
394
|
|
|
Method: http.MethodPost, |
395
|
|
|
Path: "/anotherpage", |
396
|
|
|
}, |
397
|
|
|
&http.Response{ |
398
|
|
|
StatusCode: http.StatusCreated, |
399
|
|
|
Request: &http.Request{ |
400
|
|
|
URL: test.MustParseURL(b, "/anotherpage"), |
401
|
|
|
}, |
402
|
|
|
}, |
403
|
|
|
), |
404
|
|
|
scan.NewResult( |
405
|
|
|
scan.Target{ |
406
|
|
|
Method: http.MethodPost, |
407
|
|
|
Path: "/anotherpage2", |
408
|
|
|
}, |
409
|
|
|
&http.Response{ |
410
|
|
|
StatusCode: http.StatusCreated, |
411
|
|
|
Request: &http.Request{ |
412
|
|
|
URL: test.MustParseURL(b, "/anotherpage2"), |
413
|
|
|
}, |
414
|
|
|
}, |
415
|
|
|
), |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
b.ResetTimer() |
419
|
|
|
|
420
|
|
|
for i := 0; i < b.N; i++ { |
421
|
|
|
testResult = tree.NewResultTreeProducer().String(results) |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
testResult += "1" |
425
|
|
|
} |
426
|
|
|
|