forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
134 lines (116 loc) · 3.08 KB
/
api_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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
txdb "github.com/DATA-DOG/go-txdb"
"github.com/cucumber/godog"
)
func init() {
// we register an sql driver txdb
txdb.Register("txdb", "mysql", "root@/godog_test")
}
type apiFeature struct {
server
resp *httptest.ResponseRecorder
}
func (a *apiFeature) resetResponse(*godog.Scenario) {
a.resp = httptest.NewRecorder()
if a.db != nil {
a.db.Close()
}
db, err := sql.Open("txdb", "api")
if err != nil {
panic(err)
}
a.db = db
}
func (a *apiFeature) iSendrequestTo(method, endpoint string) (err error) {
req, err := http.NewRequest(method, endpoint, nil)
if err != nil {
return
}
// handle panic
defer func() {
switch t := recover().(type) {
case string:
err = fmt.Errorf(t)
case error:
err = t
}
}()
switch endpoint {
case "/users":
a.users(a.resp, req)
default:
err = fmt.Errorf("unknown endpoint: %s", endpoint)
}
return
}
func (a *apiFeature) theResponseCodeShouldBe(code int) error {
if code != a.resp.Code {
if a.resp.Code >= 400 {
return fmt.Errorf("expected response code to be: %d, but actual is: %d, response message: %s", code, a.resp.Code, string(a.resp.Body.Bytes()))
}
return fmt.Errorf("expected response code to be: %d, but actual is: %d", code, a.resp.Code)
}
return nil
}
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) (err error) {
var expected, actual interface{}
// re-encode expected response
if err = json.Unmarshal([]byte(body.Content), &expected); err != nil {
return
}
// re-encode actual response too
if err = json.Unmarshal(a.resp.Body.Bytes(), &actual); err != nil {
return
}
// the matching may be adapted per different requirements.
if !reflect.DeepEqual(expected, actual) {
return fmt.Errorf("expected JSON does not match actual, %v vs. %v", expected, actual)
}
return nil
}
func (a *apiFeature) thereAreUsers(users *godog.Table) error {
var fields []string
var marks []string
head := users.Rows[0].Cells
for _, cell := range head {
fields = append(fields, cell.Value)
marks = append(marks, "?")
}
stmt, err := a.db.Prepare("INSERT INTO users (" + strings.Join(fields, ", ") + ") VALUES(" + strings.Join(marks, ", ") + ")")
if err != nil {
return err
}
for i := 1; i < len(users.Rows); i++ {
var vals []interface{}
for n, cell := range users.Rows[i].Cells {
switch head[n].Value {
case "username":
vals = append(vals, cell.Value)
case "email":
vals = append(vals, cell.Value)
default:
return fmt.Errorf("unexpected column name: %s", head[n].Value)
}
}
if _, err = stmt.Exec(vals...); err != nil {
return err
}
}
return nil
}
func InitializeScenario(ctx *godog.ScenarioContext) {
api := &apiFeature{}
ctx.BeforeScenario(api.resetResponse)
ctx.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo)
ctx.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
ctx.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON)
ctx.Step(`^there are users:$`, api.thereAreUsers)
}