-
-
Notifications
You must be signed in to change notification settings - Fork 17.2k
/
Copy pathapp.request.js
143 lines (109 loc) · 2.91 KB
/
app.request.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
'use strict'
var after = require('after')
var express = require('../')
, request = require('supertest');
describe('app', function(){
describe('.request', function(){
it('should extend the request prototype', function(done){
var app = express();
app.request.querystring = function(){
return require('node:url').parse(this.url).query;
};
app.use(function(req, res){
res.end(req.querystring());
});
request(app)
.get('/foo?name=tobi')
.expect('name=tobi', done);
})
it('should only extend for the referenced app', function (done) {
var app1 = express()
var app2 = express()
var cb = after(2, done)
app1.request.foobar = function () {
return 'tobi'
}
app1.get('/', function (req, res) {
res.send(req.foobar())
})
app2.get('/', function (req, res) {
res.send(req.foobar())
})
request(app1)
.get('/')
.expect(200, 'tobi', cb)
request(app2)
.get('/')
.expect(500, /(?:not a function|has no method)/, cb)
})
it('should inherit to sub apps', function (done) {
var app1 = express()
var app2 = express()
var cb = after(2, done)
app1.request.foobar = function () {
return 'tobi'
}
app1.use('/sub', app2)
app1.get('/', function (req, res) {
res.send(req.foobar())
})
app2.get('/', function (req, res) {
res.send(req.foobar())
})
request(app1)
.get('/')
.expect(200, 'tobi', cb)
request(app1)
.get('/sub')
.expect(200, 'tobi', cb)
})
it('should allow sub app to override', function (done) {
var app1 = express()
var app2 = express()
var cb = after(2, done)
app1.request.foobar = function () {
return 'tobi'
}
app2.request.foobar = function () {
return 'loki'
}
app1.use('/sub', app2)
app1.get('/', function (req, res) {
res.send(req.foobar())
})
app2.get('/', function (req, res) {
res.send(req.foobar())
})
request(app1)
.get('/')
.expect(200, 'tobi', cb)
request(app1)
.get('/sub')
.expect(200, 'loki', cb)
})
it('should not pollute parent app', function (done) {
var app1 = express()
var app2 = express()
var cb = after(2, done)
app1.request.foobar = function () {
return 'tobi'
}
app2.request.foobar = function () {
return 'loki'
}
app1.use('/sub', app2)
app1.get('/sub/foo', function (req, res) {
res.send(req.foobar())
})
app2.get('/', function (req, res) {
res.send(req.foobar())
})
request(app1)
.get('/sub')
.expect(200, 'loki', cb)
request(app1)
.get('/sub/foo')
.expect(200, 'tobi', cb)
})
})
})