Skip to content

Commit

Permalink
Merge pull request #46 from embermap/move-body-parser
Browse files Browse the repository at this point in the history
Add an echo endpoint for testing
  • Loading branch information
ryanto committed May 1, 2019
2 parents f2d1d5b + 59ab493 commit 10b72c9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ module.exports = {

_fastbootRenderingMiddleware(app) {

app.use(bodyParser.json());
app.post('/__mock-request', (req, res) => {
app.post('/__mock-request', bodyParser.json(), (req, res) => {
let mock = nock(req.headers.origin)
.persist()
.intercept(req.body.path, req.body.method)
Expand Down Expand Up @@ -118,6 +117,13 @@ module.exports = {
res.json({ err: jsonError });
});
});

if (this.app.name === "dummy") {
// our dummy app has an echo endpoint!
app.post('/fastboot-testing/echo', bodyParser.text(), (req, res) => {
res.send(req.body);
});
}
},

postBuild(result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Controller from '@ember/controller';

export default Controller.extend({
queryParams: ['message']
});
20 changes: 20 additions & 0 deletions tests/dummy/app/pods/examples/network/other/echo/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Route from '@ember/routing/route';
import fetch from 'fetch';

export default Route.extend({

// this endpoint is defined in index.js, it's used to represent
// a url that ember-cli might already have in it's express router.

// we should be able to post a body to this echo endpoint and
// get a reply back.
model(params) {
return fetch('/fastboot-testing/echo', {
method: 'POST',
body: params.message
}).then(response => {
return response.text();
});
}

});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div data-test-id="echo">{{model}}</div>
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Router.map(function() {
this.route('other', function() {
this.route('get-request');
this.route('post-request');
this.route('echo');
});
});

Expand Down
5 changes: 5 additions & 0 deletions tests/fastboot/network-mocking-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { setup, visit, mockServer } from 'ember-cli-fastboot-testing/test-suppor
module('Fastboot | network mocking', function(hooks) {
setup(hooks);

test('it will not change an endpoint that already exists', async function(assert) {
await visit('/examples/network/other/echo?message=hello%20world');
assert.dom('[data-test-id="echo"]').hasText("hello world");
});

test('it can mock an array of models', async function(assert) {
await mockServer.get('/api/posts', {
data: [
Expand Down

0 comments on commit 10b72c9

Please sign in to comment.