Skip to content
This repository was archived by the owner on Aug 9, 2018. It is now read-only.

Commit 8e4d3cf

Browse files
author
Kevin Shay
committed
Add a url() method to the overridden $location, with tests.
There's now a set of tests for all the important $location methods. This also fixes a bug in the search() method that the tests revealed.
1 parent 162e145 commit 8e4d3cf

File tree

4 files changed

+114
-5
lines changed

4 files changed

+114
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/*
2+
.DS_Store

lib/ngoverrides.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ function registerModule(context) {
127127
}
128128
);
129129

130+
var reparseUrl = function () {
131+
requestUrlParts = url.parse(url.format(requestUrlParts), true);
132+
}
133+
130134
return {
131135
location: {
132136
absUrl: absUrl,
@@ -151,6 +155,8 @@ function registerModule(context) {
151155
redirectTo,
152156
oldUrl
153157
);
158+
requestUrlParts.pathname = set;
159+
reparseUrl();
154160
return this;
155161
}
156162
return parts.pathname;
@@ -181,6 +187,12 @@ function registerModule(context) {
181187
else {
182188
parts.query = set;
183189
}
190+
var searchArgs = [];
191+
for (var k in parts.query) {
192+
searchArgs.push(k + '=' + parts.query[k]);
193+
}
194+
requestUrlParts.search = '?' + searchArgs.join('&');
195+
reparseUrl();
184196
return this;
185197
}
186198
return parts.query;
@@ -189,7 +201,16 @@ function registerModule(context) {
189201
replace: function () {
190202
return this;
191203
},
192-
runningOnServer: true
204+
runningOnServer: true,
205+
url: parsedUrl(
206+
function (parts, set) {
207+
if (set) {
208+
requestUrlParts = url.parse(set, true);
209+
reparseUrl();
210+
}
211+
return requestUrlParts.path;
212+
}
213+
)
193214
},
194215
setRequest: function (newRequest) {
195216
if (request) {

res/fakeangular.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,33 @@
22
// This is a fake angular-like thing that we can load into an angular context for tests.
33
var modulesRegistered = [];
44
var requestsRegistered = [];
5+
var factoriesRegistered = {};
6+
var providersRegistered = {};
7+
var directivesRegistered = {};
58
window.angular = {
69
fake: true,
710
modulesRegistered: modulesRegistered,
811
requestsRegistered: requestsRegistered,
12+
factoriesRegistered: factoriesRegistered,
13+
providersRegistered: providersRegistered,
14+
directivesRegistered: directivesRegistered,
915
module: function (name, deps) {
1016
if (deps) {
1117
modulesRegistered.push(name);
1218
}
13-
// just enough module to keep ngoverrides happy
19+
// just enough module to keep ngoverrides happy; store the code
20+
// so we can test it
1421
return {
15-
factory: function (name) {
22+
factory: function (name, func) {
23+
factoriesRegistered[name] = func;
1624
return this;
1725
},
18-
provider: function (name) {
26+
provider: function (name, func) {
27+
providersRegistered[name] = func;
1928
return this;
2029
},
21-
directive: function (name) {
30+
directive: function (name, func) {
31+
directivesRegistered[name] = func;
2232
return this;
2333
}
2434
};

tests/ngoverrides.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var angularServer = require('../lib/main.js');
5+
6+
exports.testNgoverrides = function (test) {
7+
test.expect(16);
8+
var server = angularServer.Server(
9+
{
10+
serverScripts: [
11+
path.join(__dirname, '../res/fakeangular.js')
12+
]
13+
}
14+
);
15+
var $broadcast;
16+
var fakeInjector = {
17+
get: function () {
18+
return {
19+
$broadcast: $broadcast
20+
};
21+
}
22+
};
23+
var mw = server.wrapMiddlewareWithAngular(
24+
function (req, res, next, injector) {
25+
var sRCFactory = injector.angular.factoriesRegistered.serverRequestContext;
26+
test.ok(sRCFactory, 'serverRequestContext factory registered');
27+
var sRC = sRCFactory(fakeInjector);
28+
test.ok(!sRC.hasRequest(), 'context has no request initially');
29+
sRC.setRequest({
30+
url: '/the/path/to/riches.jpg?abc=123',
31+
headers: {
32+
host: 'foo.bar.com'
33+
}
34+
});
35+
test.ok(sRC.hasRequest(), 'context has request after setRequest');
36+
test.throws(function () {
37+
sRC.setRequest({}, null, 'second setRequest throws');
38+
});
39+
var $location = sRC.location;
40+
test.equal($location.absUrl(), 'http://foo.bar.com/the/path/to/riches.jpg?abc=123',
41+
'$location has expected absUrl');
42+
test.equal($location.host(), 'foo.bar.com', '$location has expected host');
43+
test.deepEqual($location.search(), {abc: '123'}, '$location has expected search');
44+
$location.search({def: '456'});
45+
test.deepEqual($location.search(), {def: '456'}, '$location has expected search after set');
46+
$location.search('ghi', '789');
47+
test.deepEqual($location.search(), {def: '456', ghi: '789'},
48+
'$location has expected search after value set');
49+
test.equal($location.path(), '/the/path/to/riches.jpg', '$location has expected path');
50+
$broadcast = function ($event, redirectTo, oldUrl) {
51+
test.equal($event, '$locationChangeSuccess', 'setting path broadcasts success');
52+
test.equal(redirectTo, 'http://foo.bar.com/st/elsewhere?def=456&ghi=789',
53+
'expected redirectTo broadcast after path set');
54+
};
55+
$location.path('/st/elsewhere');
56+
test.equal($location.path(), '/st/elsewhere', 'path was updated');
57+
test.equal($location.url(), '/st/elsewhere?def=456&ghi=789', '$location has expected url');
58+
$broadcast = function ($event, redirectTo, oldUrl) {
59+
test.equal(redirectTo, 'http://foo.bar.com/other/place?klm=789',
60+
'expected redirectTo broadcast after url set');
61+
};
62+
$location.url('/other/place?klm=789');
63+
test.equal($location.url(), '/other/place?klm=789', '$location has expected url after set');
64+
test.equal($location.path(), '/other/place', '$location has expected path after set');
65+
test.done();
66+
}
67+
);
68+
69+
var req = {};
70+
req.get = function () {
71+
return 'baz';
72+
};
73+
req.protocol = 'http';
74+
req.url = '/foo';
75+
76+
mw(req, {},{});
77+
};

0 commit comments

Comments
 (0)