-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebrequest-on-before-send-headers.spec.js
169 lines (128 loc) · 3.42 KB
/
webrequest-on-before-send-headers.spec.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
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
/**
* Module dependencies.
*/
require('mocha-generators').install()
var Nightmare = require('nightmare')
var should = require('chai').should()
var url = require('url')
var path = require('path')
var qs = require('querystring')
var server
/**
* Temporary directory
*/
var tmp_dir = path.join(__dirname, 'tmp')
/**
* Get rid of a warning.
*/
process.setMaxListeners(0)
/**
* Locals.
*/
var base = 'http://localhost:7500/'
describe('onBeforeSendHeaders', function () {
var nightmare
before(function (done) {
require('../src/webrequest-on-before-send-headers')
require('../src/webrequest-on-send-headers')
server = require('./server').listen(7500, 'localhost', done)
})
beforeEach(function () {
nightmare = Nightmare()
})
it('should be accessable', function * () {
nightmare.onBeforeSendHeaders.should.be.ok
})
it('receives details object', function * () {
var result
yield nightmare
.goto(fixture('webrequest'))
.onBeforeSendHeaders(function (details, callback) {
callback({cancel: true})
})
.on('onBeforeSendHeaders', function (details) {
if (details) {
result = details
}
})
.evaluate(function () {
$.ajax({
url: 'http://localhost:7500/webrequest/',
type: 'GET'
})
})
.wait(500)
result.should.be.ok
;(typeof result.requestHeaders).should.equal('object')
})
it('can change the request headers', function * () {
var result
yield nightmare
.goto(fixture('webrequest'))
.onBeforeSendHeaders(function (details, callback) {
callback({cancel: false, requestHeaders: {Accept: '*/*;test/header'} })
})
.on('onSendHeaders', function (details) {
if (details) {
result = details
}
})
.onSendHeaders({urls: ['http://*/*', 'https://*/*']})
.evaluate(function () {
$.ajax({
url: 'http://localhost:7500/webrequest/',
type: 'GET'
})
})
.wait(500)
result.should.be.ok
;(typeof result.requestHeaders).should.equal('object')
;(JSON.stringify(result.requestHeaders)).should.equal(JSON.stringify({'Accept': '*/*;test/header'}))
})
it('resets the whole headers', function * () {
var result
yield nightmare
.goto(fixture('webrequest'))
.onBeforeSendHeaders(function (details, callback) {
var requestHeaders = {
Test: 'header'
}
callback({cancel: false, requestHeaders: requestHeaders})
})
.on('onSendHeaders', function (details) {
if (details) {
result = details
}
})
.onSendHeaders({})
.evaluate(function () {
$.ajax({
url: 'http://localhost:7500/webrequest/',
type: 'GET'
})
})
.wait(500)
result.should.be.ok
;(typeof result.requestHeaders).should.equal('object')
;(JSON.stringify(result.requestHeaders)).should.equal(JSON.stringify({Test: 'header'}))
})
afterEach(function * () {
nightmare.onBeforeSendHeaders(null)
nightmare.onSendHeaders(null)
yield nightmare.end()
})
after(function (done) {
delete nightmare.onBeforeSendHeaders
delete nightmare.onSendHeaders
server.close(done)
})
})
/**
* Generate a URL to a specific fixture.
*
* @param {String} path
* @returns {String}
*/
function fixture (path) {
return url.resolve(base, path) + '/'
}