-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry.go
More file actions
153 lines (130 loc) · 3.98 KB
/
Copy pathtry.go
File metadata and controls
153 lines (130 loc) · 3.98 KB
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
package try
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
type TestCase struct {
TestName string
Request Request
RequestBody interface{}
RequestReader io.Reader
RequestContentType string
RequestCookies []*http.Cookie
RequestHeaders map[string]string
Expected ExpectedResponse
AccessToken string
Setup func(testCase *TestCase)
Teardown func(testCase *TestCase, res *HijackableResponseRecorder)
DisplayResponse bool
}
type Request struct {
Method string
Url string
}
type ExpectedResponse struct {
StatusCode int
BodyPart string
BodyParts []string
BodyPartMissing string
BodyPartsMissing []string
Headers map[string]string
ExpectedCallBack func(res *HijackableResponseRecorder)
}
func GenerateRequest(testCase *TestCase) (*http.Request, error) {
reqJson, err := json.Marshal(testCase.RequestBody)
if err != nil {
return nil, err
}
var req *http.Request
if testCase.RequestReader != nil {
req, err = http.NewRequest(testCase.Request.Method, testCase.Request.Url, testCase.RequestReader)
} else {
req, err = http.NewRequest(testCase.Request.Method, testCase.Request.Url, bytes.NewBuffer(reqJson))
}
if err != nil {
return nil, err
}
// Set some default headers for simplicity. These can be overwritten further down on each test case if required.
if testCase.RequestContentType != "" {
req.Header.Set(echo.HeaderContentType, testCase.RequestContentType)
} else {
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
req.Header.Set(echo.HeaderXForwardedFor, "127.0.0.0")
// Add cookies in if present
if len(testCase.RequestCookies) > 0 {
for _, cookie := range testCase.RequestCookies {
req.AddCookie(cookie)
}
}
// Add headers if present
if len(testCase.RequestHeaders) > 0 {
for headerKey, headerValue := range testCase.RequestHeaders {
// Set requires to overrise content type. May need to be add if you need multiple headers
// with the same key.
req.Header.Set(headerKey, headerValue)
}
}
return req, nil
}
func ExecuteRequest(e *echo.Echo, req *http.Request) *HijackableResponseRecorder {
// Create a new recorder then process request with server.
rr := NewHijackableRecorder(nil)
e.ServeHTTP(rr, req)
return rr
}
func ValidateResults(t *testing.T, test *TestCase, res *HijackableResponseRecorder) {
if test.DisplayResponse {
fmt.Println("Request Output: ")
fmt.Println(res.Body.String())
}
if test.Expected.ExpectedCallBack != nil {
test.Expected.ExpectedCallBack(res)
}
if res.Code != 0 {
assert.Equal(t, test.Expected.StatusCode, res.Code)
}
if test.Expected.BodyPart != "" {
assert.Contains(t, res.Body.String(), test.Expected.BodyPart)
}
if len(test.Expected.BodyParts) > 0 {
for _, expectedText := range test.Expected.BodyParts {
assert.Contains(t, res.Body.String(), expectedText)
}
}
if test.Expected.BodyPartMissing != "" {
assert.NotContains(t, res.Body.String(), test.Expected.BodyPartMissing)
}
if len(test.Expected.BodyPartsMissing) > 0 {
for _, expectedText := range test.Expected.BodyPartsMissing {
assert.NotContains(t, res.Body.String(), expectedText)
}
}
if test.Expected.Headers != nil {
for headerKey, headerValue := range test.Expected.Headers {
assert.Equal(t, headerValue, res.Header().Get(headerKey))
}
}
}
func ExecuteTest(t *testing.T, e *echo.Echo, testCase *TestCase) {
// Run any setup required before we execute the request
if testCase.Setup != nil {
testCase.Setup(testCase)
}
req, err := GenerateRequest(testCase)
if err != nil {
t.Fatalf("unable to Generate Request: %v", err)
}
res := ExecuteRequest(e, req)
ValidateResults(t, testCase, res)
if testCase.Teardown != nil {
testCase.Teardown(testCase, res)
}
}