-
Notifications
You must be signed in to change notification settings - Fork 3
/
webrequest-on-before-redirect.spec.js
104 lines (79 loc) · 2.09 KB
/
webrequest-on-before-redirect.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
/**
* 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('onBeforeRedirect', function () {
var nightmare
before(function (done) {
require('../src/webrequest-on-before-request')
require('../src/webrequest-on-before-redirect')
server = require('./server').listen(7500, 'localhost', done)
})
beforeEach(function () {
nightmare = Nightmare()
})
it('should be accessable', function * () {
nightmare.onBeforeRequest.should.be.ok
nightmare.onBeforeRedirect.should.be.ok
})
it('receives details object', function * () {
var result
yield nightmare
.onBeforeRequest(function (details, callback) {
var defaultURL = 'http://localhost:7500/webrequest/'
if (details.url === defaultURL) {
callback({cancel: false, redirectURL: 'http://localhost:7500/redirect'})
} else {
callback({})
}
})
.onBeforeRedirect({urls: ['http://*/*', 'https://*/*']})
.on('onBeforeRedirect', function (details) {
if (details && !result) {
result = details
}
})
.goto(fixture('webrequest'))
result.should.be.ok
;(typeof result.fromCache).should.equal('boolean')
result.statusLine.should.equal('HTTP/1.1 307 Internal Redirect')
result.statusCode.should.equal(307)
})
afterEach(function * () {
nightmare.onBeforeRequest(null)
nightmare.onBeforeRedirect(null)
yield nightmare.end()
})
after(function (done) {
delete nightmare.onBeforeRequest
delete nightmare.onBeforeRedirect
server.close(done)
})
})
/**
* Generate a URL to a specific fixture.
*
* @param {String} path
* @returns {String}
*/
function fixture (path) {
return url.resolve(base, path) + '/'
}