This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrequest.test.js
More file actions
160 lines (131 loc) · 4.93 KB
/
Copy pathrequest.test.js
File metadata and controls
160 lines (131 loc) · 4.93 KB
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
/* global describe, it, before, expect */
const Remit = require('../')
describe('Request', function () {
let remit
before(function () {
remit = Remit()
})
describe('#object', function () {
it('should be a function', function () {
expect(remit.request).to.be.a('function')
})
it('should expose "on" global function', function () {
expect(remit.endpoint.on).to.be.a('function')
})
})
describe('#return', function () {
let remit, request
before(function () {
remit = Remit()
request = remit.request('request-test')
})
it('should throw if no event given', function () {
expect(remit.request).to.throw('No/invalid event specified when creating a request')
})
it('should throw if given no event in options object', function () {
expect(remit.request.bind(remit, {})).to.throw('No/invalid event specified when creating a request')
})
it('should allow options to be set on first run', function () {
const req = remit.request({
event: 'tester-123',
timeout: 2000
})
expect(req._options).to.have.property('event', 'tester-123')
expect(req._options).to.have.property('timeout', 2000)
})
it('should return a Request', function () {
expect(request).to.be.an.instanceof(remit.request.Type)
})
it('should be runnable (#send)', function () {
expect(request).to.be.a('function')
})
it('should expose an "on" function', function () {
expect(request.on).to.be.a('function')
})
it('should expose a "fallback" function', function () {
expect(request.fallback).to.be.a('function')
})
it('should expose an "options" function', function () {
expect(request.options).to.be.a('function')
})
it('should expose a "ready" function', function () {
expect(request.ready).to.be.a('function')
})
it('should expose a "send" function', function () {
expect(request.send).to.be.a('function')
})
})
describe('#usage', function () {
let remit
before(function () {
remit = Remit({name: 'requestRemit'})
})
it('should assign new options over old ones', function () {
const request = remit.request('options-test')
expect(request._options).to.have.property('event', 'options-test')
request.options({event: 'options-queue'})
expect(request._options).to.have.property('event', 'options-queue')
})
it('should parse timestrings in a timeout option', function () {
const request = remit.request('options-timestring-test')
request.options({timeout: '30m'})
expect(request._options).to.have.property('timeout', 1800000)
request.options({timeout: '2s'})
expect(request._options).to.have.property('timeout', 2000)
})
it('should return \'ready\' promise when request is ready to be used', function () {
this.slow(200)
const request = remit.request('on-start')
const ret = request.ready()
expect(ret).to.be.a('promise')
return ret
})
it('should allow a fallback to be set', function () {
const request = remit.request('fallback-test')
expect(request).to.not.have.property('_fallback')
request.fallback(123)
expect(request).to.have.property('_fallback', 123)
})
it('should allow a falsy fallback to be set', function () {
const request = remit.request('fallback-test-falsy')
expect(request).to.not.have.property('_fallback')
request.fallback(null)
expect(request).to.have.property('_fallback', null)
})
it('should allow a fallback to be unset', function () {
const request = remit.request('fallback-test-unset')
expect(request).to.not.have.property('_fallback')
request.fallback('testfallback')
expect(request).to.have.property('_fallback', 'testfallback')
request.fallback()
expect(request).to.not.have.property('_fallback')
})
it('should throw if sent with invalid priority', async function () {
const request = remit.request('invalid-priority')
request.options({priority: 11})
try {
await request()
throw new Error('Should have failed with invalid priority')
} catch (e) {
expect(e.message).to.equal('Invalid priority "11" when making request')
}
})
it('should timeout in configurable times', async function () {
this.slow(2000)
const request = remit.request('timeout-test')
request.options({timeout: 1000})
await remit.endpoint('timeout-test')
.handler((e, cb) => setTimeout(cb, 3000))
.start()
try {
await request()
throw new Error('Request should not have succeeded')
} catch (e) {
expect(e.message).to.equal('Request timed out after no response for 1000ms')
}
})
it('should return fallback if timing out and fallback set')
it('should expire from queue after same time as timeout')
it('should send NULL if given unparsable data')
})
})