Skip to content

Commit

Permalink
Issue-30: improve logging and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dtobe committed Oct 30, 2020
1 parent 49f7746 commit 1346fd7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/configValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const schema = {

module.exports = (responseConfig, resourcePath, method) => {
if (!SUPPORTED_METHODS.includes(method)) {
throw new Error(`Method ${method} is not supported. Path '${resourcePath}'-${method} will be skipped.`);
throw new Error(`Method ${method} is not supported.`);
}
var ajv = new Ajv({ schemas: [schema, responseSchema] });
var validate = ajv.getSchema('ResponseDefs.json');
Expand All @@ -55,6 +55,6 @@ module.exports = (responseConfig, resourcePath, method) => {
const prop = validate.errors[0].propertyName;
const path = validate.errors[0].dataPath;
const msg = validate.errors[0].message;
throw new Error(`Validation error: ${prop ? `property [${prop}] at path ` : ''}'${path}' ${msg}`);
throw new Error(`Validation error: ${prop ? `property [${prop}] at path ` : ''}'${path}' ${msg}.`);
}
};
34 changes: 16 additions & 18 deletions mock-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,22 @@ class AietesServer {
}

let numCalls = 0;
if (matchingPathStats) {
const statList = _.flatMap(matchingPathStats, (statBlock) => {
return _.filter(statBlock, (stats, method) => {
if (Array.isArray(methodMatcher)) {
return methodMatcher.map(value => value.toLowerCase()).includes(method);
} else {
return method === methodMatcher.toLowerCase();
}
})
.map((stats) => {
return stats.numCalls;
});
});
const statList = _.flatMap(matchingPathStats, (statBlock) => {
return _.filter(statBlock, (stats, method) => {
if (Array.isArray(methodMatcher)) {
return methodMatcher.map(value => value.toLowerCase()).includes(method);
} else {
return method === methodMatcher.toLowerCase();
}
})
.map((stats) => {
return stats.numCalls;
});
});

numCalls = _.reduce(statList, function(sum, n) {
return sum + n;
}, 0);
}
numCalls = _.reduce(statList, function(sum, n) {
return sum + n;
}, 0);

return numCalls;
}
Expand Down Expand Up @@ -138,7 +136,7 @@ const createResponses = (responsesConfig) => {
validateResponses(responses, path, methodForExpress);
return new ResponseConfig(path, methodForExpress, responses);
} catch (error) {
log.warn(error);
log.warn(`${error.message} ${methodForExpress}::${path} skipped`);
}
});
}).filter(response => {
Expand Down
43 changes: 43 additions & 0 deletions test/mock-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,47 @@ describe('AietesServer IT', () => {

expect(res.status).toBe(404);
});

describe('Configuration errors', () => {
// TBD would be nice to test but terminates jest
// it('Aietes server exits cleanly if an error is thrown at startup (port not given)', () => {
// const mockServer = new AietesServer(
// {
// '/new-endpoint': {
// get: {
// status: 200
// }
// }
// },
// undefined
// );
// mockServer.start();
// expect(mockServer.server).toBeFalsy();
// });

it('Badly configured endpoints are skipped and server startup continues', async() => {
const mockServer = new AietesServer(
{
'/faulty-endpoint': {
get: {
status: '200'
}
},
'/endpoint2': {
get: {}
}
},
await getPort()
);
mockServer.start();

expect(mockServer.server).toBeTruthy();
let res = await request(mockServer.server).get('/faulty-endpoint');
expect(res.status).toBe(404);
res = await request(mockServer.server).get('/endpoint2');
expect(res.status).toBe(200);

mockServer.stop();
});
});
});

0 comments on commit 1346fd7

Please sign in to comment.