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

Finally add $location.url() to ngoverrides, and tests. #19

Merged
merged 4 commits into from
Apr 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/*
.DS_Store
23 changes: 22 additions & 1 deletion lib/ngoverrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ function registerModule(context) {
}
);

var reparseUrl = function () {
requestUrlParts = url.parse(url.format(requestUrlParts), true);
};

return {
location: {
absUrl: absUrl,
Expand All @@ -151,6 +155,8 @@ function registerModule(context) {
redirectTo,
oldUrl
);
requestUrlParts.pathname = set;
reparseUrl();
return this;
}
return parts.pathname;
Expand Down Expand Up @@ -181,6 +187,12 @@ function registerModule(context) {
else {
parts.query = set;
}
var searchArgs = [];
for (var k in parts.query) {
searchArgs.push(k + '=' + parts.query[k]);
}
requestUrlParts.search = '?' + searchArgs.join('&');
reparseUrl();
return this;
}
return parts.query;
Expand All @@ -189,7 +201,16 @@ function registerModule(context) {
replace: function () {
return this;
},
runningOnServer: true
runningOnServer: true,
url: parsedUrl(
function (parts, set) {
if (set) {
requestUrlParts = url.parse(set, true);
reparseUrl();
}
return requestUrlParts.path;
}
)
},
setRequest: function (newRequest) {
if (request) {
Expand Down
18 changes: 14 additions & 4 deletions res/fakeangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@
// This is a fake angular-like thing that we can load into an angular context for tests.
var modulesRegistered = [];
var requestsRegistered = [];
var factoriesRegistered = {};
var providersRegistered = {};
var directivesRegistered = {};
window.angular = {
fake: true,
modulesRegistered: modulesRegistered,
requestsRegistered: requestsRegistered,
factoriesRegistered: factoriesRegistered,
providersRegistered: providersRegistered,
directivesRegistered: directivesRegistered,
module: function (name, deps) {
if (deps) {
modulesRegistered.push(name);
}
// just enough module to keep ngoverrides happy
// just enough module to keep ngoverrides happy; store the code
// so we can test it
return {
factory: function (name) {
factory: function (name, func) {
factoriesRegistered[name] = func;
return this;
},
provider: function (name) {
provider: function (name, func) {
providersRegistered[name] = func;
return this;
},
directive: function (name) {
directive: function (name, func) {
directivesRegistered[name] = func;
return this;
}
};
Expand Down
77 changes: 77 additions & 0 deletions tests/ngoverrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

var path = require('path');
var angularServer = require('../lib/main.js');

exports.testNgoverrides = function (test) {
test.expect(16);
var server = angularServer.Server(
{
serverScripts: [
path.join(__dirname, '../res/fakeangular.js')
]
}
);
var $broadcast;
var fakeInjector = {
get: function () {
return {
$broadcast: $broadcast
};
}
};
var mw = server.wrapMiddlewareWithAngular(
function (req, res, next, injector) {
var sRCFactory = injector.angular.factoriesRegistered.serverRequestContext;
test.ok(sRCFactory, 'serverRequestContext factory registered');
var sRC = sRCFactory(fakeInjector);
test.ok(!sRC.hasRequest(), 'context has no request initially');
sRC.setRequest({
url: '/the/path/to/riches.jpg?abc=123',
headers: {
host: 'foo.bar.com'
}
});
test.ok(sRC.hasRequest(), 'context has request after setRequest');
test.throws(function () {
sRC.setRequest({}, null, 'second setRequest throws');
});
var $location = sRC.location;
test.equal($location.absUrl(), 'http://foo.bar.com/the/path/to/riches.jpg?abc=123',
'$location has expected absUrl');
test.equal($location.host(), 'foo.bar.com', '$location has expected host');
test.deepEqual($location.search(), {abc: '123'}, '$location has expected search');
$location.search({def: '456'});
test.deepEqual($location.search(), {def: '456'}, '$location has expected search after set');
$location.search('ghi', '789');
test.deepEqual($location.search(), {def: '456', ghi: '789'},
'$location has expected search after value set');
test.equal($location.path(), '/the/path/to/riches.jpg', '$location has expected path');
$broadcast = function ($event, redirectTo, oldUrl) {
test.equal($event, '$locationChangeSuccess', 'setting path broadcasts success');
test.equal(redirectTo, 'http://foo.bar.com/st/elsewhere?def=456&ghi=789',
'expected redirectTo broadcast after path set');
};
$location.path('/st/elsewhere');
test.equal($location.path(), '/st/elsewhere', 'path was updated');
test.equal($location.url(), '/st/elsewhere?def=456&ghi=789', '$location has expected url');
$broadcast = function ($event, redirectTo, oldUrl) {
test.equal(redirectTo, 'http://foo.bar.com/other/place?klm=789',
'expected redirectTo broadcast after url set');
};
$location.url('/other/place?klm=789');
test.equal($location.url(), '/other/place?klm=789', '$location has expected url after set');
test.equal($location.path(), '/other/place', '$location has expected path after set');
test.done();
}
);

var req = {};
req.get = function () {
return 'baz';
};
req.protocol = 'http';
req.url = '/foo';

mw(req, {}, {});
};