Skip to content

Commit

Permalink
feat(Server): Add throwOnError behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
liqueurdetoile committed Apr 10, 2019
1 parent 1bebddc commit 2c3f42d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
56 changes: 42 additions & 14 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import sinon from 'sinon';
* @author Liqueur de Toile <contact@liqueurdetoile.com>
*/
export class Server {
_presets = {};
_fixtures = [];
_onError = 'throw';
_presets = {};

history = new ServerHistory();

Expand Down Expand Up @@ -67,6 +68,13 @@ export class Server {
if (this.running && resetStub) this.stub.resetHistory();
this.history.reset();
this._fixtures = [];
this.throwOnError(true);

return this;
}

throwOnError(throwOnError) {
this._onError = throwOnError ? 'throw' : 'fail500';

return this;
}
Expand Down Expand Up @@ -152,30 +160,50 @@ export class Server {
if (!this._fixtures.length) throw new Error('No fixtures defined to respond to request');

for (let fixture of this._fixtures) {
// Do not register fallback fixture
if (fixture._matcher === null) continue;
if (await fixture.match(request)) matches.push(fixture);
}

if (!matches.length) throw new Error('Unable to find a matching fixture for the current request');
if (matches.length === 1) return matches[0];
// Disambiguate default from others and returns the first
for (let fixture of matches) {
if (fixture._matcher !== null) return fixture;
if (!matches.length) {
const index = this._fixtures.findIndex(f => f._matcher === null);

if (index >= 0) return this._fixtures[index];

throw new Error('Unable to find a matching fixture for the current request and no fixture is set as fallback');
}

if (matches.length > 1) {
console.warn(`FMF : Server found ${matches.length} fixtures matching the request "${request.url}". Using the first one.`); // eslint-disable-line
}

return matches[0];
}

async _processRequest(request, init) {
// Build FMFRequest object
request = new FMFRequest(request, init);
try {
// Build FMFRequest object
request = new FMFRequest(request, init);

// Locate matching fixture
let fixture = await this._findFixture(request);

// Locate matching fixture
let fixture = await this._findFixture(request);
// Prepare response
let response = await fixture.getResponse(request);

let response = await fixture.getResponse(request);
// Store request in history
this.history.push(request.clone(), response.clone());

// Store request in history
this.history.push(request.clone(), response.clone());
return response;
} catch (err) {
if (this._onError === 'throw') throw err;

return response;
return new Response(err.toString(), {
'content-type': 'text/html',
status: 500,
statusText: 'FMF error'
})
}
}

get request() {
Expand Down
28 changes: 28 additions & 0 deletions tests/units/errors.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Server} from '@';

const server = new Server();

describe('Errors management', function() {
before(() => server.start())
beforeEach(() => server.reset())
after(() => server.stop())

describe('Server errors', function() {
it('should be set to throw on error by default', async function() {
try {
await fetch();
expect.fail('Error not thrown');
} catch (err) {
err.should.be.instanceof(Error);
}
})

it('should be set to respond with 500 error when failing', async function() {
server.throwOnError(false);

const response = await fetch();

response.status.should.equal(500);
})
})
})

0 comments on commit 2c3f42d

Please sign in to comment.