/
test.js
117 lines (111 loc) · 3.78 KB
/
test.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
/*!
* Pact
* Copyright 2011 Yahoo! Inc.
* Licensed under the BSD license.
*/
var vows = require('vows');
var assert = require('assert');
var pact = require('./index');
var http = require('http');
function mockServer(cb, port) {
return function() {
pact.httpify(http.createServer(function(req, res) {
cb(req, res, port);
}), port).apply(this);
};
}
var headers = { 'Content-Type' : 'text/plain' };
vows.describe('Pact').addBatch({
'A simple server' : {
topic: mockServer(function(req, res) {
res.writeHead(200, headers);
res.end('ok!');
}),
'when / is requested' : {
topic: pact.request(),
'should succeed' : function(topic) {
assert.strictEqual(topic.status, 200);
},
'contains correct header' : function(topic) {
assert.include(topic.headers, 'content-type');
assert.strictEqual(topic.headers['content-type'],
headers['Content-Type']);
},
'contains valid data' : function(topic) {
assert.strictEqual(topic.body, 'ok!');
}
},
},
'A JSON API' : {
topic: mockServer(function(req, res) {
if (req.url === '/') {
res.writeHead(200, {'Content-Type': 'application/json'});
} else if (req.url == '/with-charset') {
res.writeHead(200, {'Content-Type': 'application/json;charset=utf-8'});
}
res.end('{"foo": "bar"}');
}),
'when / is requested' : {
topic: pact.request(),
'contains a parsed JSON response object' : function(topic) {
assert.isObject(topic.body);
assert.strictEqual(topic.body.foo, 'bar');
}
},
'when /with-charset is requested' : {
topic: pact.request(),
'contains a parsed JSON response object' : function(topic) {
assert.isObject(topic.body);
assert.strictEqual(topic.body.foo, 'bar');
}
}
},
'A 302 redirecting server with a relative path' : {
topic: mockServer(function(req, res) {
if (req.url === '/ok') {
res.writeHead(200, headers);
res.end('tango');
} else {
res.writeHead(302, {
'Location' : '/ok'
});
res.end();
}
}),
'when / is requested' : {
topic: pact.request(),
'should be 200 instead of 302' : function(topic) {
assert.strictEqual(topic.status, 200);
},
'contains valid data' : function(topic) {
assert.strictEqual(topic.body, 'tango');
}
}
},
'A 302 redirecting server with an absolute path' : {
topic: mockServer(function(req, res, port) {
if (req.url === '/ok') {
res.writeHead(200, headers);
res.end('foxtrot');
} else if (req.url == '/') {
res.writeHead(302, {
'Location' : 'http://127.0.0.1:' + port + '/ok'
});
res.end();
} else {
res.writeHead(404, headers);
res.end('Invalid path: ' + req.url);
}
}, 8091),
'when / is requested' : {
topic: pact.request(),
'should be 302' : function(topic) {
assert.strictEqual(topic.status, 302);
},
'contains Location with empty body' : function(topic) {
assert.include(topic.headers, 'location');
assert.strictEqual(topic.body, '');
}
}
}
}).export(module);