forked from angular-app/angular-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-proxy.js
122 lines (106 loc) · 3.73 KB
/
mongo-proxy.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
var rewire = require('rewire');
var mongoProxyFactory = rewire('../lib/mongo-proxy');
var url = require('url');
var proxy;
var testMapUrl = function(test, request, expected, message) {
var actual = proxy.mapUrl(request);
expected = url.parse(expected, true);
test.equal(actual.protocol, expected.protocol);
test.equal(actual.hostname, expected.hostname);
// To test the path we need to parse again to account for order differences in query
var actualPath = url.parse(actual.path, true);
var expectedPath = url.parse(expected.path, true);
test.deepEqual(actualPath.pathname, expectedPath.pathname);
test.deepEqual(actualPath.query, expectedPath.query);
test.done();
};
// Mock up the https service
mongoProxyFactory.__set__('https', {
request: function(options, callback) {
return { end: function() {} };
}
});
// Create a proxy to test
proxy = mongoProxyFactory('https://api.mongolab.com/api/1/databases','4fb51e55e4b02e56a67b0b66');
module.exports = {
factory: {
testFactory: function(test) {
test.ok(!!proxy, 'The created proxy is defined.');
test.done();
}
},
mapUrl : {
testDatabasesUrl: function(test) {
testMapUrl(test,
'/',
'https://api.mongolab.com/api/1/databases/?apiKey=4fb51e55e4b02e56a67b0b66');
},
testCollectionsUrl: function(test) {
testMapUrl(test,
'/ascrum/collections',
'https://api.mongolab.com/api/1/databases/ascrum/collections?apiKey=4fb51e55e4b02e56a67b0b66');
},
testUrlMapWithQuery: function(test) {
testMapUrl(test,
'/ascrum/collections/users?q=%7B%7D&c=true',
'https://api.mongolab.com/api/1/databases/ascrum/collections/users?q=%7B%7D&c=true&apiKey=4fb51e55e4b02e56a67b0b66');
},
testUrlMapWithId: function(test) {
testMapUrl(test,
'/ascrum/collections/users/SOME_ID',
'https://api.mongolab.com/api/1/databases/ascrum/collections/users/SOME_ID?apiKey=4fb51e55e4b02e56a67b0b66');
},
testUrlMapWithLocalPath: function(test) {
testMapUrl(test,
'/ascrum/collections/users/SOME_ID',
'https://api.mongolab.com/api/1/databases/ascrum/collections/users/SOME_ID?apiKey=4fb51e55e4b02e56a67b0b66');
}
},
mapRequest: {
testMethod: function(test) {
var newRequest = proxy.mapRequest({url:'/', method: 'GET'});
test.equal(newRequest.method, 'GET');
newRequest = proxy.mapRequest({url:'/', method: 'POST'});
test.equal(newRequest.method, 'POST');
newRequest = proxy.mapRequest({url:'/', method: 'HEAD'});
test.equal(newRequest.method, 'HEAD');
test.done();
},
testHostHeader: function(test) {
var newRequest = proxy.mapRequest({url:'/', headers: {Host:'localhost:3000'}});
test.equal(newRequest.headers.host, 'api.mongolab.com');
test.done();
},
testHeaders: function(test) {
var headers = { Accept:'text/html', 'Cache-Control':'max-age=0', Host:'localhost:3000', Connection:'keep-alive' };
var newRequest = proxy.mapRequest({url:'/', headers: headers});
for(var key in newRequest.headers) {
if(key !== 'Host') { // Host gets modified, see testHostHeader
test.equal(newRequest.headers[key], headers[key]);
}
}
test.done();
}
},
proxy: {
checkHttpsRequestOptions: function(test) {
var req = {
url: '/ascrum/collections/users?q=%7B%7D&c=true',
method: 'GET',
data: ''
};
var res = { };
proxy(req, res);
test.done();
},
nextShouldNotBeCalled: function(test) {
var req = { url: '', end: function() {} };
var res = { };
var next = function() {
throw new Error('Should not call next.');
};
proxy(req, res, next);
test.done();
}
}
};