diff --git a/lib/gavel.js b/lib/gavel.js index b9b859f2..353cc79b 100644 --- a/lib/gavel.js +++ b/lib/gavel.js @@ -1,15 +1,17 @@ const { HttpRequest, ExpectedHttpRequest } = require('./model/http-request'); const { HttpResponse, ExpectedHttpResponse } = require('./model/http-response'); - const { isValid, isValidatable } = require('./validate'); -const validate = require('./api/validate'); +const validate = require('./next/validate'); module.exports = { + // next + validate, + + // legacy HttpRequest, HttpResponse, ExpectedHttpRequest, ExpectedHttpResponse, - validate, isValid, isValidatable }; diff --git a/lib/api/test/integration/validateMessage.test.js b/lib/next/test/integration/validateMessage.test.js similarity index 100% rename from lib/api/test/integration/validateMessage.test.js rename to lib/next/test/integration/validateMessage.test.js diff --git a/lib/api/test/unit/units/normalize/normalizeHeaders.test.js b/lib/next/test/unit/units/normalize/normalizeHeaders.test.js similarity index 100% rename from lib/api/test/unit/units/normalize/normalizeHeaders.test.js rename to lib/next/test/unit/units/normalize/normalizeHeaders.test.js diff --git a/lib/api/test/unit/units/normalize/normalizeStatusCode.test.js b/lib/next/test/unit/units/normalize/normalizeStatusCode.test.js similarity index 100% rename from lib/api/test/unit/units/normalize/normalizeStatusCode.test.js rename to lib/next/test/unit/units/normalize/normalizeStatusCode.test.js diff --git a/lib/api/test/unit/units/validateBody.test.js b/lib/next/test/unit/units/validateBody.test.js similarity index 100% rename from lib/api/test/unit/units/validateBody.test.js rename to lib/next/test/unit/units/validateBody.test.js diff --git a/lib/api/test/unit/units/validateBody/getBodySchemaType.test.js b/lib/next/test/unit/units/validateBody/getBodySchemaType.test.js similarity index 100% rename from lib/api/test/unit/units/validateBody/getBodySchemaType.test.js rename to lib/next/test/unit/units/validateBody/getBodySchemaType.test.js diff --git a/lib/api/test/unit/units/validateBody/getBodyType.test.js b/lib/next/test/unit/units/validateBody/getBodyType.test.js similarity index 100% rename from lib/api/test/unit/units/validateBody/getBodyType.test.js rename to lib/next/test/unit/units/validateBody/getBodyType.test.js diff --git a/lib/api/test/unit/units/validateBody/getBodyValidator.test.js b/lib/next/test/unit/units/validateBody/getBodyValidator.test.js similarity index 100% rename from lib/api/test/unit/units/validateBody/getBodyValidator.test.js rename to lib/next/test/unit/units/validateBody/getBodyValidator.test.js diff --git a/lib/api/test/unit/units/validateBody/isJson.test.js b/lib/next/test/unit/units/validateBody/isJson.test.js similarity index 100% rename from lib/api/test/unit/units/validateBody/isJson.test.js rename to lib/next/test/unit/units/validateBody/isJson.test.js diff --git a/lib/api/test/unit/units/validateBody/isJsonContentType.test.js b/lib/next/test/unit/units/validateBody/isJsonContentType.test.js similarity index 100% rename from lib/api/test/unit/units/validateBody/isJsonContentType.test.js rename to lib/next/test/unit/units/validateBody/isJsonContentType.test.js diff --git a/lib/api/test/unit/units/validateBody/isJsonSchema.test.js b/lib/next/test/unit/units/validateBody/isJsonSchema.test.js similarity index 100% rename from lib/api/test/unit/units/validateBody/isJsonSchema.test.js rename to lib/next/test/unit/units/validateBody/isJsonSchema.test.js diff --git a/lib/api/test/unit/units/validateHeaders.test.js b/lib/next/test/unit/units/validateHeaders.test.js similarity index 100% rename from lib/api/test/unit/units/validateHeaders.test.js rename to lib/next/test/unit/units/validateHeaders.test.js diff --git a/lib/api/test/unit/units/validateStatusCode.test.js b/lib/next/test/unit/units/validateStatusCode.test.js similarity index 100% rename from lib/api/test/unit/units/validateStatusCode.test.js rename to lib/next/test/unit/units/validateStatusCode.test.js diff --git a/lib/api/test/unit/utils/evolve.test.js b/lib/next/test/unit/utils/evolve.test.js similarity index 100% rename from lib/api/test/unit/utils/evolve.test.js rename to lib/next/test/unit/utils/evolve.test.js diff --git a/lib/api/units/normalize/index.js b/lib/next/units/normalize/index.js similarity index 100% rename from lib/api/units/normalize/index.js rename to lib/next/units/normalize/index.js diff --git a/lib/api/units/normalize/normalizeHeaders.js b/lib/next/units/normalize/normalizeHeaders.js similarity index 100% rename from lib/api/units/normalize/normalizeHeaders.js rename to lib/next/units/normalize/normalizeHeaders.js diff --git a/lib/api/units/normalize/normalizeStatusCode.js b/lib/next/units/normalize/normalizeStatusCode.js similarity index 100% rename from lib/api/units/normalize/normalizeStatusCode.js rename to lib/next/units/normalize/normalizeStatusCode.js diff --git a/lib/api/units/validateBody.js b/lib/next/units/validateBody.js similarity index 100% rename from lib/api/units/validateBody.js rename to lib/next/units/validateBody.js diff --git a/lib/api/units/validateHeaders.js b/lib/next/units/validateHeaders.js similarity index 100% rename from lib/api/units/validateHeaders.js rename to lib/next/units/validateHeaders.js diff --git a/lib/api/units/validateStatusCode.js b/lib/next/units/validateStatusCode.js similarity index 100% rename from lib/api/units/validateStatusCode.js rename to lib/next/units/validateStatusCode.js diff --git a/lib/api/utils/evolve.js b/lib/next/utils/evolve.js similarity index 100% rename from lib/api/utils/evolve.js rename to lib/next/utils/evolve.js diff --git a/lib/api/validate.js b/lib/next/validate.js similarity index 67% rename from lib/api/validate.js rename to lib/next/validate.js index b149fa00..14a6859a 100644 --- a/lib/api/validate.js +++ b/lib/next/validate.js @@ -1,5 +1,13 @@ const { validateMessage } = require('./validateMessage'); +/** + * Validates the given HTTP messages pair and returns + * a legacy-compliant validation results. + * @param {Object} real + * @param {Object} expected + * @param {'request'|'response'} type + * @param {(error: Error, result: Object) => void} callback + */ function validate(real, expected, type, callback) { if (type !== 'request' && type !== 'response') { throw new Error( diff --git a/lib/api/validateMessage.js b/lib/next/validateMessage.js similarity index 100% rename from lib/api/validateMessage.js rename to lib/next/validateMessage.js diff --git a/lib/validate.js b/lib/validate.js index 184612fc..2c6ea453 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -19,15 +19,22 @@ function proxy(validatableObject, method, cb) { * @param {'request'|'response'} type */ function getValidatableObject(real, expected, type) { + let request; + let response; + switch (type) { case 'request': - const request = new HttpRequest(real); + request = new HttpRequest(real); request.expected = new ExpectedHttpRequest(expected); return request; case 'response': - const response = new HttpResponse(real); + response = new HttpResponse(real); response.expected = new ExpectedHttpResponse(expected); return response; + default: + throw new Error( + `Can't validate: expected HTTP message type to be "request" or "response", but got: ${type}.` + ); } } diff --git a/package.json b/package.json index 539e588c..99a36d3c 100644 --- a/package.json +++ b/package.json @@ -11,15 +11,15 @@ }, "scripts": { "lint": "eslint lib/**/*.js test/**/*.js", - "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": "npm run test:next && npm run test:server && npm run test:browser && npm run test:features", + "test:next": "mocha \"lib/next/test/**/*.test.js\"", "test:server": "mocha \"test/unit/**/*-test.js\"", "test:server:coverage": "nyc npm run test:server", "test:browser": "mochify \"test/unit/**/*.js\"", "test:features": "node scripts/cucumber.js", "coveralls": "nyc --reporter=text-lcov npm run test:server | coveralls", "ci:lint": "npm run lint", - "ci:test": "npm run coveralls && npm run test:new && npm run test:browser && npm run test:features", + "ci:test": "npm run coveralls && npm run test:next && npm run test:browser && npm run test:features", "ci:release": "semantic-release", "precommit": "lint-staged" },