-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler_test.go
154 lines (132 loc) · 3.93 KB
/
handler_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
package handler_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/USACE/instrumentation-api/api/internal/config"
"github.com/USACE/instrumentation-api/api/internal/handler"
"github.com/USACE/instrumentation-api/api/internal/server"
"github.com/stretchr/testify/assert"
"github.com/xeipuuv/gojsonschema"
)
const (
truncateLinesBody = 30
maxLines = 50
host = "http://localhost:8080"
mockJwt = `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ikw0YXFVRmd6YV9RVjhqc1ZOa281OW5GVzl6bGh1b0JGX3RxdlpkTUZkajQifQ.eyJzdWIiOiJmOGRjYWZlYS0yNDNlLTRiODktOGQ3ZC1mYTAxOTE4MTMwZjQiLCJ0eXAiOiJCZWFyZXIiLCJhbGxvd2VkLW9yaWdpbnMiOlsiaHR0cDovL2xvY2FsaG9zdDozMDAwIl0sIm5hbWUiOiJBbnRob255IExhbWJlcnQiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJ0ZXN0IiwiZ2l2ZW5fbmFtZSI6IkFudGhvbnkiLCJmYW1pbHlfbmFtZSI6IkxhbWJlcnQiLCJlbWFpbCI6ImFudGhvbnkubS5sYW1iZXJ0QGZha2UudXNhY2UuYXJteS5taWwiLCJzdWJqZWN0RE4iOiJsYW1iZXJ0LmFudGhvbnkubS4yIiwiY2FjVUlEIjoiMiJ9.8CjeifD51ZEZZOx9eeMd7RPanvtgkQQus-R19aU91Rw`
mockAppKey = "appkey"
)
const IDSlugNameArrSchema = `{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string" },
"slug": { "type": "string" },
"name": { "type": "string" }
}
}
}`
// HTTPTest contains parameters for HTTP Integration Tests
type HTTPTest struct {
Name string
URL string
Method string
Body string
ExpectedStatus int
ExpectedSchema *gojsonschema.Schema
authHeader string
}
// singleton api server since database is used in integration tests
var testApi *server.ApiServer
func testApiServer() *server.ApiServer {
if testApi == nil {
cfg := config.NewApiConfig()
h := handler.NewApi(cfg)
testApi = server.NewApiServer(cfg, h)
}
return testApi
}
// RunHTTPTest accepts a HTTPTest type to execute the HTTP request
func RunHTTPTest(v HTTPTest) (*http.Response, error) {
req, err := http.NewRequest(v.Method, host+v.URL, strings.NewReader(v.Body))
if err != nil {
return nil, err
}
if v.authHeader == "" {
v.authHeader = mockJwt
}
req.Header.Set("Authorization", v.authHeader)
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
s := testApiServer()
s.ServeHTTP(rr, req)
return rr.Result(), err
}
func RunAll(t *testing.T, tests []HTTPTest) {
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
run, httpErr := RunHTTPTest(v)
assert.Nil(t, httpErr, "error calling RunHTTPTest(v)")
if httpErr != nil {
t.Log(httpErr.Error())
}
assert.Equal(t, v.ExpectedStatus, run.StatusCode)
body, err := io.ReadAll(run.Body)
assert.Nil(t, err, "error calling io.ReadAll(run.Body)")
if err != nil {
t.Log(err.Error())
return
}
// truncate verbose -v output
var dst bytes.Buffer
if err := json.Indent(&dst, body, "", " "); err != nil {
s := string(body)
if len(s) > 500 {
s = fmt.Sprintf("%s\n...", s[:500])
}
t.Logf("could not format json response body: %s", s)
} else {
s := dst.String()
ss := strings.Split(s, "\n")
if len(ss) > truncateLinesBody {
s = fmt.Sprintf("%s\n...", strings.Join(ss[:truncateLinesBody], "\n"))
}
t.Logf("response body: %s", s)
}
if v.ExpectedStatus != run.StatusCode {
return
}
if v.ExpectedSchema != nil {
loader := gojsonschema.NewBytesLoader(body)
result, err := v.ExpectedSchema.Validate(loader)
assert.Nil(t, err, "error calling v.ExpectedSchema.Validate")
if err != nil {
return
}
valid := result.Valid()
assert.Truef(t, valid, "response body did not match json schema:")
if !valid {
var errs string
for idx, err := range result.Errors() {
if idx >= maxLines {
if idx == maxLines {
errs += "\n"
errs += "..."
}
continue
}
errs += "\n"
errs += err.String()
}
t.Log(errs)
}
}
})
}
}