-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite_test.go
150 lines (132 loc) · 4.25 KB
/
suite_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
// Copyright 2024 WorkOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apirunner
import (
"io"
"net/http"
"strings"
"testing"
)
type MockHttpClient struct {
StatusCode int
Body string
Header map[string][]string
}
func (c *MockHttpClient) Do(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: c.StatusCode,
Body: io.NopCloser(strings.NewReader(c.Body)),
Header: c.Header,
}, nil
}
func TestBasicResponse(t *testing.T) {
mockClient := MockHttpClient{}
mockClient.StatusCode = 200
mockClient.Body = "{ \"id\": 1, \"name\": \"name\" }"
results, _ := ExecuteSuite(RunConfig{
BaseUrl: "",
CustomHeaders: nil,
HttpClient: &mockClient,
}, "basicresponse.json", true)
if len(results.Passed) == 0 {
t.Errorf("All tests should have passed.\n")
}
if len(results.Failed) > 0 {
for _, test := range results.Failed {
t.Errorf("Failed test result: [%s]\n", test.Result())
}
}
}
func TestListResponse(t *testing.T) {
mockClient := MockHttpClient{}
mockClient.StatusCode = 200
mockClient.Body = "[{\"id\": 1, \"name\": \"name1\"},{\"id\": 2,\"name\": \"name2\"}]"
respHeaders := make(map[string][]string)
respHeaders["Warrant-Token"] = []string{"asdf"}
mockClient.Header = respHeaders
results, _ := ExecuteSuite(RunConfig{
BaseUrl: "",
CustomHeaders: nil,
HttpClient: &mockClient,
}, "listresponse.json", true)
if len(results.Passed) == 0 {
t.Errorf("All tests should have passed.\n")
}
if len(results.Failed) > 0 {
for _, test := range results.Failed {
t.Errorf("Failed test result: [%s]\n", test.Result())
}
}
}
func TestIgnoredFields(t *testing.T) {
mockClient := MockHttpClient{}
mockClient.StatusCode = 200
mockClient.Body = "[{\"id\": 1, \"name\": \"name1\", \"nested\": {\"hello\": 1, \"createdAt\": \"2023-04-05T12:38:54.038Z\"}, \"createdAt\": \"2023-04-05T12:38:54.038Z\"},{\"id\": 2,\"name\": \"name2\", \"nested\": {\"hello\": 0, \"createdAt\": \"2023-04-05T12:38:54.036226Z\"}, \"createdAt\": \"2023-04-05T12:38:54.036226Z\"}]"
results, _ := ExecuteSuite(RunConfig{
BaseUrl: "",
CustomHeaders: nil,
HttpClient: &mockClient,
}, "ignoredfieldsresponse.json", true)
if len(results.Passed) == 0 {
t.Errorf("All tests should have passed.\n")
}
if len(results.Failed) > 0 {
for _, test := range results.Failed {
t.Errorf("Failed test result: [%s]\n", test.Result())
}
}
}
func TestTemplateVars(t *testing.T) {
mockClient := MockHttpClient{}
mockClient.StatusCode = 200
mockClient.Body = "{\"userId\": \"user_1\"}"
results, _ := ExecuteSuite(RunConfig{
BaseUrl: "",
CustomHeaders: nil,
HttpClient: &mockClient,
}, "templatevars.json", true)
if len(results.Passed) != 1 && len(results.Failed) != 1 && len(results.Skipped) != 0 {
t.Errorf("Expected 1 Passed, 1 Failed, 0 Skipped.")
}
if !strings.Contains(results.Failed[0].Result(), "missing template value for var: 'test1.userIdWrongVar'") {
t.Errorf("Expected failure result to contain string: 'missing template value for var: 'test1.userIdWrongVar''")
}
}
type EchoRequestHttpClient struct {
StatusCode int
}
func (c *EchoRequestHttpClient) Do(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: c.StatusCode,
Body: req.Body,
Header: req.Header,
}, nil
}
func TestParsesBodyCorrectly(t *testing.T) {
mockClient := EchoRequestHttpClient{}
mockClient.StatusCode = 200
results, _ := ExecuteSuite(RunConfig{
BaseUrl: "",
CustomHeaders: nil,
HttpClient: &mockClient,
}, "bodyparser.json", true)
if len(results.Passed) == 0 {
t.Errorf("All tests should have passed.\n")
}
if len(results.Failed) > 0 {
for _, test := range results.Failed {
t.Errorf("Failed test result: [%s]\n", test.Result())
}
}
}