-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
309 lines (285 loc) · 8.35 KB
/
api.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) 2020 Alexandru Catrina <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
const assert = require('assert')
const express = require('express')
const bodyParser = require('body-parser')
const request = require('supertest')
const { ContentBuilder, registerHttpCall } = require('../api')
describe('Test response for a known sample and a mockup request', () => {
it('should throw error if request or response are invalid', () => {
const input = {
request: null,
response: null
}
assert.throws(() => {
new ContentBuilder(input)
})
})
it('should merge the request parameters on each new call', () => {
const request = {
route: '/',
method: 'get',
payload: {
a: 1
}
}
const response = {
code: 201,
data: 'A={payload.a} B={payload.b}'
}
const incomingRequest = {
body: {
b: 2
}
}
const cb = new ContentBuilder({ request, response })
const { code, headers, content } = cb.generate(incomingRequest)
assert.strict.equal(code, 201)
assert.strict.deepEqual(headers, {})
assert.strict.equal(content, 'A=1 B=2')
})
it('should generate response according to meta', () => {
const request = {
route: '/:id',
method: 'get',
headers: {
authorization: 'Basic XXXXXXXX',
'content-type': 'application/json',
'x-friendly-name': 'samplest'
},
payload: {
title: 'The lord of the rings',
author: 'J. R. R. Tolkien',
ebook: true
}
}
const response = {
code: 200,
headers: {
'x-powered-by': 'samplest',
'x-feedback': '{headers.x-friendly-name}',
'content-type': 'application/json'
},
data: {
saved: true,
id: '{route.id}',
related: ['1', '2', '3', '4', '5']
},
$data: {
cast: {
'related.*': 'number',
id: 'number',
saved: 'boolean'
}
}
}
const cb = new ContentBuilder({ request, response })
const { code, headers, content } = cb.generate({
params: { id: 2020 }
})
assert.strict.equal(code, response.code)
assert.strict.deepEqual(headers, {
'x-powered-by': response.headers['x-powered-by'],
'x-feedback': request.headers['x-friendly-name'],
'content-type': request.headers['content-type']
})
assert.strict.deepEqual(content, {
saved: true,
id: 2020,
related: [1, 2, 3, 4, 5]
})
})
it('should follow an exception response if validations are not ok', () => {
const request = {
route: '/',
method: 'get',
payload: {
a: 1,
b: 2
}
}
const response = {
code: 200,
data: 'A={payload.a} B={payload.b} C={payload.c}'
}
const except = {
'Payload must contain three fields': {
validate: [
'Object.keys(payload).length === 3'
],
response: {
code: 400,
data: 'Must submit exactly 3 fields in payload'
}
}
}
const incomingRequest = {
body: {
b: 2
}
}
const cb = new ContentBuilder({ request, response, except })
const { flow, code, headers, content } = cb.generate(incomingRequest)
assert.strict.equal(code, 400)
assert.strict.equal(flow, 'Payload must contain three fields')
assert.strict.deepEqual(headers, { 'X-Assertion': 'Payload must contain three fields' })
assert.strict.equal(content, 'Must submit exactly 3 fields in payload')
})
it('should follow happy path if except validations are ok', () => {
const request = {
route: '/',
method: 'get',
payload: {
a: 1,
b: 2
}
}
const response = {
code: 200,
data: 'A={payload.a} B={payload.b} C={payload.c}'
}
const except = {
'Payload must contain three fields': {
validate: [
'Object.keys(payload).length === 3'
],
response: {
code: 400,
data: 'Must submit exactly 3 fields in payload'
}
}
}
const incomingRequest = {
body: {
b: 200,
c: 300
}
}
const cb = new ContentBuilder({ request, response, except })
const { flow, code, headers, content } = cb.generate(incomingRequest)
assert.strict.equal(code, 200)
assert.strict.equal(flow, null)
assert.strict.deepEqual(headers, {})
assert.strict.equal(content, 'A=1 B=200 C=300')
})
})
describe('Test an API instance with actual http calls', () => {
const cb = new ContentBuilder({
request: {
route: '/:username/post/:id',
method: 'post',
headers: {
'x-domain': 'samplest.localhost'
},
payload: {
bookTitle: 'The hobbit',
popularity: [0.778, 0.821]
}
},
response: {
code: 200,
headers: {
'x-domain-confirm': '{headers.x-domain} enabled'
},
data: 'added {payload.bookTitle} with popularity range {payload.popularity}'
},
except: {
'API token must be valid (if set)': {
validate: [
'headers["x-secret"] && Buffer.from(headers["x-secret"], "base64").toString("ascii") === "samplest"'
],
response: {
code: 403,
data: {
error: 'access denied',
params: {
username: '{route.username}',
postNumber: '{route.id}'
}
},
$data: {
cast: {
'params.postNumber': 'number'
}
}
}
},
'Username must match github': {
validate: [
'route.username === `lexndru`'
],
response: {
code: 400,
data: {
error: 'incorrect username'
}
}
}
}
})
it('should return 200 and an array of text messages', () => {
const api = express()
api.use(bodyParser.json())
registerHttpCall(cb, api)
request(api)
.post('/lexndru/post/2020')
.send({
bookTitle: 'The return of the king',
popularity: [0.8, 0.9]
})
.expect(200, JSON.stringify('added The return of the king with popularity range 0.8,0.9'))
.end(err => err ? assert.fail(err) : null)
})
it('should return 400 and a header with assertion message', () => {
const api = express()
api.use(bodyParser.json())
registerHttpCall(cb, api)
request(api)
.post('/alexandru/post/2020')
.send({
bookTitle: 'The return of the king',
popularity: [0.8, 0.9]
})
.expect('X-Assertion', 'Username must match github')
.expect(400, JSON.stringify({ error: 'incorrect username' }))
.end(err => err ? assert.fail(err) : null)
})
it('should return 403 and the payload with assertion message', () => {
const api = express()
api.use(bodyParser.json())
registerHttpCall(cb, api)
request(api)
.post('/lexndru/post/2020')
.set('x-secret', 'samplest is the secret')
.send({
bookTitle: 'The return of the king',
popularity: [0.8, 0.9]
})
.expect('X-Assertion', 'API token must be valid (if set)')
.expect(403, JSON.stringify(({
error: 'access denied',
params: {
username: 'lexndru',
postNumber: 2020
}
})))
.end(err => err ? assert.fail(err) : null)
})
})