-
Notifications
You must be signed in to change notification settings - Fork 0
/
features_test.go
154 lines (124 loc) · 3.24 KB
/
features_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 main_test
import (
"bytes"
"context"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"strings"
"github.com/cucumber/godog"
"github.com/gin-gonic/gin"
"github.com/itsubaki/quasar/handler"
"github.com/jfilipczyk/gomatch"
)
var api = &apiFeature{}
type apiFeature struct {
header http.Header
body io.Reader
resp *httptest.ResponseRecorder
server *gin.Engine
keep map[string]interface{}
close []func() error
}
func (a *apiFeature) start() {
a.server = handler.New()
a.keep = make(map[string]interface{})
}
func (a *apiFeature) reset(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
a.header = make(http.Header)
a.body = nil
a.resp = httptest.NewRecorder()
return ctx, nil
}
func (a *apiFeature) replace(str string) string {
for k, v := range a.keep {
switch val := v.(type) {
case string:
str = strings.Replace(str, k, val, -1)
}
}
return str
}
func (a *apiFeature) Request(method, endpoint string) error {
r := a.replace(endpoint)
req := httptest.NewRequest(method, r, a.body)
req.Header = a.header
a.server.ServeHTTP(a.resp, req)
return nil
}
func (a *apiFeature) ResponseCodeShouldBe(code int) error {
if code == a.resp.Code {
return nil
}
return fmt.Errorf("got=%v, want=%v", a.resp.Code, code)
}
func (a *apiFeature) ResponseShouldMatchJSON(body *godog.DocString) error {
want := a.replace(body.Content)
got := a.resp.Body.String()
ok, err := gomatch.NewDefaultJSONMatcher().Match(want, got)
if err != nil {
return fmt.Errorf("got=%v, want=%v, match: %v", got, want, err)
}
if !ok {
return fmt.Errorf("got=%v, want=%v", got, want)
}
return nil
}
func (a *apiFeature) SetHeader(k, v string) error {
a.header.Add(k, v)
return nil
}
func (a *apiFeature) SetRequestBody(body *godog.DocString) error {
r := a.replace(body.Content)
a.body = bytes.NewBuffer([]byte(r))
return nil
}
func (a *apiFeature) SetUploadFile(path string) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("file open: %v", err)
}
defer file.Close()
body := &bytes.Buffer{}
mw := multipart.NewWriter(body)
fw, err := mw.CreateFormFile("file", file.Name())
if err != nil {
return fmt.Errorf("create form file: %v", err)
}
if _, err := io.Copy(fw, file); err != nil {
return fmt.Errorf("io copy: %v", err)
}
a.body = body
a.header.Add("Content-Type", mw.FormDataContentType())
if err := mw.Close(); err != nil {
return fmt.Errorf("multipart writer close: %v", err)
}
return nil
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
gin.SetMode(gin.ReleaseMode)
api.close = []func() error{}
api.start()
})
ctx.AfterSuite(func() {
for _, c := range api.close {
if err := c(); err != nil {
log.Printf("defer: %v", err)
}
}
})
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Before(api.reset)
ctx.Step(`^I set "([^"]*)" header with "([^"]*)"$`, api.SetHeader)
ctx.Step(`^I set request body:$`, api.SetRequestBody)
ctx.Step(`^I set upload file "([^"]*)"$`, api.SetUploadFile)
ctx.Step(`^I send "([^"]*)" request to "([^"]*)"$`, api.Request)
ctx.Step(`^the response code should be (\d+)$`, api.ResponseCodeShouldBe)
ctx.Step(`^the response should match json:$`, api.ResponseShouldMatchJSON)
}