forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.spec.js
More file actions
295 lines (259 loc) · 11.5 KB
/
server.spec.js
File metadata and controls
295 lines (259 loc) · 11.5 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'use strict'
/**
* adonis-framework
* Copyright(c) 2015-2016 Harminder Virk
* MIT Licensed
*/
const Server = require('../../src/Server')
const Request = require('../../src/Request')
const ResponseBuilder = require('../../src/Response')
const Static = require('../../src/Static')
const Route = require('../../src/Route')
const Middleware = require('../../src/Middleware')
const chai = require('chai')
const Ioc = require('adonis-fold').Ioc
const supertest = require('co-supertest')
const expect = chai.expect
const http = require('http')
const EventProvider = require('../../src/Event')
const path = require('path')
const stderr = require('test-console').stderr
class Session {
}
const Config = {
get: function (key) {
switch (key) {
case 'app.static':
return {}
case 'event':
return {
wildcard: true,
delimiter: ':'
}
case 'app.http.allowMethodSpoofing':
return true
default:
return 2
}
}
}
const Helpers = {
publicPath: function () {
return path.join(__dirname, './public')
},
makeNameSpace: function (base, toPath) {
return `App/${base}/${toPath}`
}
}
const Event = new EventProvider(Config)
require('co-mocha')
describe('Server', function () {
before(function () {
Ioc.autoload('App', path.join(__dirname, './app'))
const staticServer = new Static(Helpers, Config)
const Response = new ResponseBuilder({}, {}, Config)
this.server = new Server(Request, Response, Route, Helpers, Middleware, staticServer, Session, Config, Event)
})
beforeEach(function () {
Route.new()
Middleware.new()
})
it('should serve static resource from a given directory', function * () {
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/style.css').expect('Content-type', /css/).expect(200).end()
expect(res.text).to.match(/(?:\s*\S+\s*{[^}]*})+/g)
})
it('should serve favicon when request is for favicon', function * () {
const testServer = http.createServer(this.server.handle.bind(this.server))
yield supertest(testServer).get('/favicon.ico').expect('Content-type', /x-icon/).expect(200).end()
})
it('should make 404 error when unable to find static resource', function * () {
const testServer = http.createServer(this.server.handle.bind(this.server))
yield supertest(testServer).get('/foo.css').expect(404).end()
})
it('should not serve static resources with route is not GET or HEAD', function * () {
const testServer = http.createServer(this.server.handle.bind(this.server))
yield supertest(testServer).post('/style.css').expect(404).end()
})
it('should serve static resource even if route is defined', function * () {
Route.get('/favicon.ico', function * (request, response) {
response.send({rendered: true})
})
const testServer = http.createServer(this.server.handle.bind(this.server))
yield supertest(testServer).get('/favicon.ico').expect('Content-type', /x-icon/).expect(200).end()
})
it('should call route action if defined', function * () {
Route.get('/', function * (request, response) {
response.send({rendered: true})
})
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(200).end()
expect(res.body.rendered).to.equal(true)
})
it('should invoke route for verb defined using _method', function * () {
Route.put('/', function * (request, response) {
response.send({rendered: true})
})
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/?_method=PUT').expect(200).end()
expect(res.body.rendered).to.equal(true)
})
it('should not spoof request method when allowMethodSpoofing is not turned on', function * () {
Route.put('/', function * (request, response) {
response.send({rendered: true})
})
const staticServer = new Static(Helpers, Config)
const Response = new ResponseBuilder({}, {}, Config)
const customConfig = {
get: function (key) {
if (key === 'app.http.allowMethodSpoofing') {
return false
}
return Config.get(key)
}
}
const server = new Server(Request, Response, Route, Helpers, Middleware, staticServer, Session, customConfig, Event)
const testServer = http.createServer(server.handle.bind(server))
yield supertest(testServer).get('/?_method=PUT').expect(404).end()
})
it('should not log warning when allowMethodSpoofing is not turned on but trying to spoof method', function * () {
Route.put('/', function * (request, response) {
response.send({rendered: true})
})
const inspect = stderr.inspect()
const staticServer = new Static(Helpers, Config)
const Response = new ResponseBuilder({}, {}, Config)
const customConfig = {
get: function (key) {
if (key === 'app.http.allowMethodSpoofing') {
return false
}
return Config.get(key)
}
}
const server = new Server(Request, Response, Route, Helpers, Middleware, staticServer, Session, customConfig, Event)
const testServer = http.createServer(server.handle.bind(server))
yield supertest(testServer).get('/?_method=PUT').expect(404).end()
inspect.restore()
expect(inspect.output.join('')).to.match(/You are making use of method spoofing/)
})
it('should call route action via controller method', function * () {
Route.get('/', 'HomeController.index')
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(200).end()
expect(res.body.rendered).to.equal(true)
})
it('should return error when route handler is not of a valid type', function * () {
Route.get('/', {})
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(500).end()
expect(res.error.text).to.match(/InvalidArgumentException: E_INVALID_IOC_BINDING: Handler must point to a valid namespace or a closure/)
})
it('should return error when unable to find controller', function * () {
Route.get('/', 'FooController.index')
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(500).end()
expect(res.error.text).to.match(/Cannot find module/)
})
it('should return error when unable to find controller method', function * () {
Route.get('/', 'HomeController.foo')
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(500).end()
expect(res.error.text).to.match(/foo does not exists/)
})
it('should call all global middleware before reaching the route handler', function * () {
Middleware.global(['App/Http/Middleware/Global'])
Route.get('/', 'UserController.index')
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(200).end()
expect(res.text).to.equal('2')
})
it('should catch errors created by global middleware', function * () {
Middleware.global(['App/Http/Middleware/GlobalCatch'])
Route.get('/', 'UserController.index')
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(401).end()
expect(res.error.text).to.equal('Login')
})
it('should catch errors thrown by global middleware', function * () {
Middleware.global(['App/Http/Middleware/GlobalThrow'])
Route.get('/', 'UserController.index')
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(401).end()
expect(res.error.text).to.match(/Error: Login/)
})
it('should return error when unable to resolve middleware', function * () {
Middleware.register('auth', ['App/Auth'])
Route.get('/', 'HomeController.index').middlewares(['auth'])
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(500).end()
expect(res.error.text).to.match(/Cannot find module/)
})
it('should return error when unable to find handle method on middleware', function * () {
Middleware.register('auth', ['App/Http/Middleware/NoHandle'])
Route.get('/', 'HomeController.index').middlewares(['auth'])
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(500).end()
expect(res.error.text).to.match(/handle does not exists/)
})
it('should handle call middleware attached to a route', function * () {
Middleware.register('parser', 'App/Http/Middleware/Parser')
Middleware.register('cycle', 'App/Http/Middleware/Cycle2')
Route.get('/', 'UserController.index').middlewares(['parser', 'cycle'])
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(200).end()
expect(res.text).to.equal('1')
})
it('should call middlewares attached on route with closure as handler', function * () {
Middleware.register('parser', 'App/Http/Middleware/Parser')
Middleware.register('cycle', 'App/Http/Middleware/Cycle2')
Route.get('/', function * (request, response) {
response.send(request.count)
}).middlewares(['parser', 'cycle'])
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(200).end()
expect(res.text).to.equal('1')
})
it('should report error thrown my route closure', function * () {
Route.get('/', function * () {
throw new Error('Unable to login')
})
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(500).end()
expect(res.error.text).to.match(/Unable to login/)
})
it('should show default error stack when error itself does not have any message', function * () {
Route.get('/', function * () {
throw new Error()
})
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(500).end()
expect(res.error.text).to.match(/Error/)
})
it('should emit error event when there are listeners attach to error', function * () {
Event.when('Http:error:*', function (error, request, response) {
response.status(401).send(error.message)
})
Route.get('/', function * () {
throw new Error('Forbidden')
})
const testServer = http.createServer(this.server.handle.bind(this.server))
const res = yield supertest(testServer).get('/').expect(401).end()
expect(res.error.text).to.equal('Forbidden')
})
it('should server instance is null', function * () {
expect(this.server.httpInstance).to.be.null
})
it('should server instance is http.Server', function * () {
const httpServer = this.server.getInstance()
expect(httpServer).to.be.instanceOf(http.Server)
expect(this.server.httpInstance).to.be.instanceOf(http.Server)
})
it('should listen to server on a given port and host using listen method', function * () {
Route.get('/', 'HomeController.index')
this.server.listen('0.0.0.0', 8000)
const testServer = supertest.agent('http://127.0.0.1:8000')
const res = yield testServer.get('/').expect(200).end()
expect(res.body).deep.equal({rendered: true})
})
})