|
| 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