/
stubbing.js
77 lines (63 loc) · 2.1 KB
/
stubbing.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
'use strict';
var connect = require('connect'),
merge = connect.utils.merge,
repositories = require('repository');
// We don't want to use keepalive connections, because a test case
// may shutdown the stub, which prevents new connections for
// the port, but that won't prevent the system under test
// from reusing an existing TCP connection after the stub
// has shutdown, causing difficult to track down bugs when
// multiple tests are run.
var defaults = {
statusCode: 200,
headers: {
'Connection': 'close'
},
body: ''
};
function methodMatches(actual, expected) {
return !expected || actual === expected;
}
function allHeadersMatch(actual, expected) {
return !expected || Object.keys(expected).every(function (header) {
return expected[header] === actual[header.toLowerCase()];
});
}
function bodyMatches(actual, expected) {
return !expected || actual.indexOf(expected) >= 0;
}
function requestMatches(actual, expected) {
return methodMatches(actual.method, expected.method) &&
allHeadersMatch(actual.headers, expected.headers) &&
bodyMatches(actual.body, expected.body);
}
function create() {
var stubs = repositories.create();
function findFirstMatchingStub(request) {
var possibleMatches = stubs.load(request.url),
stub;
stub = possibleMatches.filter(function (stub) {
return (!stub.request || requestMatches(request, stub.request));
})[0];
if (!stub || !stub.response) {
return Object.create(defaults);
}
return stub.response;
}
function stubber(request, response) {
var defaultStub = Object.create(defaults),
match = findFirstMatchingStub(request),
headers = merge(defaultStub.headers, match.headers),
stub = merge(defaultStub, match);
response.writeHead(stub.statusCode, headers);
response.write(stub.body);
response.end();
}
return {
middleware: stubber,
addStub: function (stub) {
stubs.save(stub);
}
};
}
exports.create = create;