request tests uses setUp() and tearDown()
Showing
1 changed file
with
36 additions
and
30 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) { | 16 | }, |
11 | test.assertNot(request.headers.some(testHeader), | ||
12 | "Casper.open() sets no custom header by default"); | ||
13 | }, | ||
14 | function(request) { | ||
15 | test.assert(request.headers.some(testHeader), | ||
16 | "Casper.open() can set a custom header"); | ||
17 | }, | ||
18 | function(request) { | ||
19 | test.assertNot(request.headers.some(testHeader), | ||
20 | "Casper.open() custom headers option is not persistent"); | ||
21 | } | ||
22 | ]; | ||
23 | 17 | ||
24 | casper.on('page.resource.requested', function(request) { | 18 | tearDown: function() { |
25 | tests[current++](request); | 19 | currentRequest = undefined; |
26 | }); | 20 | casper.removeListener('page.resource.requested', onResourceRequested); |
21 | }, | ||
27 | 22 | ||
28 | casper.start('tests/site/index.html'); | 23 | test: function(test) { |
24 | casper.start('tests/site/index.html', function() { | ||
25 | test.assertNot(currentRequest.headers.some(testHeader), | ||
26 | "Casper.open() sets no custom header by default"); | ||
27 | }); | ||
29 | 28 | ||
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 | } |
34 | }); | 33 | }, function() { |
34 | test.assert(currentRequest.headers.some(testHeader), | ||
35 | "Casper.open() can set a custom header"); | ||
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