-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtesting.go
188 lines (148 loc) · 4.05 KB
/
testing.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
186
187
188
package gopherman
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"testing"
"github.com/pkg/errors"
"github.com/cohix/gopherman/postman"
"github.com/stretchr/testify/assert"
)
// Tester represents a collection test tool
type Tester struct {
Environment *postman.Environment
Client *http.Client
Collections []postman.Collection
Hostname string
Port string
}
// NewTesterWithCollection loads a collection from a file
func NewTesterWithCollection(path string, envFile string, files ...string) (*Tester, error) {
env, err := postman.EnvironmentFromFile(filepath.Join(path, envFile))
if err != nil {
return nil, err
}
collections := make([]postman.Collection, len(files))
for i, name := range files {
file, err := ioutil.ReadFile(filepath.Join(path, name))
if err != nil {
return nil, err
}
collection := postman.Collection{}
if err := json.Unmarshal(file, &collection); err != nil {
return nil, err
}
collections[i] = collection
}
tester := Tester{
Environment: env,
Client: http.DefaultClient,
Collections: collections,
Hostname: "localhost",
Port: "3002",
}
return &tester, nil
}
// TestRequestWithName finds the named request in the collection, makes the same request, and then returns the request, expected response, and actual response
func (t *Tester) TestRequestWithName(name string, tst *testing.T, handler func(*TestHelper, *postman.Request, *postman.Response, *postman.Response)) []error {
vars := t.Environment.VariableMap()
tmplHost, err := postman.SubstVars("{{ .BaseUrl }}:{{ .Port }}", vars)
if err != nil {
fmt.Println(err)
tmplHost = "localhost:8080"
}
errs := []error{}
for _, collection := range t.Collections {
helper := NewTestHelper(tst)
// put this in a func so that critical errors can be collected and then bail out
func() {
itm := collection.ItemWithName(name)
if itm == nil {
helper.Error(fmt.Errorf("item with name %s doesn't exist", name))
return
}
httpReq := itm.Request.ToHTTPRequest(vars)
if httpReq == nil {
helper.Error(errors.New("failed to build HTTP request"))
return
}
httpReq.URL.Host = tmplHost
httpReq.URL.Scheme = "http"
actual, err := makeRequest(t.Client, httpReq)
if err != nil {
helper.Error(err)
return
}
handler(helper, &itm.Request, &itm.Response[0], actual)
}()
if helper.HasErrors() {
errs = append(errs, helper.AnnotateErrors(collection.Info.Name, name)...)
}
}
if len(errs) > 0 {
return errs
}
return nil
}
func makeRequest(client *http.Client, req *http.Request) (*postman.Response, error) {
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
actual := &postman.Response{
Mode: "raw",
Raw: string(body),
Status: resp.StatusCode,
}
return actual, nil
}
////////// helper functions //////////
// AssertErrors loops through the collected errors and t.Error's each of them
func AssertErrors(t *testing.T, errs []error) {
for _, e := range errs {
t.Error(e)
}
}
///////// TestHelper //////////////
// TestHelper helps with running tests
type TestHelper struct {
Assert *assert.Assertions
t *testing.T
errors []error
}
// NewTestHelper creates a new test helper
func NewTestHelper(t *testing.T) *TestHelper {
helper := &TestHelper{
Assert: assert.New(t),
t: t,
errors: []error{},
}
return helper
}
// HasErrors returns true if the testhelper has errors
func (t *TestHelper) HasErrors() bool {
return len(t.errors) > 0
}
func (t *TestHelper) Error(err error) {
t.errors = append(t.errors, err)
}
// Log logs something
func (t *TestHelper) Log(msg string) {
t.t.Log(msg)
}
// AnnotateErrors adds collection and test names to errors held by t
func (t *TestHelper) AnnotateErrors(collectionName, testName string) []error {
errs := []error{}
for _, e := range t.errors {
wrapped := errors.Wrapf(e, "(collection %s, request %s)", collectionName, testName)
errs = append(errs, wrapped)
}
return errs
}