1
|
|
|
/* eslint-env mocha */ |
2
|
|
|
/* eslint-disable no-unused-expressions */ |
3
|
|
|
/* global transportFactory, basicFactory */ |
4
|
|
|
|
5
|
|
|
var Sinon = require('sinon') |
6
|
|
|
var Chai = require('chai') |
7
|
|
|
var expect = Chai.expect |
8
|
|
|
var SDK = require('../../../../lib') |
9
|
|
|
var Http = SDK.Http |
10
|
|
|
var Rest = Http.Rest |
11
|
|
|
var Method = Http.Method |
12
|
|
|
var Loggers = require('../../../../lib/logger') |
13
|
|
|
|
14
|
|
|
Chai.use(require('chai-as-promised')) |
15
|
|
|
|
16
|
|
|
function branchStopper () { |
17
|
|
|
Chai.assert(false, 'This branch should have never been executed') |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
var through = function (value) { |
21
|
|
|
return value |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
describe('Integration', function () { |
25
|
|
|
describe('/http', function () { |
26
|
|
|
describe('/rest.js', function () { |
27
|
|
|
var serializer |
28
|
|
|
var client |
29
|
|
|
|
30
|
|
|
var clientFactory = function (transport, opts) { |
31
|
|
|
opts = opts || {methodOverrideHeader: 'X-MOH'} |
32
|
|
|
opts.logger = {level: Loggers.Level.Trace} |
33
|
|
|
opts.serializer = opts.serializer || serializer |
34
|
|
|
return new Rest(opts, transport) |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
beforeEach(function () { |
38
|
|
|
serializer = { |
39
|
|
|
serialize: Sinon.spy(through), |
40
|
|
|
deserialize: Sinon.spy(through) |
41
|
|
|
} |
42
|
|
|
client = clientFactory(transportFactory({code: 200})) |
43
|
|
|
}) |
44
|
|
|
|
45
|
|
|
describe('.Client', function () { |
46
|
|
|
describe('< new', function () { |
47
|
|
|
it('understands transport-only construction', function () { |
48
|
|
|
var transport = transportFactory({code: 200}) |
49
|
|
|
var client = new Rest(transport) |
50
|
|
|
var resource = '/' |
51
|
|
|
return client |
52
|
|
|
.request(Method.Get, resource) |
53
|
|
|
.then(function () { |
54
|
|
|
expect(transport.callCount).to.eq(1) |
55
|
|
|
expect(transport.getCall(0).args[0]).to.eq(resource) |
56
|
|
|
}) |
57
|
|
|
}) |
58
|
|
|
|
59
|
|
|
it('tolerates invalid settings construction', function () { |
60
|
|
|
var transport = transportFactory({code: 200}) |
61
|
|
|
var client = new Rest(false, transport) |
62
|
|
|
var resource = '/' |
63
|
|
|
return client |
64
|
|
|
.request(Method.Get, resource) |
65
|
|
|
.then(function () { |
66
|
|
|
expect(transport.callCount).to.eq(1) |
67
|
|
|
expect(transport.getCall(0).args[0]).to.eq(resource) |
68
|
|
|
}) |
69
|
|
|
}) |
70
|
|
|
}) |
71
|
|
|
|
72
|
|
|
describe('#execute', function () { |
73
|
|
|
it('converts resource into url', function () { |
74
|
|
|
var basic = basicFactory({code: 200}) |
75
|
|
|
var client = clientFactory(null, {client: basic}) |
76
|
|
|
var resource = '/' |
77
|
|
|
|
78
|
|
|
return client |
79
|
|
|
.execute({method: Method.Get, resource: resource}) |
80
|
|
|
.then(function () { |
81
|
|
|
expect(basic.execute.callCount).to.eq(1) |
82
|
|
|
var request = basic.execute.getCall(0).args[0] |
83
|
|
|
expect(request.url).to.eq(resource) |
84
|
|
|
}) |
85
|
|
|
}) |
86
|
|
|
|
87
|
|
|
it('provides 0.2.0 compatibility for route attribute', function () { |
88
|
|
|
var basic = basicFactory({code: 200}) |
89
|
|
|
var client = clientFactory(null, {client: basic}) |
90
|
|
|
var route = '/' |
91
|
|
|
|
92
|
|
|
return client |
93
|
|
|
.execute({method: Method.Get, route: route}) |
94
|
|
|
.then(function () { |
95
|
|
|
expect(basic.execute.callCount).to.eq(1) |
96
|
|
|
var request = basic.execute.getCall(0).args[0] |
97
|
|
|
expect(request.url).to.eq(route) |
98
|
|
|
}) |
99
|
|
|
}) |
100
|
|
|
}) |
101
|
|
|
|
102
|
|
|
describe('#request', function () { |
103
|
|
|
it('serializes provided payload', function () { |
104
|
|
|
var data = {x: 12} |
105
|
|
|
return client |
106
|
|
|
.request(Method.Post, '/', data) |
107
|
|
|
.then(function () { |
108
|
|
|
expect(serializer.serialize.callCount).to.eq(1) |
109
|
|
|
expect(serializer.serialize.getCall(0).args[0]).to.eq(data) |
110
|
|
|
}) |
111
|
|
|
}) |
112
|
|
|
|
113
|
|
|
it('deserializes received response', function () { |
114
|
|
|
var data = '{"x": 12}' |
115
|
|
|
var basic = basicFactory({code: 200, payload: data}) |
116
|
|
|
var client = clientFactory(null, {client: basic}) |
117
|
|
|
return client |
118
|
|
|
.request(Method.Get, '/') |
119
|
|
|
.then(function () { |
120
|
|
|
expect(serializer.deserialize.callCount).to.eq(1) |
121
|
|
|
expect(serializer.deserialize.getCall(0).args[0]).to.eq(data) |
122
|
|
|
}) |
123
|
|
|
}) |
124
|
|
|
|
125
|
|
|
it('throws client error on 4xx by default', function () { |
126
|
|
|
var client = clientFactory(transportFactory({code: 400})) |
127
|
|
|
return client |
128
|
|
|
.request(Method.Get, '/') |
129
|
|
|
.then(branchStopper, function (e) { |
130
|
|
|
expect(e).to.be.instanceOf(Http.ClientErrorException) |
131
|
|
|
}) |
132
|
|
|
}) |
133
|
|
|
|
134
|
|
|
it('throws server error on 5xx by default', function () { |
135
|
|
|
var client = clientFactory(transportFactory({code: 500})) |
136
|
|
|
return client |
137
|
|
|
.request(Method.Get, '/') |
138
|
|
|
.then(branchStopper, function (e) { |
139
|
|
|
expect(e).to.be.instanceOf(Http.ServerErrorException) |
140
|
|
|
}) |
141
|
|
|
}) |
142
|
|
|
|
143
|
|
|
it('passes through query, headers and payload', function () { |
144
|
|
|
var basic = basicFactory({code: 200, payload: ''}) |
145
|
|
|
var client = clientFactory(null, {client: basic}) |
146
|
|
|
var payload = 'Some text' |
147
|
|
|
var query = {alpha: [1, 2]} |
148
|
|
|
var headers = {alpha: [1, 2]} |
149
|
|
|
|
150
|
|
|
return client |
151
|
|
|
.request(Method.Post, '/entity', payload, query, headers) |
152
|
|
|
.then(function () { |
153
|
|
|
expect(basic.execute.callCount).to.eq(1) |
154
|
|
|
var request = basic.execute.getCall(0).args[0] |
155
|
|
|
expect(request.query).to.deep.eq(query) |
156
|
|
|
expect(request.headers).to.deep.eq(headers) |
157
|
|
|
expect(request.payload).to.deep.eq(payload) |
158
|
|
|
}) |
159
|
|
|
}) |
160
|
|
|
}) |
161
|
|
|
|
162
|
|
|
describe('#exists', function () { |
163
|
|
|
it('returns true for code 200 response', function () { |
164
|
|
|
var transport = transportFactory({code: 200}) |
165
|
|
|
var client = clientFactory(transport) |
166
|
|
|
|
167
|
|
|
return expect(client.exists('/entity')).to.eventually.eq(true) |
168
|
|
|
}) |
169
|
|
|
|
170
|
|
|
it('returns true for code 201 response', function () { |
171
|
|
|
var transport = transportFactory({code: 201}) |
172
|
|
|
var client = clientFactory(transport) |
173
|
|
|
|
174
|
|
|
return expect(client.exists('/entity')).to.eventually.eq(true) |
175
|
|
|
}) |
176
|
|
|
|
177
|
|
|
it('returns false for code 404 response', function () { |
178
|
|
|
var transport = transportFactory({code: 404}) |
179
|
|
|
var client = clientFactory(transport) |
180
|
|
|
|
181
|
|
|
return expect(client.exists('/entity')).to.eventually.eq(false) |
182
|
|
|
}) |
183
|
|
|
}) |
184
|
|
|
|
185
|
|
|
describe('#get', function () { |
186
|
|
|
it('returns deserialized payload for code 200 response', function () { |
187
|
|
|
var payload = 'Some text' |
188
|
|
|
var transport = transportFactory({code: 200, text: payload}) |
189
|
|
|
var client = clientFactory(transport) |
190
|
|
|
|
191
|
|
|
return client |
192
|
|
|
.get('/entity') |
193
|
|
|
.then(function (data) { |
194
|
|
|
expect(data).to.eq(payload) |
195
|
|
|
expect(serializer.deserialize.callCount).to.eq(1) |
196
|
|
|
}) |
197
|
|
|
}) |
198
|
|
|
|
199
|
|
|
it('returns deserialized payload for code 201 call', function () { |
200
|
|
|
var payload = 'Some text' |
201
|
|
|
var transport = transportFactory({code: 201, text: payload}) |
202
|
|
|
var client = clientFactory(transport) |
203
|
|
|
|
204
|
|
|
return client |
205
|
|
|
.get('/entity') |
206
|
|
|
.then(function (data) { |
207
|
|
|
expect(data).to.eq(payload) |
208
|
|
|
expect(serializer.deserialize.callCount).to.eq(1) |
209
|
|
|
}) |
210
|
|
|
}) |
211
|
|
|
|
212
|
|
|
it('returns null for code 404 on .get() call', function () { |
213
|
|
|
var payload = 'Some text' |
214
|
|
|
var transport = transportFactory({code: 404, text: payload}) |
215
|
|
|
var client = clientFactory(transport) |
216
|
|
|
|
217
|
|
|
return expect(client.get('/entity')).to.eventually.be.null |
218
|
|
|
}) |
219
|
|
|
}) |
220
|
|
|
|
221
|
|
|
var unsafe = ['create', 'set', 'modify', 'delete'] |
222
|
|
|
|
223
|
|
|
unsafe.forEach(function (method) { |
224
|
|
|
describe('#' + method, function () { |
225
|
|
|
it('throws NotFoundException on 404 response', function () { |
226
|
|
|
var client = clientFactory(transportFactory({code: 404})) |
227
|
|
|
var ExceptionClass = Http.NotFoundException |
228
|
|
|
var call = client[method]('/entity') |
229
|
|
|
|
230
|
|
|
return expect(call).to.eventually.be.rejectedWith(ExceptionClass) |
231
|
|
|
}) |
232
|
|
|
|
233
|
|
|
it('throws ClientErrorException on 4xx responses', function () { |
234
|
|
|
var client = clientFactory(transportFactory({code: 400})) |
235
|
|
|
var ExceptionClass = Http.ClientErrorException |
236
|
|
|
var call = client[method]('/entity') |
237
|
|
|
|
238
|
|
|
return expect(call).to.eventually.be.rejectedWith(ExceptionClass) |
239
|
|
|
}) |
240
|
|
|
|
241
|
|
|
it('throws ServerErrorException on 5xx responses', function () { |
242
|
|
|
var client = clientFactory(transportFactory({code: 500})) |
243
|
|
|
var ExceptionClass = Http.ServerErrorException |
244
|
|
|
var call = client[method]('/entity') |
245
|
|
|
|
246
|
|
|
return expect(call).to.eventually.be.rejectedWith(ExceptionClass) |
247
|
|
|
}) |
248
|
|
|
|
249
|
|
|
it('returns passed data on 200 response', function () { |
250
|
|
|
var payload = 'Some text' |
251
|
|
|
var client = clientFactory(transportFactory({ |
252
|
|
|
code: 200, |
253
|
|
|
text: payload |
254
|
|
|
})) |
255
|
|
|
|
256
|
|
|
return expect(client[method]('/entity')).to.eventually.eq(payload) |
257
|
|
|
}) |
258
|
|
|
|
259
|
|
|
it('returns passed data on 201 response', function () { |
260
|
|
|
var payload = 'Some text' |
261
|
|
|
var client = clientFactory(transportFactory({ |
262
|
|
|
code: 201, |
263
|
|
|
text: payload |
264
|
|
|
})) |
265
|
|
|
|
266
|
|
|
return expect(client[method]('/entity')).to.eventually.eq(payload) |
267
|
|
|
}) |
268
|
|
|
}) |
269
|
|
|
}) |
270
|
|
|
}) |
271
|
|
|
}) |
272
|
|
|
}) |
273
|
|
|
}) |
274
|
|
|
|