Skip to content

Commit

Permalink
adds support for urlPrefix to allow requests to external resources; f…
Browse files Browse the repository at this point in the history
…ixes miragejs#228
  • Loading branch information
jamesdixon committed Aug 10, 2015
1 parent eb3682e commit 41e09c7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
17 changes: 16 additions & 1 deletion addon/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class Server {
this.environment = options.environment;
this.timing = 400;
this.namespace = '';
this.urlPrefix = '';

this._setupStubAliases();

Expand Down Expand Up @@ -69,7 +70,7 @@ export default class Server {
var _this = this;
path = path[0] === '/' ? path.slice(1) : path;

this.interceptor[verb].call(this.interceptor, this.namespace + '/' + path, function(request) {
this.interceptor[verb].call(this.interceptor, this._getFullPath(path), function(request) {
var response = controller.handle(verb, handler, (_this.schema || _this.db), request, code, options);
var shouldLog = typeof _this.logging !== 'undefined' ? _this.logging : (_this.environment !== 'test');

Expand Down Expand Up @@ -168,4 +169,18 @@ export default class Server {
return ary;
}

/*
Builds a full path for Pretender to monitor based on the `path` and
configured options (`urlPrefix` and `namespace`).
*/
_getFullPath(path) {
var urlPrefix = this.urlPrefix,
namespace = this.namespace;

urlPrefix = urlPrefix[urlPrefix.length - 1] === '/' ? urlPrefix : urlPrefix + '/';
namespace = namespace ? namespace + '/' : namespace;

return urlPrefix + namespace + path;
}

}
1 change: 1 addition & 0 deletions blueprints/ember-cli-mirage/files/app/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function() {
Note: these only affect routes defined *after* them!
*/
// this.urlPrefix = ''; // make this `http://localhost:8080`, for example, if your API is on a different server
// this.namespace = ''; // make this `api`, for example, if your API is namespaced
// this.timing = 400; // delay for each request, automatically set to 0 during testing

Expand Down
43 changes: 43 additions & 0 deletions tests/integration/server-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,46 @@ test("namespace can be configured", function(assert) {
done();
});
});

test("urlPrefix can be configured", function(assert) {
assert.expect(1);
var done = assert.async();
var server = this.server;

var contacts = [
{id: 1, name: 'Link'},
{id: 2, name: 'Zelda'},
];
server.db.loadData({
contacts: contacts
});
server.urlPrefix = 'http://localhost:3000';
server.get('/contacts');

$.getJSON('http://localhost:3000/contacts', function(data) {
assert.deepEqual(data, { contacts: contacts });
done();
});
});

test("urlPrefix and namespace can be configured simultaneously", function(assert) {
assert.expect(1);
var done = assert.async();
var server = this.server;

var contacts = [
{id: 1, name: 'Link'},
{id: 2, name: 'Zelda'},
];
server.db.loadData({
contacts: contacts
});
server.urlPrefix = 'http://localhost:3000';
server.namespace = 'api';
server.get('/contacts');

$.getJSON('http://localhost:3000/api/contacts', function(data) {
assert.deepEqual(data, { contacts: contacts });
done();
});
});

0 comments on commit 41e09c7

Please sign in to comment.