-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
185 lines (153 loc) · 4.58 KB
/
helper.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
// © 2016-2023 Graylog, Inc.
package logger
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"strings"
"sync"
)
var helperOnce sync.Once
type helper struct {
demoURL string
demoURL1 string
mockAgent string
mockHTML string
mockHTML2 string
mockHTML3 string
mockHTML4 string
mockHTML5 string
mockJSON string
mockJSONescaped string
mockNow int64
mockQueryString string
mockURL string
mockURLSdenied []string
mockURLSinvalid []string
mockFormData string
}
var testHelper *helper
// MockGetRequest covers a get request to compare against logging.
// https://appdividend.com/2019/12/02/golang-http-example-get-post-http-requests-in-golang/
func (testHelper *helper) MockGetRequest() (request http.Request, err error) {
resp, err := http.Get(testHelper.demoURL)
if err != nil {
log.Println(err)
return
}
request = *resp.Request
return
}
// func MockDoRequest() http.Request {
// helper := newTestHelper()
// resp, err := http.Get(helper.demoURL)
// resp, err = http.Do(resp)
// request := resp.Request
// return *request
// }
func (testHelper *helper) MockHeadRequest() (request http.Request, err error) {
resp, err := http.Head(testHelper.demoURL)
if err != nil {
log.Println(err)
return
}
request = *resp.Request
return
}
func (testHelper *helper) MockPostRequest() (request http.Request, err error) {
resp, err := http.Post(testHelper.demoURL, "html", bytes.NewBuffer([]byte(testHelper.mockJSON)))
if err != nil {
log.Println(err)
return
}
request = *resp.Request
return
}
func (testHelper *helper) MockPostFormRequest() (request http.Request, err error) {
form := url.Values{}
form.Add("username", "resurfaceio")
resp, err := http.PostForm(testHelper.demoURL, form)
if err != nil {
log.Println(err)
return
}
request = *resp.Request
return
}
func (h *helper) MockRequestWithJson() (request *http.Request) {
// request, _ := http.NewRequest("POST", h.mockURL, requestBody)
// request.Header.Add("Content-Type", "Application/JSON")
// request.PostForm.Add("message", "{ \"hello\" : \"world\" }")
requestBody, _ := json.Marshal(h.mockJSON)
request, _ = http.NewRequest("POST", h.mockURL, bytes.NewBuffer(requestBody))
request.Header.Add("content-type", "Application/JSON")
return
}
func (h *helper) MockRequestWithJson2() (request *http.Request) {
request = h.MockRequestWithJson()
request.Header.Add("ABC", "123")
request.Header.Add("A", "1")
request.Header.Add("A", "2")
request.Header.Add("ABC", "123")
request.Header.Add("ABC", "234")
return
}
func (h *helper) MockResponse() (response *http.Response) {
response = &http.Response{
Body: io.NopCloser(bytes.NewBufferString(h.mockHTML)),
Header: map[string][]string{},
}
return
}
func (h *helper) MockResponseWithHtml() (response *http.Response) {
response = h.MockResponse()
response.StatusCode = 200
response.Header.Add("content-type", "text/html; charset=utf-8")
response.Request = h.MockRequestWithJson2()
return
}
// https://golang.org/pkg/encoding/json/#example_Unmarshal
func parseable(msg string) bool {
if msg == "" || !strings.HasPrefix(msg, "[") || !strings.HasSuffix(msg, "]") || strings.Contains(msg, "[]") || strings.Contains(msg, ",,") {
return false
}
/* json.Valid won't work with our custom json formatted
we need to test our custom string over the wire first
then see if we can use Go native methods like json.Marshal
and json.Unmarshal
*/
return json.Valid([]byte(msg))
}
func newTestHelper() *helper {
helperOnce.Do(func() {
testHelper = &helper{
demoURL: "https://demo.resurface.io/ping",
demoURL1: "https://demo.resurface.io",
mockAgent: "helper.go",
mockHTML: "<html>Hello World!</html>",
mockHTML2: "<html>Hola Mundo!</html>",
mockHTML3: "<html>1 World 2 World Red World Blue World!</html>",
mockHTML4: "<html>1 World\n2 World\nRed World \nBlue World!\n</html>",
mockHTML5: "<html>\n<input type=\"hidden\">SENSITIVE</input>\n<input class='foo' type=\"hidden\">\nSENSITIVE\n</input>\n</html>",
mockJSON: `{ \"hello\" : \"world\" }`,
mockJSONescaped: "{ \\'hello\\' : \\'world\\' }",
mockNow: 1455908640173,
mockQueryString: "foo=bar",
mockURL: "http://something.com:3000/index.html",
mockURLSdenied: []string{
"https://demo.resurface.io/ping/noway3is5this1valid2",
"https://www.noway3is5this1valid2.com/",
},
mockURLSinvalid: []string{"",
"noway3is5this1valid2",
"ftp:\\www.noway3is5this1valid2.com/",
"urn:ISSN:1535–3613",
},
mockFormData: "\"username\": { \" ResurfaceIO \" ",
}
})
return testHelper
}