Skip to content

Commit

Permalink
Add mockServer origin override (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfmartinez authored Apr 28, 2020
1 parent c5cea45 commit e1415eb
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 4 deletions.
11 changes: 10 additions & 1 deletion addon-test-support/-private/mock-server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { fetch } from 'whatwg-fetch';

let createMock = async function(path, method, statusCode, response) {
let origin = false;

if (path.startsWith('http')) {
const url = new URL(path);
origin = url.origin;
path = `${url.pathname}${url.search}`;
}

return await fetch('/__mock-request', {
method: 'post',
headers: {
Expand All @@ -10,7 +18,8 @@ let createMock = async function(path, method, statusCode, response) {
path,
method,
statusCode,
response
response,
origin
}),
});
}
Expand Down
7 changes: 4 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ let bodyParser = require('body-parser');

function createMockRequest(app) {
app.post('/__mock-request', bodyParser.json({ limit: '50mb' }), (req, res) => {
let mock = nock(req.headers.origin)
const requestOrigin = req.body.origin || req.headers.origin;
let mock = nock(requestOrigin)
.persist()
.intercept(req.body.path, req.body.method)
.reply(req.body.statusCode, req.body.response);
Expand Down Expand Up @@ -56,7 +57,7 @@ function createFastbootTest(app, callback) {
callback({ req, res, options, urlToVisit });
});
}

function createFastbootEcho(app) {
app.post('/fastboot-testing/echo', bodyParser.text(), (req, res) => {
res.send(req.body);
Expand Down Expand Up @@ -104,4 +105,4 @@ module.exports = {
reloadServer,
createServer,
makeFastbootTestingConfig
};
};
22 changes: 22 additions & 0 deletions tests/dummy/app/pods/docs/network-mocking/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ test('it renders the 404 page when it cannot fetch a note', async function(asser
});
```

By default, passing just a path to `mockServer` will use the current host running your app.

Some implementations require a different origin for `mockServer` calls. In that case, you can override the default host by including the hostname with the path.

```js
test('it makes a call to a different host', async function(assert) {
await mockServer.get('http://localhost:3000/api/notes/1', {
data: {
type: 'note',
id: '1',
attributes: {
title: 'Hello world!'
}
}
});

await visit('/notes/1');

assert.dom('[data-test-id="page-not-found"]').exists();
});
```

The `mockServer` also exposes `post`, `patch`, `put`, and `delete` mocking methods.

## Video
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Route from '@ember/routing/route';
import fetch from 'fetch';
import config from 'dummy/config/environment';

export default Route.extend({

async model() {
const { originForOverride } = config;
let response = await fetch(`${originForOverride}/api/notes`, { method: 'get' });
return await response.json();
}

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The data loaded from the server is:

{{#each model as |note|}}
<div data-test-id="title-{{note.id}}">
{{note.title}}
</div>
{{/each}}
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Router.map(function() {
this.route('other', function() {
this.route('get-request');
this.route('post-request');
this.route('origin-override');
this.route('echo');
});
});
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function(environment) {
environment,
rootURL: '/',
locationType: 'auto',
originForOverride: 'http://localhost:3000',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
Expand Down
11 changes: 11 additions & 0 deletions tests/fastboot/network-mocking-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
visit,
mockServer
} from "ember-cli-fastboot-testing/test-support";
import config from 'dummy/config/environment';

module("Fastboot | network mocking", function(hooks) {
setup(hooks);
Expand Down Expand Up @@ -96,4 +97,14 @@ module("Fastboot | network mocking", function(hooks) {

assert.dom('[data-test-id="title-1"]').exists();
});

test("it can override the origin on a mock response", async function(assert) {
const { originForOverride } = config;

await mockServer.get(`${originForOverride}/api/notes`, [{ id: 1, title: "get note" }]);

await visit("/examples/network/other/origin-override");

assert.dom('[data-test-id="title-1"]').hasText("get note");
});
});

0 comments on commit e1415eb

Please sign in to comment.