request.coffee
3.35 KB
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
#global casper
#eslint strict:0
utils = require "utils"
if phantom.casperEngine == 'slimerjs'
casper.test.skip(6, 'request.abort() test is broken for slimerjs')
casper.test.done()
else
SERVER = 'http://localhost:54321/'
ORIGINAL_URL = "tests/site/index.html"
CHANGED_URL = "tests/site/index.html?foo=bar"
setToTrueOnResourceRequested = false
setToTrueOnResourceReceived = false
requestURLRequested = ''
requestURLReceived = ''
onResourceRequested = (casper, requestData, request) ->
if requestData.url == (SERVER + ORIGINAL_URL)
setToTrueOnResourceRequested = true
requestURLRequested = requestData.url
onResourceRequestedWithAbort = (casper, requestData, request) ->
if requestData.url == (SERVER + ORIGINAL_URL)
request.abort()
onResourceRequestedWithChangeURL = (casper, requestData, request) ->
if requestData.url == (SERVER + ORIGINAL_URL)
request.changeUrl(SERVER + CHANGED_URL)
onResourceReceived = (casper, response) ->
if response.url == (SERVER + ORIGINAL_URL)
setToTrueOnResourceReceived = true
requestURLReceived = response.url
onResourceReceivedWithChangeURL = (casper, response) ->
if response.url == (SERVER + CHANGED_URL)
requestURLReceived = response.url
setUp = (test) ->
casper.options.onResourceRequested = onResourceRequested
casper.options.onResourceReceived = onResourceReceived
casper.start()
setUpWithAbort = (test) ->
casper.options.onResourceRequested = onResourceRequestedWithAbort
casper.options.onResourceReceived = onResourceReceived
casper.start()
setUpWithChangeURL = (test) ->
casper.options.onResourceRequested = onResourceRequestedWithChangeURL
casper.options.onResourceReceived = onResourceReceivedWithChangeURL
casper.start()
tearDown = (test) ->
setToTrueOnResourceRequested = false
setToTrueOnResourceReceived = false
casper.options.onResourceRequested = null
casper.options.onResourceReceived = null
casper.test.begin "onResourceRequested tests without abort/override", 4,
setUp: setUp
tearDown: tearDown
test: (test) ->
casper.open(ORIGINAL_URL).then ->
casper.wait 200, ->
test.assertEquals setToTrueOnResourceRequested, true, "Casper.options.onResourceRequested called successfully"
test.assertEquals requestURLRequested, SERVER+ORIGINAL_URL, "request url successfully recorded"
test.assertEquals setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived called successfully"
test.assertEquals requestURLReceived, SERVER+ORIGINAL_URL, "response url successfully recorded"
casper.run ->
test.done()
casper.test.begin "onResourceRequested tests with request.abort()", 1,
setUp: setUpWithAbort
tearDown: tearDown
test: (test) ->
casper.open(ORIGINAL_URL).then ->
casper.wait 200, ->
test.assertNotEquals setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived correctly never called"
casper.run ->
test.done()
casper.test.begin "onResourceRequested tests with request.changeUrl()", 1,
setUp: setUpWithChangeURL
tearDown: tearDown
test: (test) ->
casper.open(ORIGINAL_URL).then ->
casper.wait 200, ->
test.assertEquals requestURLReceived, SERVER+CHANGED_URL, "response url successfully changed"
casper.run ->
test.done()