-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnector_test.go
149 lines (124 loc) · 3.62 KB
/
connector_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
package jac
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi"
"github.com/stretchr/testify/assert"
"gitlab.com/distributed_lab/ape"
"gitlab.com/distributed_lab/ape/problems"
)
// -- Test API --
type (
// GET models
getTestResponse struct {
Foo string `json:"foo"`
}
// POST models
postTestRequestBody struct {
X int64 `json:"x"`
Y int64 `json:"y"`
}
postTestResponse struct {
Z int64 `json:"z"`
}
// PATCH models
patchTestRequestBody struct {
Foo string `json:"foo"`
}
patchTestResponse struct {
Bar string `json:"bar"`
}
)
func newTestRouter() chi.Router {
r := chi.NewRouter()
r.Route("/integrations", func(r chi.Router) {
r.Route("/some-logic", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ape.Render(w, getTestResponse{Foo: "bar"})
})
r.Post("/add", func(w http.ResponseWriter, r *http.Request) {
var request postTestRequestBody
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
ape.RenderErr(w, problems.BadRequest(err)...)
}
ape.Render(w, postTestResponse{Z: request.X + request.Y})
})
r.Post("/multiply", func(w http.ResponseWriter, r *http.Request) {
var request postTestRequestBody
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
ape.RenderErr(w, problems.BadRequest(err)...)
}
ape.Render(w, postTestResponse{Z: request.X * request.Y})
})
r.Patch("/", func(w http.ResponseWriter, r *http.Request) {
var request patchTestRequestBody
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
ape.RenderErr(w, problems.BadRequest(err)...)
}
ape.Render(w, patchTestResponse{Bar: request.Foo})
})
})
})
return r
}
// -- Actual tests --
const (
testUrlBase = "/integrations/some-logic"
)
var testRouter = newTestRouter()
func getTestJac(server *httptest.Server) Jac {
var (
testJac = NewJac(server.URL)
)
return testJac
}
func TestJacer_Get(t *testing.T) {
testServer := httptest.NewServer(testRouter)
defer testServer.Close()
testJac := getTestJac(testServer)
t.Run("get request", func(t *testing.T) {
var testResponse getTestResponse
_, err := testJac.Get(RequestParams{Endpoint: testUrlBase}, &testResponse)
assert.Nil(t, err, "expected nil error when unmarshalling response")
assert.Equal(t, getTestResponse{Foo: "bar"}, testResponse)
})
}
func TestJacer_Post(t *testing.T) {
testServer := httptest.NewServer(testRouter)
defer testServer.Close()
var (
testJac = getTestJac(testServer)
testRequest = postTestRequestBody{X: 10, Y: 50}
testAddExpectedResponse = postTestResponse{Z: 60}
testMultiplyExpectedResponse = postTestResponse{Z: 500}
)
requestAsBytes, err := json.Marshal(testRequest)
assert.Nil(t, err, "expected nil error when marshalling a request body")
t.Run("post add request", func(t *testing.T) {
var testAddResponse postTestResponse
_, err = testJac.Post(
RequestParams{
Endpoint: fmt.Sprintf("%s/%s", testUrlBase, "add"),
Body: requestAsBytes,
},
&testAddResponse,
)
assert.Nil(t, err, "expected nil error when unmarshalling add response")
assert.Equal(t, testAddResponse, testAddExpectedResponse)
})
t.Run("post multiply request", func(t *testing.T) {
var testMultiplyResponse postTestResponse
_, err = testJac.Post(
RequestParams{
Endpoint: fmt.Sprintf("%s/%s", testUrlBase, "multiply"),
Body: requestAsBytes,
},
&testMultiplyResponse,
)
assert.Nil(t, err, "expected nil error when unmarshalling multiply response")
assert.Equal(t, testMultiplyResponse, testMultiplyExpectedResponse)
})
}