Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Correctly build full URL from parts
Browse files Browse the repository at this point in the history
The old way of building a URL was too brittle and didn't cover a lot of
the different cases that could take place, in different combinations of
the host, namespace, and segment having or not having slashes included.

The new approach implemented here handles the various cases correctly,
and ensures that we do not remove a trailing slash if one was provided.

Closes #70
  • Loading branch information
alexlafroscia committed May 30, 2016
1 parent 8826919 commit b46efae
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/unit/ajax-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,72 @@ test('it JSON encodes JSON:API "extension" request data automatically', function
}
});
});

test('it correctly creates the URL to request', function(assert) {
class NamespaceLeadingSlash extends AjaxRequest {
static get slashType() {
return 'leading slash';
}
get namespace() {
return '/bar';
}
}

class NamespaceTrailingSlash extends AjaxRequest {
static get slashType() {
return 'trailing slash';
}
get namespace() {
return 'bar/';
}
}

class NamespaceTwoSlash extends AjaxRequest {
static get slashType() {
return 'leading and trailing slash';
}
get namespace() {
return '/bar/';
}
}

class NamespaceNoSlash extends AjaxRequest {
static get slashType() {
return 'no slashes';
}
get namespace() {
return 'bar';
}
}

const hosts = [
{ hostType: 'trailing slash', host: 'http://foo.com/' },
{ hostType: 'no trailing slash', host: 'http://foo.com' }
];

[NamespaceLeadingSlash, NamespaceTrailingSlash, NamespaceTwoSlash, NamespaceNoSlash].forEach((Klass) => {
let req = new Klass();

hosts.forEach((exampleHost) => {
const { hostType, host } = exampleHost;
['/baz', 'baz'].forEach((segment) => {
assert.equal(
req._buildURL(segment, { host }),
'http://foo.com/bar/baz',
`Host with ${hostType}, Namespace with ${Klass.slashType}, segment: ${segment}`
);
});
['/baz/', 'baz/'].forEach((segment) => {
assert.equal(
req._buildURL(segment, { host }),
'http://foo.com/bar/baz/',
`Host with ${hostType}, Namespace with ${Klass.slashType}, segment: ${segment}`
);
});
});
});

let req = new AjaxRequest();
assert.equal(req._buildURL('/baz', { host: 'http://foo.com' }), 'http://foo.com/baz', 'Builds URL correctly without namespace');
assert.equal(req._buildURL('/baz'), '/baz', 'Builds URL correctly without namespace or host');
});

0 comments on commit b46efae

Please sign in to comment.