/
casperserver.js
143 lines (116 loc) · 3.61 KB
/
casperserver.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
var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});
require('../casperserver.js').create(casper, {port: 8007, responsesDir: './responses/'});
/*
* Utils
*/
casper.sendGET = function (path) {
// Need a callback
this.evaluate(function(path) {
var xhr = new XMLHttpRequest();
xhr.open("GET", path, true);
xhr.onload = function(e) {
content = this.response;
document.title = content;
return content;
};
xhr.send();
}, {path: path});
};
casper.sendPOST = function (path, data) {
this.evaluate(function(path, data) {
__utils__.sendAJAX(path, 'POST', data, true);
}, {path: path, data: data});
};
casper.server.watchPath('^(/js/|/css/|/scripts/)', {
filePath: function (request) {
return '.' + request.url;
},
permanent: true
});
casper.server.start();
casper.start('http://localhost:8007', function (){
this.test.assertTitle('Title from index.html', 'index.html is loaded and title is correct');
});
casper.then(function () {
var actual = this.evaluate(function(){
return window.this_is_a_dummy_function();
});
this.test.assertEquals(actual, "ok", "scripts/test.js has been loaded");
});
/*
* Define content from a string
*/
casper.then(function () {
var path = '/123456/';
this.server.watchPath(path, {content: "okok"});
this.sendGET(path);
});
casper.then(function () {
this.test.assertTitle("okok", "We can define a content response from a string.");
});
/*
* Define content from a function
*/
casper.then(function () {
var path = '/azerty/';
this.server.watchPath(path, {content: function (request) {return "Requested: " + request.url;}});
this.sendGET(path);
});
casper.then(function () {
this.test.assertTitle("Requested: /azerty/", "We can define a response content from a function.");
});
/*
* Define a filePath
*/
casper.then(function () {
var path = '/a-simple-path/';
this.server.watchPath(path, {filePath: "simple_response.txt"});
this.sendGET(path);
});
casper.then(function () {
this.test.assertTitle("Content from file simple_response.txt", "We can use a simple filePath to serve the response.");
});
/*
* Define a filePath from a function
*/
casper.then(function () {
var path = '/another-path/';
this.server.watchPath(path, {filePath: function (request) { return "simple_response.txt";}});
this.sendGET(path);
});
casper.then(function () {
this.test.assertTitle("Content from file simple_response.txt", "We can use a function to define the filePath to use as content.");
});
/*
* Use a regex to catch path
*/
casper.then(function () {
this.server.watchPath('^/path/', {content: function (request) { return "path: " + request.url;}});
this.server.watchPath('^/folder/', {content: function (request) { return "folder: " + request.url;}});
this.sendGET("/path/22/");
});
casper.then(function () {
this.test.assertTitle("path: /path/22/", "We can use a regex to catch paths.");
});
/*
* Listen a request
*/
casper.then(function () {
path = "/123/";
this.server.watchPath(path, {content: "ok"});
this.server.watchRequest(path);
this.sendPOST(path, {myfield: "myvalue"});
});
casper.then(function () {
var request = this.server.getWatchedRequest(path);
this.test.assertTruthy(request, "Request has been catched");
this.test.assertEqual(request.method, 'POST', 'POST received');
this.test.assertEqual(request.post.myfield, "myvalue", 'POST myfield value is correct');
});
casper.run(function() {
this.test.done();
this.test.renderResults(true);
});