Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uhtred committed Feb 11, 2016
1 parent 9437343 commit ee3d38d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/models/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ Endpoints.prototype.find = function (data, callback) {
};

Endpoints.prototype.found = function (endpoint, captures, callback, data) {
var filename;
var filePath;
var body;
var filename, filePath, body;
var response = endpoint.response[this.sightings[endpoint.id]++ % endpoint.response.length];
var _ref = response.body;

Expand All @@ -121,15 +119,15 @@ Endpoints.prototype.found = function (endpoint, captures, callback, data) {
filePath = path.resolve(this.datadir, filename);

try {
if( path.extname(filename) === '.js' ) {
if (path.extname(filename) === '.js') {
body = require(filePath.toString())({
response: response,
endpoint: endpoint,
captures: captures,
data: data
});

response.body = new Buffer(JSON.stringify(body), 'utf8');
response.body = new Buffer(body, 'utf8');
} else {
response.body = fs.readFileSync(filePath);
}
Expand Down
7 changes: 7 additions & 0 deletions test/data/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@
file: test/data/endpoints.file
body: body contents!

- request:
url: /file/module
method: POST
response:
file: test/data/endpoints.js
body: body contents!



- request:
Expand Down
5 changes: 5 additions & 0 deletions test/data/endpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
module.exports = function (metadata) {
var post = JSON.parse(metadata.data.post);
return JSON.stringify({title: post.title});
};
12 changes: 12 additions & 0 deletions test/e2e.stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,18 @@ describe('End 2 End Stubs Test Suite', function () {
done();
});
});

it('should handle module file response when file can be found', function (done) {
var expect = '{"title":"New Item"}';
this.context.url = '/file/module';
this.context.method = 'post';
this.context.post = expect;

createRequest(this.context, function (response) {
assert(response.data === expect);
done();
});
});
});

describe('request', function () {
Expand Down
6 changes: 3 additions & 3 deletions test/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('Endpoint', function () {
actual = new Endpoint(this.data);

waitsFor(function () {
return actual.response[0].status === 301;
return actual.response[0].status === 302;
}, 'endpoint to record', waitTime, done);
});

Expand All @@ -150,7 +150,7 @@ describe('Endpoint', function () {
actual = new Endpoint(this.data);

waitsFor(function () {
return actual.response[0].status === 301 && actual.response[1].status === 200;
return actual.response[0].status === 302 && actual.response[1].status === 200;
}, 'endpoint to record', waitTime, done);
});

Expand All @@ -175,7 +175,7 @@ describe('Endpoint', function () {
actual = new Endpoint(data);

waitsFor(function () {
return actual.response[0].status === 301 && actual.response[1].status === 420;
return actual.response[0].status === 302 && actual.response[1].status === 420;
}, 'endpoint to record', waitTime, done);
});
});
Expand Down

0 comments on commit ee3d38d

Please sign in to comment.