-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
142 lines (116 loc) · 3.56 KB
/
test.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
const test = require('ava')
const listen = require('test-listen')
const {array, object, number, string} = require('superstruct')
const microSuperstruct = require('.')
const micro = require('micro')
const got = require('got')
const Article = object({
id: number(),
title: string(),
tags: array(string()),
author: object({
id: number()
})
})
const Name = object({
first: string()
})
const handler = async (request, response) => {
const body = await micro.json(request)
micro.send(response, 200, body)
}
const handlerFrom = prop => async (request, response) => {
micro.send(response, 200, request[prop])
}
test('response on valid body', async t => {
const validate = microSuperstruct(Article)
const router = micro(validate(handler))
const url = await listen(router)
const data = {
id: 4,
title: 'Unicorn Article',
tags: ['some_tag'],
author: {
id: 1
}
}
const response = await got.post(url, {json: data}).json()
t.deepEqual(response, data)
})
test('populates `body` property on request', async t => {
const validate = microSuperstruct(Article)
const router = micro(validate(handlerFrom('body')))
const url = await listen(router)
const data = {
id: 4,
title: 'Unicorn Article',
tags: ['some_tag'],
author: {
id: 1
}
}
const response = await got.post(url, {json: data}).json()
t.deepEqual(response, data)
})
test('response on valid query string', async t => {
const validate = microSuperstruct({query: Name})
const router = micro(validate(handler))
const url = await listen(router)
const data = {hello: 'world'}
const response = await got.post(`${url}?first=Brandon`, {json: data}).json()
t.deepEqual(response, data)
})
test('populates `query` property on request', async t => {
const validate = microSuperstruct({query: Name})
const router = micro(validate(handlerFrom('query')))
const url = await listen(router)
const data = {hello: 'world'}
const response = await got.post(`${url}?first=Brandon`, {json: data}).json()
t.deepEqual(response, {first: 'Brandon'})
})
test('response on valid body & query string', async t => {
const validate = microSuperstruct({body: Article, query: Name})
const router = micro(validate(handler))
const url = await listen(router)
const data = {
id: 4,
title: 'Unicorn Article',
tags: ['some_tag'],
author: {
id: 1
}
}
const response = await got.post(`${url}?first=Brandon`, {json: data}).json()
t.deepEqual(response, data)
})
test('error on invalid body', async t => {
const validate = microSuperstruct({body: Article})
const router = micro(validate(handler))
const url = await listen(router)
const data = {
title: 'Unicorn Article',
tags: ['some_tag'],
author: {
id: 1
}
}
const {response} = await t.throwsAsync(got.post(url, {json: data}).json())
t.is(response.statusCode, 400)
t.is(response.body, 'Expected a value of type `number` for `id` but received `undefined`.')
})
test('error on invalid query string', async t => {
const validate = microSuperstruct({query: Name})
const router = micro(validate(handler))
const url = await listen(router)
const {response} = await t.throwsAsync(got.post(url, {json: {hello: 'world'}}).json())
t.is(response.statusCode, 400)
t.is(response.body, 'Expected a value of type `string` for `first` but received `undefined`.')
})
test('error on invalid input', async t => {
const validate = microSuperstruct('unicorns')
const router = micro(validate(handler))
const url = await listen(router)
const {response} = await t.throwsAsync(got.post(url).json())
t.is(response.statusCode, 500)
t.true(response.body.startsWith('micro-superstruct'))
})