Skip to content

Commit 71e6d8b

Browse files
author
Carlos Ramirez
committed
fixed scheme replace // with /, added more test cases
1 parent 33c3750 commit 71e6d8b

2 files changed

Lines changed: 84 additions & 72 deletions

File tree

src/restangular.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,9 @@ module.provider('Restangular', function() {
636636
Path.prototype = new BaseCreator();
637637

638638
Path.prototype.normalizeUrl = function (url){
639-
return url.replace(/[\\\/]+/g, '/');
639+
var parts = /(http[s]?:\/\/)?(.*)?/.exec(url);
640+
parts[2] = parts[2].replace(/[\\\/]+/g, '/');
641+
return (typeof parts[1] !== 'undefined')? parts[1] + parts[2] : parts[2];
640642
};
641643

642644
Path.prototype.base = function(current) {

test/restangularSpec.js

Lines changed: 81 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,62 @@ describe("Restangular", function() {
119119
restangularAccounts = Restangular.all("accounts");
120120
restangularAccount0 = Restangular.one("accounts", 0);
121121
restangularAccount1 = Restangular.one("accounts", 1);
122+
123+
124+
// Another API for testing
125+
customers = [
126+
{
127+
id: 0,
128+
name: "Alice",
129+
status: 'active',
130+
credit: 4000.0
131+
},
132+
{
133+
id: 1,
134+
name: "Bob",
135+
status: 'active',
136+
credit: 4000.0
137+
},
138+
{
139+
id: 2,
140+
name: "Carl",
141+
status: 'active',
142+
credit: 4000.0
143+
}
144+
];
145+
publications = [
146+
{
147+
id: 1,
148+
title: "Sample",
149+
content: "Rich data",
150+
tags: [
151+
'science',
152+
'chemistry'
153+
]
154+
}
155+
];
156+
newCustomer = {
157+
id: 3,
158+
name: "New",
159+
status: 'active',
160+
credit: 4000.0
161+
};
162+
163+
$httpBackend.whenGET("/customers/").respond(customers);
164+
$httpBackend.whenGET("http://localhost:8080/customers/").respond(customers);
165+
$httpBackend.whenGET("api.new.domain/customers/").respond(customers);
166+
$httpBackend.whenGET("/customers/?active=true").respond(customers);
167+
$httpBackend.whenGET("/customers/publications/?tags=chemistry").respond(publications);
168+
$httpBackend.whenPUT("/customers/0").respond(function (method, url, data) {
169+
customers[0] = angular.fromJson(data);
170+
return [200, data, ""];
171+
});
172+
$httpBackend.whenPOST("/customers/").respond(function (method, url, data, headers) {
173+
var newData = angular.fromJson(data);
174+
newData.fromServer = true;
175+
return [201, JSON.stringify(newData), ""];
176+
});
177+
122178
}));
123179

124180
afterEach(function() {
@@ -962,78 +1018,8 @@ describe("Restangular", function() {
9621018
expect(Restangular.stripRestangular(["test","test2"])).toEqual(["test","test2"]);
9631019
});
9641020
});
965-
});
966-
967-
describe("Restangular url path normalize", function() {
968-
var Restangular, $httpBackend;
969-
var customers, publications, newCustomer, allCustomers;
970-
beforeEach(angular.mock.module("restangular"));
971-
972-
beforeEach(inject(function($injector) {
973-
customers = [
974-
{
975-
id: 0,
976-
name: "Alice",
977-
status: 'active',
978-
credit: 4000.0
979-
},
980-
{
981-
id: 1,
982-
name: "Bob",
983-
status: 'active',
984-
credit: 4000.0
985-
},
986-
{
987-
id: 2,
988-
name: "Carl",
989-
status: 'active',
990-
credit: 4000.0
991-
}
992-
];
993-
publications = [
994-
{
995-
id: 1,
996-
title: "Sample",
997-
content: "Rich data",
998-
tags: [
999-
'science',
1000-
'chemistry'
1001-
]
1002-
}
1003-
];
1004-
newCustomer = {
1005-
id: 3,
1006-
name: "New",
1007-
status: 'active',
1008-
credit: 4000.0
1009-
};
1010-
1011-
$httpBackend = $injector.get("$httpBackend");
1012-
Restangular = $injector.get("Restangular");
1013-
1014-
$httpBackend.whenGET("/customers/").respond(customers);
1015-
$httpBackend.whenGET("/customers/?active=true").respond(customers);
1016-
$httpBackend.whenGET("/customers/publications/?tags=chemistry").respond(publications);
1017-
$httpBackend.whenPUT("/customers/0").respond(function (method, url, data) {
1018-
customers[0] = angular.fromJson(data);
1019-
return [200, data, ""];
1020-
});
1021-
$httpBackend.whenPOST("/customers/").respond(function (method, url, data, headers) {
1022-
var newData = angular.fromJson(data);
1023-
newData.fromServer = true;
1024-
return [201, JSON.stringify(newData), ""];
1025-
});
1026-
1027-
allCustomers = Restangular.all('customers/').getList().$object;
1028-
1029-
}));
1030-
1031-
afterEach(function() {
1032-
$httpBackend.verifyNoOutstandingExpectation();
1033-
$httpBackend.verifyNoOutstandingRequest();
1034-
});
10351021

1036-
describe("get list from endpoint ", function () {
1022+
describe("testing normilize url", function () {
10371023

10381024
it("should get a list of objects", function () {
10391025
Restangular.all('customers/').getList().then(function(res){
@@ -1071,5 +1057,29 @@ describe("Restangular url path normalize", function() {
10711057
});
10721058
$httpBackend.flush();
10731059
});
1060+
1061+
it("should return a normilized URL even it has extra slashes", function() {
1062+
var restangularSpaces = Restangular.one("accounts//", 123).one("buildings//", 456).all("spaces///");
1063+
expect(restangularSpaces.getRestangularUrl()).toEqual("/accounts/123/buildings/456/spaces/");
1064+
});
1065+
1066+
it("should create a new service and still working normilized URL", function() {
1067+
var newRes = Restangular.withConfig(function(RestangularConfigurer){
1068+
RestangularConfigurer.setBaseUrl('http://localhost:8080');
1069+
});
1070+
expect(newRes.configuration.baseUrl).toEqual('http://localhost:8080');
1071+
newRes.all("customers////").getList();
1072+
$httpBackend.expectGET('http://localhost:8080/customers/');
1073+
1074+
var newApi = Restangular.withConfig(function(RestangularConfigurer){
1075+
RestangularConfigurer.setBaseUrl('api.new.domain');
1076+
});
1077+
1078+
expect(newApi.configuration.baseUrl).toEqual('api.new.domain');
1079+
newApi.all("customers////").getList();
1080+
$httpBackend.expectGET('api.new.domain/customers/');
1081+
1082+
$httpBackend.flush();
1083+
});
10741084
});
10751085
});

0 commit comments

Comments
 (0)