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 0
/
index.js
176 lines (142 loc) · 3.58 KB
/
index.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
'use strict'
/**
* Module dependencies.
*/
const assert = require('assert').strict
const got = require('got')
const JSPath = require('jspath')
/**
* Initializes `Test`.
*
* @method freddo
* @api public
*/
function Test(url, options) {
this.data = {
url,
options,
response: {},
error: null
}
this.promise = Promise.resolve(null)
this.request()
}
const freddo = (url, options = null) => new Test(url, options)
Test.prototype.extend = function (promise, that) {
for (const key in that) {
promise[key] = that[key]
}
}
Test.prototype.next = function (what) {
this.promise = this.promise.then(what)
this.extend(this.promise, this)
return this.promise
}
Test.prototype.request = function () {
return this.next(async () => {
let response
if (this.data.options === undefined) {
response = await got(this.data.url)
} else {
response = await got(this.data.url, this.data.options)
}
this.data.response = response
return true
})
}
Test.prototype.verify = async function (key, expected, isHeader) {
let check = expected
let value
let location
if (key instanceof Expression) {
if (typeof this.data.response.body !== 'string') {
this.data.response.body = JSON.stringify(this.data.response.body)
}
value = key.apply(JSON.parse(this.data.response.body))
location = `expression ${JSON.stringify(key.expression)}`
} else {
if (isHeader) {
value = this.data.response.headers[key]
} else {
value = this.data.response[key]
}
if (typeof value === 'undefined') {
throw new TypeError(`Key ${JSON.stringify(key)} does not exist`)
}
location = `key ${JSON.stringify(key)}`
}
if (typeof expected !== 'function') {
check = (actual, location) => {
try {
assert.deepStrictEqual(actual, expected)
} catch (_) {
return {
result: false,
error: `Expected ${location} to be ${JSON.stringify(expected)}, but got ${JSON.stringify(actual)}`
}
}
return true
}
}
let result = check(value, location)
let error = `Custom assertion failed: ${check}`
if (typeof result !== 'boolean') {
if (typeof result.result === 'undefined') {
throw new TypeError('Custom assertion functions must return a boolean or a {result, error} object')
}
if (typeof result.error !== 'undefined') {
error = result.error
}
result = result.result
}
if (!result) {
this.data.error = error
}
return result
}
Test.prototype.expect = function (key, expected, isHeader = false) {
return this.next(prev => prev && this.verify(key, expected, isHeader))
}
Test.prototype.status = function (expected) {
return this.expect('statusCode', expected)
}
Test.prototype.header = function (key, expected) {
return this.expect(key, String(expected), true)
}
Test.prototype.body = function (expected, expression = null) {
if (expression === null) {
return this.expect('body', expected)
}
return this.expect(expression, expected)
}
Test.prototype.redirectsTo = function (url) {
return this.status(code => [301, 302, 303, 307, 308].includes(code))
.header('location', url)
}
Test.prototype.ensure = async function () {
if (!(await this)) {
throw new Error(this.data.error)
}
}
const exists = (actual, location) => {
if (actual.length !== 0) {
return true
}
return {
result: false,
error: `Expected ${location} to contain a value, but it does not exist`
}
}
class Expression {
constructor(expression) {
this.expression = expression
}
apply(haystack) {
return JSPath.apply(this.expression, haystack)
}
}
const expr = expression => new Expression(expression)
/**
* Expose `freddo`, `expr`, and `exists`.
*/
module.exports = {freddo, expr, exists}