This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.ts
99 lines (85 loc) · 2.29 KB
/
test.ts
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
import test from 'ava'
import {freddo, expr, exists} from '@meeshkanml/freddo'
import got from 'got'
import micro from 'micro'
import testListen from 'test-listen'
import imageType from 'image-type'
import m from '.'
const template = {
name: 'fake({{name.lastName}}, {{name.firstName}} {{name.suffix}})',
ssn: 'chance.ssn',
knownAddresses: {
street: 'address.streetAddress',
city: 'address.city',
zipCode: 'address.zipCode',
_repeat: 2
},
ipAddresses: 'internet.ip|3'
}
const invalidTemplate = {
name: 'thisFunctionDoesNotExist'
}
let url: string
const isHTML = (str: string): boolean => str.trim().startsWith('<!DOCTYPE html>')
const isImage = (input: Buffer | Uint8Array, type: string): boolean => {
const image = imageType(input)
if (!image) {
return false
}
return image.ext === type
}
test.before(async () => {
url = await testListen(micro(m))
})
test('valid POST request', async t => {
await freddo(url, {
method: 'POST',
body: template,
json: true,
responseType: 'json'
})
.status(200)
.header('content-type', 'application/json; charset=utf-8')
.body(exists, expr('.name'))
.body(exists, expr('.ssn'))
.body(exists, expr('.knownAddresses'))
.body(exists, expr('.knownAddresses[0].street'))
.body(exists, expr('.knownAddresses[0].city'))
.body(exists, expr('.knownAddresses[0].zipCode'))
.body(exists, expr('.knownAddresses[1].street'))
.body(exists, expr('.knownAddresses[1].city'))
.body(exists, expr('.knownAddresses[1].zipCode'))
.body(exists, expr('.ipAddresses'))
.body((ipAddresses: string[]) => ipAddresses.length === 3, expr('.ipAddresses'))
.ensure()
t.pass()
})
test('invalid POST body', async t => {
await t.throwsAsync(got(url, {
method: 'POST',
json: invalidTemplate,
responseType: 'json'
}), {
message: 'Response code 400 (Bad Request)'
})
})
test('valid GET request', async t => {
await freddo(url)
.status(200)
.body(isHTML)
.ensure()
await freddo(`${url}/demo.gif`)
.status(200)
.body((body: string) => isImage(Buffer.from(body, 'utf8'), 'gif'))
.ensure()
await freddo(`${url}/favicon.png`, {encoding: null})
.status(200)
.body((body: string) => isImage(Buffer.from(body), 'png'))
.ensure()
await freddo(`${url}/xyz`)
.status(200)
.header('content-length', 17)
.body('Use POST request.')
.ensure()
t.pass()
})