-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservirtium_test.go
235 lines (200 loc) · 8.94 KB
/
servirtium_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package servirtium
import (
"fmt"
"log"
"net/http"
"os"
"regexp"
"testing"
"github.com/stretchr/testify/suite"
)
type ServirtiumTestSuite struct {
servirtium *Impl
suite.Suite
}
func TestServirtiumTestSuite(t *testing.T) {
suite.Run(t, new(ServirtiumTestSuite))
}
func (s *ServirtiumTestSuite) SetupTest() {
servirtium := NewServirtium()
s.servirtium = servirtium
}
func (s *ServirtiumTestSuite) TestInitServerPlaybackOnPort() {
s.servirtium.initPlaybackServerOnPort("mockName", 61416)
s.NotNil(s.servirtium.ServerPlayback)
s.servirtium.EndPlayback()
}
func (s *ServirtiumTestSuite) TestStartServerPlaybackOnPort() {
s.servirtium.StartPlayback("mockName", 61416)
s.NotNil(s.servirtium.ServerPlayback)
s.servirtium.EndPlayback()
}
func (s *ServirtiumTestSuite) TestAnualAvgHandlerPlayback() {
result := s.servirtium.playbackHandler("mockName")
s.NotNil(result)
}
func (s *ServirtiumTestSuite) TestGetPlaybackURL() {
s.servirtium.StartPlayback("mockName", 61417)
result := s.servirtium.GetPlaybackURL()
s.Equal("http://127.0.0.1:61417", result)
}
func (s *ServirtiumTestSuite) TestInitRecordServerOnPort() {
s.servirtium.initRecordServerOnPort("https://google.com", 61418)
s.NotNil(s.servirtium.ServerRecord)
s.servirtium.EndRecord()
}
func (s *ServirtiumTestSuite) TestStartRecordServerOnPort() {
s.servirtium.StartRecord("https://google.com", 61419)
s.NotNil(s.servirtium.ServerRecord)
s.servirtium.EndRecord()
}
func (s *ServirtiumTestSuite) TestGetRecordURL() {
s.servirtium.StartRecord("mockName", 61420)
result := s.servirtium.GetRecordURL()
s.Equal("http://127.0.0.1:61420", result)
}
func (s *ServirtiumTestSuite) TestManInTheMiddleHandler() {
result := s.servirtium.recordHandler("https://google.com")
s.NotNil(result)
}
func (s *ServirtiumTestSuite) TestCheckMarkdownExist_True() {
workingPath, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
result := s.servirtium.checkMarkdownExists(fmt.Sprintf("%s/servirtium.go", workingPath))
s.True(result)
}
func (s *ServirtiumTestSuite) TestCheckMarkdownExist_False() {
result := s.servirtium.checkMarkdownExists("./servitium1.go")
s.False(result)
}
func (s *ServirtiumTestSuite) TestAppendContentInFile_WhenSequenceZero() {
s.servirtium.interactionSequence = 0
result := s.servirtium.appendContentInFile("currentContent", "newContent")
s.Equal(result, "newContent")
}
func (s *ServirtiumTestSuite) TestAppendContentInFile_WhenSequenceOne() {
s.servirtium.interactionSequence = 1
result := s.servirtium.appendContentInFile("currentContent", "newContent")
s.Equal("currentContent\nnewContent", result)
}
func (s *ServirtiumTestSuite) TestRecord() {
params := recordData{
RequestMethod: "GET",
RequestURLPath: "http://google.com",
RequestHeader: nil,
RequestBody: "request",
ResponseContentType: "application/json",
ResponseBody: "response",
ResponseHeader: nil,
ResponseStatus: 200,
}
s.servirtium.interactionSequence = 0
s.servirtium.record(params)
s.NotNil(s.servirtium.content)
}
func (s *ServirtiumTestSuite) TestReplaceContent() {
inputContent := "<password>your password</password>"
passwordRegex := regexp.MustCompile(`(<password>.{0,}<\/password>)`)
result := replaceContent(inputContent, map[*regexp.Regexp]string{passwordRegex: "<password>MASKED</password>"})
s.Equal("<password>MASKED</password>", result)
}
func (s *ServirtiumTestSuite) TestRemoveHeaders() {
mockHeader := http.Header{"User-Agent": []string{"123"}, "Authorization": []string{"mockAuthorization"}}
expected := http.Header{"User-Agent": []string{"123"}}
result := removeHeaders(mockHeader, []string{"Authorization"})
s.Equal(expected, result)
}
func (s *ServirtiumTestSuite) TestReplaceHeaders() {
mockHeader := http.Header{"User-Agent": []string{"123"}, "Authorization": []string{"mockAuthorization"}}
expected := http.Header{"User-Agent": []string{"123"}, "Authorization": []string{"MASKED"}}
authorizationRegex := regexp.MustCompile(`(Authorization: .{0,})`)
result := replaceHeaders(mockHeader, map[*regexp.Regexp]string{authorizationRegex: "Authorization: MASKED"})
s.Equal(expected, result)
}
func (s *ServirtiumTestSuite) TestRemoveHeadersPlayback() {
mockHeader := map[string]string{"User-Agent": "123", "Authorization": "mockAuthorization"}
expected := map[string]string{"User-Agent": "123"}
result := removeHeadersPlayback(mockHeader, []string{"Authorization"})
s.Equal(expected, result)
}
func (s *ServirtiumTestSuite) TestReplaceHeadersPlayback() {
mockHeader := map[string]string{"User-Agent": "123", "Authorization": "mockAuthorization"}
expected := map[string]string{"User-Agent": "123", "Authorization": "MASKED"}
authorizationRegex := regexp.MustCompile(`(Authorization: .{0,})`)
result := replaceHeadersPlayback(mockHeader, map[*regexp.Regexp]string{authorizationRegex: "Authorization: MASKED"})
s.Equal(expected, result)
}
func (s *ServirtiumTestSuite) TestSetCallerRequestHeadersRemoval() {
s.servirtium.SetCallerRequestHeadersRemoval([]string{"Authorization"})
expected := []string{"Authorization"}
s.Equal(expected, s.servirtium.callerRequestHeadersRemoval)
}
func (s *ServirtiumTestSuite) TestSetCallerRequestHeaderReplacements() {
authorizationRegex := regexp.MustCompile(`(Authorization: .{0,})`)
s.servirtium.SetCallerRequestHeaderReplacements(map[*regexp.Regexp]string{authorizationRegex: "123"})
expected := map[*regexp.Regexp]string{authorizationRegex: "123"}
s.Equal(expected, s.servirtium.callerRequestHeaderReplacements)
}
func (s *ServirtiumTestSuite) TestSetCallerRequestBodyReplacement() {
inputContent := "<password>your password</password>"
passwordRegex := regexp.MustCompile(`(<password>.{0,}<\/password>)`)
expected := map[*regexp.Regexp]string{passwordRegex: inputContent}
s.servirtium.SetCallerRequestBodyReplacement(map[*regexp.Regexp]string{passwordRegex: inputContent})
s.Equal(expected, s.servirtium.callerRequestBodyReplacement)
}
func (s *ServirtiumTestSuite) TestSetRecordRequestHeadersRemoval() {
s.servirtium.SetRecordRequestHeadersRemoval([]string{"Authorization"})
expected := []string{"Authorization"}
s.Equal(expected, s.servirtium.recordRequestHeadersRemoval)
}
func (s *ServirtiumTestSuite) TestSetRecordRequestHeaderReplacements() {
authorizationRegex := regexp.MustCompile(`(Authorization: .{0,})`)
s.servirtium.SetRecordRequestHeaderReplacements(map[*regexp.Regexp]string{authorizationRegex: "123"})
expected := map[*regexp.Regexp]string{authorizationRegex: "123"}
s.Equal(expected, s.servirtium.recordRequestHeaderReplacements)
}
func (s *ServirtiumTestSuite) TestSetRecordRequestBodyReplacement() {
inputContent := "<password>your password</password>"
passwordRegex := regexp.MustCompile(`(<password>.{0,}<\/password>)`)
expected := map[*regexp.Regexp]string{passwordRegex: inputContent}
s.servirtium.SetRecordRequestBodyReplacement(map[*regexp.Regexp]string{passwordRegex: inputContent})
s.Equal(expected, s.servirtium.recordRequestBodyReplacement)
}
func (s *ServirtiumTestSuite) TestSetCallerResponseHeadersRemoval() {
s.servirtium.SetCallerResponseHeadersRemoval([]string{"Authorization"})
expected := []string{"Authorization"}
s.Equal(expected, s.servirtium.callerResponseHeadersRemoval)
}
func (s *ServirtiumTestSuite) TestSetCallerResponseHeaderReplacements() {
authorizationRegex := regexp.MustCompile(`(Authorization: .{0,})`)
s.servirtium.SetCallerResponseHeaderReplacements(map[*regexp.Regexp]string{authorizationRegex: "123"})
expected := map[*regexp.Regexp]string{authorizationRegex: "123"}
s.Equal(expected, s.servirtium.callerResponseHeaderReplacements)
}
func (s *ServirtiumTestSuite) TestSetCallerResponseBodyReplacement() {
inputContent := "<password>your password</password>"
passwordRegex := regexp.MustCompile(`(<password>.{0,}<\/password>)`)
expected := map[*regexp.Regexp]string{passwordRegex: inputContent}
s.servirtium.SetCallerResponseBodyReplacement(map[*regexp.Regexp]string{passwordRegex: inputContent})
s.Equal(expected, s.servirtium.callerResponseBodyReplacement)
}
func (s *ServirtiumTestSuite) TestSetRecordResponseHeadersRemoval() {
s.servirtium.SetRecordResponseHeadersRemoval([]string{"Authorization"})
expected := []string{"Authorization"}
s.Equal(expected, s.servirtium.recordResponseHeadersRemoval)
}
func (s *ServirtiumTestSuite) TestSetRecordResponseHeaderReplacements() {
authorizationRegex := regexp.MustCompile(`(Authorization: .{0,})`)
s.servirtium.SetRecordResponseHeaderReplacements(map[*regexp.Regexp]string{authorizationRegex: "123"})
expected := map[*regexp.Regexp]string{authorizationRegex: "123"}
s.Equal(expected, s.servirtium.recordResponseHeaderReplacements)
}
func (s *ServirtiumTestSuite) TestSetRecordResponseBodyReplacement() {
inputContent := "<password>your password</password>"
passwordRegex := regexp.MustCompile(`(<password>.{0,}<\/password>)`)
expected := map[*regexp.Regexp]string{passwordRegex: inputContent}
s.servirtium.SetRecordResponseBodyReplacement(map[*regexp.Regexp]string{passwordRegex: inputContent})
s.Equal(expected, s.servirtium.recordResponseBodyReplacement)
}