-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathvalidations_test.go
133 lines (129 loc) · 3.62 KB
/
validations_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
package edgeworkers
import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestValidateBundle(t *testing.T) {
tests := map[string]struct {
params ValidateBundleRequest
responseStatus int
responseBody string
expectedPath string
expectedResponse *ValidateBundleResponse
withError error
}{
"200 OK": {
params: ValidateBundleRequest{Bundle{strings.NewReader("a valid bundle")}},
responseStatus: 200,
responseBody: `
{
"errors": [],
"warnings": []
}`,
expectedPath: "/edgeworkers/v1/validations",
expectedResponse: &ValidateBundleResponse{
Errors: []ValidationIssue{},
Warnings: []ValidationIssue{},
},
},
"200 OK with invalid gzip format error": {
params: ValidateBundleRequest{Bundle{strings.NewReader("invalid bundle format")}},
responseStatus: 200,
responseBody: `
{
"errors": [
{
"type": "INVALID_GZIP_FORMAT",
"message": "invalid GZIP file format"
}
],
"warnings": []
}`,
expectedPath: "/edgeworkers/v1/validations",
expectedResponse: &ValidateBundleResponse{
Errors: []ValidationIssue{
{
Type: "INVALID_GZIP_FORMAT",
Message: "invalid GZIP file format",
},
},
Warnings: []ValidationIssue{},
},
},
"200 OK with expiring token warning": {
params: ValidateBundleRequest{Bundle{bytes.NewReader([]byte("bundle with expiring token"))}},
responseStatus: 200,
responseBody: `
{
"errors": [],
"warnings": [
{
"type": "ACCESS_TOKEN_EXPIRING_SOON",
"message": "token expiring soon"
}
]
}`,
expectedPath: "/edgeworkers/v1/validations",
expectedResponse: &ValidateBundleResponse{
Errors: []ValidationIssue{},
Warnings: []ValidationIssue{
{
Type: "ACCESS_TOKEN_EXPIRING_SOON",
Message: "token expiring soon",
},
},
},
}, "500 internal server error": {
params: ValidateBundleRequest{Bundle{bytes.NewReader([]byte("a valid bundle"))}},
responseStatus: http.StatusInternalServerError,
responseBody: `
{
"type": "/edgeworkers/error-types/edgeworkers-server-error",
"title": "An unexpected error has occurred.",
"detail": "Error processing request",
"instance": "/edgeworkers/error-instances/abc",
"status": 500,
"errorCode": "EW4303"
}`,
expectedPath: "/edgeworkers/v1/validations",
withError: &Error{
Type: "/edgeworkers/error-types/edgeworkers-server-error",
Title: "An unexpected error has occurred.",
Detail: "Error processing request",
Instance: "/edgeworkers/error-instances/abc",
Status: 500,
ErrorCode: "EW4303",
},
},
"missing bundle reader": {
params: ValidateBundleRequest{},
withError: ErrStructValidation,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedPath, r.URL.String())
assert.Equal(t, http.MethodPost, r.Method)
w.WriteHeader(test.responseStatus)
_, err := w.Write([]byte(test.responseBody))
assert.NoError(t, err)
}))
client := mockAPIClient(t, mockServer)
result, err := client.ValidateBundle(context.Background(), test.params)
if test.withError != nil {
assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err)
return
}
require.NoError(t, err)
assert.Equal(t, test.expectedResponse, result)
})
}
}