diff --git a/addon/server.js b/addon/server.js index 67d1a5773..de0e76ef1 100644 --- a/addon/server.js +++ b/addon/server.js @@ -18,6 +18,7 @@ export default class Server { this.environment = options.environment; this.timing = 400; this.namespace = ''; + this.urlPrefix = ''; this._setupStubAliases(); @@ -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'); @@ -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; + } + } diff --git a/blueprints/ember-cli-mirage/files/app/mirage/config.js b/blueprints/ember-cli-mirage/files/app/mirage/config.js index db7c5cd87..9b41d41a4 100644 --- a/blueprints/ember-cli-mirage/files/app/mirage/config.js +++ b/blueprints/ember-cli-mirage/files/app/mirage/config.js @@ -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 diff --git a/tests/integration/server-config-test.js b/tests/integration/server-config-test.js index 1164b6496..a890882eb 100644 --- a/tests/integration/server-config-test.js +++ b/tests/integration/server-config-test.js @@ -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(); + }); +});