Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
test: integrates new implementation into old test suits
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed May 16, 2019
1 parent 148d074 commit 4bf7781
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { assert } = require('chai');
const validateRequest = require('../../validateRequest');
const validateElement = require('../../validateElement');

describe('validateRequest', () => {
describe('validateElement', () => {
describe('with matching requests', () => {
const request = {
method: 'POST',
Expand All @@ -10,7 +10,7 @@ describe('validateRequest', () => {
},
body: '{ "foo": "bar" }'
};
const res = validateRequest(request, request);
const res = validateElement(request, request);

it('returns validation result object', () => {
assert.isObject(res);
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('validateRequest', () => {
});

describe('with non-matching requests', () => {
const res = validateRequest(
const res = validateElement(
{
method: 'POST',
headers: {
Expand All @@ -86,14 +86,17 @@ describe('validateRequest', () => {
});

it('contains all validatable keys', () => {
assert.hasAllKeys(res, ['headers', 'body']);
// Note that "headers" are not present because
// Gavel demands a validatable key to be present
// in both real and expected elements.
assert.hasAllKeys(res, ['body']);
});

describe('method', () => {
it.skip('compares methods');
});

describe('headers', () => {
describe.skip('headers', () => {
it('has no validator set', () => {
assert.propertyVal(res.headers, 'validator', null);
});
Expand Down Expand Up @@ -165,4 +168,8 @@ and expected data media type
});
});
});

describe('with matching responses', () => {
it.skip('add');
});
});
23 changes: 23 additions & 0 deletions lib/api/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const normalize = require('./units/normalize');
const validateElement = require('./validateElement');

function validate(real, expected, type, callback) {
const normalizedReal = normalize(real);
const normalizedExpected = normalize(expected);

if (type !== 'request' && type !== 'response') {
throw new Error(
`Can't validate: expected transaction "type" to be "request" or "response", but got: ${type}.`
);
}

const results = validateElement(normalizedReal, normalizedExpected);

// TODO Propagate the error.
callback(null, {
version: '2',
...results
});
}

module.exports = validate;
27 changes: 27 additions & 0 deletions lib/api/validateElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const isset = require('../utils/isset');
const validateStatusCode = require('./units/validateStatusCode');
const validateHeaders = require('./units/validateHeaders');
const { validateBody } = require('./units/validateBody');

function validateElement(real, expected) {
const results = {};

if (real.statusCode) {
results.statusCode = validateStatusCode(real, expected);
}

if (real.headers && expected.headers) {
results.headers = validateHeaders(real, expected);
}

if (
isset(real.body) &&
(isset(expected.body) || isset(expected.bodySchema))
) {
results.body = validateBody(real, expected);
}

return results;
}

module.exports = validateElement;
11 changes: 0 additions & 11 deletions lib/api/validateRequest.js

This file was deleted.

3 changes: 2 additions & 1 deletion lib/gavel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { HttpRequest, ExpectedHttpRequest } = require('./model/http-request');
const { HttpResponse, ExpectedHttpResponse } = require('./model/http-response');

const { validate, isValid, isValidatable } = require('./validate');
const { isValid, isValidatable } = require('./validate');
const validate = require('./api/validate');

module.exports = {
HttpRequest,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"lint": "eslint lib/**/*.js test/**/*.js",
"test": "npm run test:server && npm run test:browser && npm run test:features",
"test": "npm run test:new && npm run test:server && npm run test:browser && npm run test:features",
"test:new": "mocha \"lib/api/test/**/*.test.js\"",
"test:server": "mocha \"test/unit/**/*-test.js\"",
"test:server:coverage": "nyc npm run test:server",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/validate-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { assert } = require('chai');
const clone = require('clone');
const { validate, isValid, isValidatable } = require('../../lib/validate');
const { validate, isValid, isValidatable } = require('../../lib/gavel');

describe('Gavel proxies to functions with callbacks', () => {
// Examples from README.md
Expand Down

0 comments on commit 4bf7781

Please sign in to comment.