Passed
Push — master ( f028f1...4fb92b )
by Stefano
02:12
created

producer_test.TestReProducerShouldProduceNothingFor404Response   A

Complexity

Conditions 2

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nop 1
dl 0
loc 33
rs 9.376
c 0
b 0
f 0
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.NewResult(
26
		scan.Target{
27
			Path:   "/home",
28
			Method: http.MethodGet,
29
			Depth:  1,
30
		},
31
		&http.Response{
32
			StatusCode: http.StatusOK,
33
			Request: &http.Request{
34
				URL: test.MustParseUrl(t, "http://mysite/contacts"),
35
			},
36
		},
37
	)
38
39
	reproducerFunc := sut.Reproduce()
40
	reproducerChannel := reproducerFunc(result)
41
42
	targets := make([]scan.Target, 0, 10)
43
	for tar := range reproducerChannel {
44
		targets = append(targets, tar)
45
	}
46
47
	sort.Slice(targets, func(i, j int) bool {
48
		return targets[i].Path < targets[j].Path && targets[i].Method < targets[j].Method
49
	})
50
51
	assert.Len(t, targets, 4)
52
53
	expectedTargets := []scan.Target{
54
		{
55
			Path:   "/home/home",
56
			Method: http.MethodGet,
57
			Depth:  0,
58
		},
59
		{
60
			Path:   "/home/about",
61
			Method: http.MethodGet,
62
			Depth:  0,
63
		},
64
		{
65
			Path:   "/home/home",
66
			Method: http.MethodPost,
67
			Depth:  0,
68
		},
69
		{
70
			Path:   "/home/about",
71
			Method: http.MethodPost,
72
			Depth:  0,
73
		},
74
	}
75
	assert.Equal(t, expectedTargets, targets)
76
77
	// reproducing again on the same result should not yield more targets
78
	reproducerChannel = reproducerFunc(result)
79
80
	targets = make([]scan.Target, 0)
81
	for tar := range reproducerChannel {
82
		targets = append(targets, tar)
83
	}
84
	assert.Len(t, targets, 0)
85
}
86
87
func TestReProducerShouldProduceNothingForDepthZero(t *testing.T) {
88
	t.Parallel()
89
90
	methods := []string{http.MethodGet, http.MethodPost}
91
	dictionary := []string{"/home", "/about"}
92
93
	dictionaryProducer := producer.NewDictionaryProducer(methods, dictionary, 1)
94
95
	sut := producer.NewReProducer(dictionaryProducer)
96
97
	result := scan.NewResult(
98
		scan.Target{
99
			Path:   "/home",
100
			Method: http.MethodGet,
101
			Depth:  0,
102
		},
103
		&http.Response{
104
			StatusCode: http.StatusOK,
105
			Request: &http.Request{
106
				URL: test.MustParseUrl(t, "http://mysite/contacts"),
107
			},
108
		},
109
	)
110
111
	reproducerFunc := sut.Reproduce()
112
	reproducerChannel := reproducerFunc(result)
113
114
	targets := make([]scan.Target, 0)
115
	for tar := range reproducerChannel {
116
		targets = append(targets, tar)
117
	}
118
119
	assert.Len(t, targets, 0)
120
}
121