request tests uses setUp() and tearDown()
Showing
1 changed file
with
25 additions
and
19 deletions
1 | /*global casper*/ | 1 | /*global casper*/ |
2 | /*jshint strict:false*/ | 2 | /*jshint strict:false*/ |
3 | var currentRequest; | ||
4 | |||
5 | function onResourceRequested(request) { | ||
6 | currentRequest = request; | ||
7 | } | ||
8 | |||
3 | function testHeader(header) { | 9 | function testHeader(header) { |
4 | return header.name === 'Accept' && header.value === 'application/json'; | 10 | return header.name === 'Accept' && header.value === 'application/json'; |
5 | } | 11 | } |
6 | 12 | ||
7 | casper.test.begin('requests tests', 3, function(test) { | 13 | casper.test.begin('requests tests', 3, { |
8 | var current = 0, | 14 | setUp: function() { |
9 | tests = [ | 15 | casper.on('page.resource.requested', onResourceRequested); |
10 | function(request) { | ||
11 | test.assertNot(request.headers.some(testHeader), | ||
12 | "Casper.open() sets no custom header by default"); | ||
13 | }, | 16 | }, |
14 | function(request) { | 17 | |
15 | test.assert(request.headers.some(testHeader), | 18 | tearDown: function() { |
16 | "Casper.open() can set a custom header"); | 19 | currentRequest = undefined; |
20 | casper.removeListener('page.resource.requested', onResourceRequested); | ||
17 | }, | 21 | }, |
18 | function(request) { | ||
19 | test.assertNot(request.headers.some(testHeader), | ||
20 | "Casper.open() custom headers option is not persistent"); | ||
21 | } | ||
22 | ]; | ||
23 | 22 | ||
24 | casper.on('page.resource.requested', function(request) { | 23 | test: function(test) { |
25 | tests[current++](request); | 24 | casper.start('tests/site/index.html', function() { |
25 | test.assertNot(currentRequest.headers.some(testHeader), | ||
26 | "Casper.open() sets no custom header by default"); | ||
26 | }); | 27 | }); |
27 | 28 | ||
28 | casper.start('tests/site/index.html'); | ||
29 | |||
30 | casper.thenOpen('tests/site/index.html', { | 29 | casper.thenOpen('tests/site/index.html', { |
31 | headers: { | 30 | headers: { |
32 | Accept: 'application/json' | 31 | Accept: 'application/json' |
33 | } | 32 | } |
33 | }, function() { | ||
34 | test.assert(currentRequest.headers.some(testHeader), | ||
35 | "Casper.open() can set a custom header"); | ||
34 | }); | 36 | }); |
35 | 37 | ||
36 | casper.thenOpen('tests/site/index.html'); | 38 | casper.thenOpen('tests/site/index.html', function() { |
39 | test.assertNot(currentRequest.headers.some(testHeader), | ||
40 | "Casper.open() custom headers option is not persistent"); | ||
41 | }); | ||
37 | 42 | ||
38 | casper.run(function() { | 43 | casper.run(function() { |
39 | this.removeAllListeners('page.resource.requested'); | 44 | this.removeAllListeners('page.resource.requested'); |
40 | test.done(); | 45 | test.done(); |
41 | }); | 46 | }); |
47 | } | ||
42 | }); | 48 | }); | ... | ... |
-
Please register or sign in to post a comment