Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelfLink suggested fixes - allow cross-site URLs to work with selfLinks #785

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions src/restangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ module.provider('Restangular', function() {
config.getUrlFromElem = function(elem) {
return config.getFieldFromElem(config.restangularFields.selfLink, elem);
};

config.useCannonicalId = _.isUndefined(config.useCannonicalId) ? false : config.useCannonicalId;
object.setUseCannonicalId = function(value) {
config.useCannonicalId = value;
Expand Down Expand Up @@ -557,7 +557,6 @@ module.provider('Restangular', function() {
add += what;
url += add;
}

if (this.config.suffix &&
url.indexOf(this.config.suffix, url.length - this.config.suffix.length) === -1 &&
!this.config.getUrlFromElem(current)) {
Expand Down Expand Up @@ -636,7 +635,9 @@ module.provider('Restangular', function() {
var elemSelfLink = __this.config.getUrlFromElem(elem);
if (elemSelfLink) {
if (__this.config.isAbsoluteUrl(elemSelfLink)) {
return elemSelfLink;
return (__this.config.absoluteUrl && __this.config.absoluteUrl !== true)
? __this.config.absoluteUrl.replace(/\/$/, '') + "/" + elemSelfLink.replace(/^\//, '') //add the absolute URL to the start if set
: elemSelfLink;
} else {
elemUrl = elemSelfLink;
}
Expand All @@ -661,6 +662,8 @@ module.provider('Restangular', function() {
}
}
}
//remove any trailing suffix from the url before concatinating new subresources
if (__this.config.suffix) acum = acum.replace(new RegExp(__this.config.suffix + '$'), '');

return acum.replace(/\/$/, '') + '/' + elemUrl;

Expand Down
49 changes: 48 additions & 1 deletion test/restangularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ describe("Restangular", function() {
{from: "Anonymous", amount: 0.1416, id: 1, _links: {self: "/accountsHAL/paul/transactions/1"}}
], _links: {self: "/accountsHAL/paul"}}
];

accountsModelWithSuffix = [
{id: 0, user: "Martin", amount: 42, transaction: [], href: "/accountsSuffix/martin.json"},
{id: 1, user: "Paul", amount: 3.1416, transaction: [
{from: "Martin", amount: 3, id: 0, href: "/accounts/paul/transactions/0.json"},
{from: "Anonymous", amount: 0.1416, id: 1, href: "/accounts/paul/transactions/1.json"}
], href: "/accounts/paul.json"}
];

infoModel = {
id: 0, text: "Some additional account information"
}
};

newAccount = {id: 44, user: "First User", amount: 45, transactions: []};

Expand Down Expand Up @@ -63,6 +71,14 @@ describe("Restangular", function() {
accountsHalModel[0] = angular.fromJson(data);
return [200, data, ""];
});

$httpBackend.whenGET("/accountsSuffix").respond(accountsModelWithSuffix);
$httpBackend.whenGET("/accountsSuffix.json").respond(accountsModelWithSuffix);
$httpBackend.whenGET("/accountsSuffix/martin.json").respond(accountsModelWithSuffix[0]);
$httpBackend.whenPUT("/accountsSuffix/martin/transactions.json").respond(function(method, url, data) {
accountsModelWithSuffix[0].transaction = angular.fromJson(data);
return [200, data, ""];
});

// Full URL
$httpBackend.whenGET('http://accounts.com/all').respond(accountsModel);
Expand Down Expand Up @@ -745,6 +761,37 @@ describe("Restangular", function() {
$httpBackend.flush();
});
});

describe("Self linking with suffix", function() {
it("Should allow for suffix in selfLinks", function() {
var linkRestangular = Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setRequestSuffix('.json');
});

var arr = linkRestangular.all('accountsSuffix').getList().$object;
$httpBackend.flush();

var account = arr[0];
var transactions = account.all('transactions');
expect(transactions.getRestangularUrl()).toEqual("/accountsSuffix/martin/transactions");
});
});

describe("Cross-site selfLinks", function() {
it("Should add the absolute URL to the self link", function() {
var linkRestangular = Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setRequestSuffix('.json');
RestangularConfigurer.setSelfLinkAbsoluteUrl('http://accounts.com/');
});

var arr = linkRestangular.all('accountsSuffix').getList().$object;
$httpBackend.flush();

var account = arr[0];
expect(account.getRestangularUrl()).toEqual("http://accounts.com/accountsSuffix/martin.json");
expect(account.all('transactions').getRestangularUrl()).toEqual("http://accounts.com/accountsSuffix/martin/transactions");
});
});

describe("Singe one (endpoint not expecting an id)", function() {
it('does not use the id for single resource GET', function() {
Expand Down