-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathuse.js
83 lines (72 loc) · 2.04 KB
/
use.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
var setup = require('./support/setup');
var uri = setup.uri;
var assert = require('assert');
var request = require('../');
describe('request', function(){
this.timeout(20000);
describe('use', function(){
it('should use plugin success', function(done){
var now = '' + Date.now();
function uuid(req){
req.set('X-UUID', now);
return req;
}
function prefix(req){
req.url = uri + req.url
return req;
}
request
.get('/echo')
.use(uuid)
.use(prefix)
.end(function(err, res){
assert.strictEqual(res.statusCode, 200);
assert.equal(res.get('X-UUID'), now);
done();
})
});
});
})
describe('subclass', function() {
var OriginalRequest;
beforeEach(function(){
OriginalRequest = request.Request;
});
afterEach(function(){
request.Request = OriginalRequest;
});
it('should be an instance of Request', function(){
var req = request.get('/');
assert(req instanceof request.Request);
});
it('should use patched subclass', function(){
assert(OriginalRequest);
var constructorCalled, sendCalled;
function NewRequest() {
constructorCalled = true;
OriginalRequest.apply(this, arguments);
}
NewRequest.prototype = Object.create(OriginalRequest.prototype);
NewRequest.prototype.send = function() {
sendCalled = true;
return this;
};
request.Request = NewRequest;
var req = request.get('/').send();
assert(constructorCalled);
assert(sendCalled);
assert(req instanceof NewRequest);
assert(req instanceof OriginalRequest);
});
it('should use patched subclass in agent too', function(){
if (!request.agent) return; // Node-only
function NewRequest() {
OriginalRequest.apply(this, arguments);
}
NewRequest.prototype = Object.create(OriginalRequest.prototype);
request.Request = NewRequest;
var req = request.agent().del('/');
assert(req instanceof NewRequest);
assert(req instanceof OriginalRequest);
});
})