Passed
Pull Request — master (#83)
by Stefano
01:39
created

producer_test.TestNewReProducer   B

Complexity

Conditions 5

Size

Total Lines 70
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 43
nop 1
dl 0
loc 70
rs 8.3813
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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