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

Commit

Permalink
refactor: adjusts "validateHeaders" to new JsonSchema call signature
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-zakharchenko committed Nov 29, 2019
1 parent cb8c01a commit f9f9357
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 95 deletions.
6 changes: 3 additions & 3 deletions lib/units/validateHeaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ function validateHeaders(expected, actual) {
expectedType === APIARY_JSON_HEADER_TYPE;

const validator = hasJsonHeaders
? new HeadersJsonExample(values.expected, values.actual)
? new HeadersJsonExample(values.expected)
: null;

// if you don't call ".validate()", it never evaluates any results.
validator && validator.validate();
const validationErrors = validator && validator.validate(values.actual);

if (validator) {
errors.push(...validator.evaluateOutputToResults());
errors.push(...validationErrors);
} else {
errors.push({
message: `\
Expand Down
42 changes: 20 additions & 22 deletions lib/validators/headers-json-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
} = require('../utils/schema-v4-generator');
const tv4ToHeadersMessage = require('../utils/tv4-to-headers-message');

const prepareHeaders = (headers) => {
const resolveHeaders = (headers) => {
if (typeof headers !== 'object') {
return headers;
}
Expand Down Expand Up @@ -38,42 +38,40 @@ const getSchema = (json) => {
};

class HeadersJsonExample extends JsonSchema {
constructor(expected, actual) {
if (typeof actual !== 'object') {
throw new errors.MalformedDataError('Actual is not an Object');
}

constructor(expected) {
if (typeof expected !== 'object') {
throw new errors.MalformedDataError('Expected is not an Object');
}

const preparedExpected = prepareHeaders(expected);
const preparedActual = prepareHeaders(actual);
const preparedSchema = getSchema(preparedExpected);
const resolvedExpected = resolveHeaders(expected);
const resolvedJsonSchema = getSchema(resolvedExpected);

if (preparedSchema && preparedSchema.properties) {
if (resolvedJsonSchema && resolvedJsonSchema.properties) {
const skippedHeaders = ['date', 'expires'];
skippedHeaders.forEach((headerName) => {
if (preparedSchema.properties[headerName]) {
delete preparedSchema.properties[headerName].enum;
if (resolvedJsonSchema.properties[headerName]) {
delete resolvedJsonSchema.properties[headerName].enum;
}
});
}

super(preparedSchema, preparedActual);
super(resolvedJsonSchema);

this.expected = preparedExpected;
this.actual = preparedActual;
this.schema = preparedSchema;
this.expected = resolvedExpected;
this.jsonSchema = resolvedJsonSchema;
}

validate() {
const result = super.validate();
validate(data) {
if (typeof data !== 'object') {
throw new errors.MalformedDataError('Actual is not an Object');
}

const results = super.validate(data);

if (result.length > 0) {
const resultCopy = clone(result, false);
if (results.length > 0) {
const resultCopy = clone(results, false);

for (let i = 0; i < result.length; i++) {
for (let i = 0; i < results.length; i++) {
resultCopy[i].message = tv4ToHeadersMessage(
resultCopy[i].message,
this.expected
Expand All @@ -83,7 +81,7 @@ class HeadersJsonExample extends JsonSchema {
return resultCopy;
}

return result;
return results;
}
}

Expand Down
90 changes: 36 additions & 54 deletions test/unit/validators/headers-json-example-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('HeadersJsonExample', () => {
describe('when I provede real data as non obejct', () => {
it('should throw an exception', () => {
const fn = () => {
headersValidator = new HeadersJsonExample({ header1: 'value1' }, '');
headersValidator = new HeadersJsonExample({ header1: 'value1' });
};
assert.throw(fn, 'is not an Object');
});
Expand All @@ -25,7 +25,7 @@ describe('HeadersJsonExample', () => {
describe('when I provede expected data as non obejct', () => {
it('should throw an exception', () => {
const fn = () => {
headersValidator = new HeadersJsonExample('', { header1: 'value1' });
headersValidator = new HeadersJsonExample('');
};
assert.throw(fn, 'is not an Object');
});
Expand All @@ -45,89 +45,76 @@ describe('HeadersJsonExample', () => {

describe('when provided real and expected headers are the same', () => {
before(() => {
headersValidator = new HeadersJsonExample(
fixtures.sampleHeaders,
fixtures.sampleHeaders
);
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders);
});

describe('and i run validate()', () => {
it("shouldn't return any errors", () => {
result = headersValidator.validate();
result = headersValidator.validate(fixtures.sampleHeaders);
assert.equal(result.length, 0);
});
});
});

describe('when provided real and expected headers differ in upper/lower-case state of keys', () => {
before(() => {
headersValidator = new HeadersJsonExample(
fixtures.sampleHeaders,
fixtures.sampleHeadersMixedCase
);
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders);
});

describe('and I run validate()', () => {
it("shouldn't return any errors", () => {
result = headersValidator.validate();
assert.equal(result.length, 0);
errors = headersValidator.validate(fixtures.sampleHeadersMixedCase);
assert.equal(errors.length, 0);
});
});
});

describe('when provided real and expected headers differ in one value (real change) of a key different by upper/lower', () => {
before(() => {
headersValidator = new HeadersJsonExample(
fixtures.sampleHeaders,
fixtures.sampleHeadersMixedCaseDiffers
);
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders);
});
describe('and I run validate()', () => {
it('should not return error', () => {
result = headersValidator.validate();
assert.lengthOf(result, 0);
errors = headersValidator.validate(
fixtures.sampleHeadersMixedCaseDiffers
);
assert.lengthOf(errors, 0);
});
});
});

describe('when key is missing in provided headers', () => {
beforeEach(() => {
headersValidator = new HeadersJsonExample(
fixtures.sampleHeaders,
fixtures.sampleHeadersMissing
);
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders);
});
describe('and i run validate()', () => {
it('should return 1 error', () => {
result = headersValidator.validate();
assert.equal(result.length, 1);
errors = headersValidator.validate(fixtures.sampleHeadersMissing);
assert.equal(errors.length, 1);
});

it('should have beautiful error message', () => {
result = headersValidator.validate();
assert.equal(result[0].message, "Header 'header2' is missing");
errors = headersValidator.validate(fixtures.sampleHeadersMissing);
assert.equal(errors[0].message, "Header 'header2' is missing");
});
});
});

describe('when value of content negotiation header in provided headers differs', () => {
beforeEach(() => {
headersValidator = new HeadersJsonExample(
fixtures.sampleHeaders,
fixtures.sampleHeadersDiffers
);
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders);
});

describe('and i run validate()', () => {
it('should return 1 errors', () => {
result = headersValidator.validate();
assert.equal(result.length, 1);
errors = headersValidator.validate(fixtures.sampleHeadersDiffers);
assert.equal(errors.length, 1);
});

it('should have beautiful error message', () => {
result = headersValidator.validate();
errors = headersValidator.validate(fixtures.sampleHeadersDiffers);
assert.equal(
result[0].message,
errors[0].message,
"Header 'content-type' has value 'application/fancy-madiatype' instead of 'application/json'"
);
});
Expand All @@ -137,61 +124,56 @@ describe('HeadersJsonExample', () => {

describe('when key is added to provided headers', () => {
before(() => {
headersValidator = new HeadersJsonExample(
fixtures.sampleHeaders,
fixtures.sampleHeadersAdded
);
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders);
});

describe('and i run validate()', () => {
it("shouldn't return any errors", () => {
result = headersValidator.validate();
assert.equal(result.length, 0);
errors = headersValidator.validate(fixtures.sampleHeadersAdded);
assert.equal(errors.length, 0);
});
});
});

describe('when real is empty object and expected is proper object', () => {
before(() => {
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders, {});
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders);
});

describe('and i run validate()', () => {
it('should return 2 errors', () => {
result = headersValidator.validate();
assert.equal(result.length, 2);
errors = headersValidator.validate({});
assert.equal(errors.length, 2);
});
});
});

describe('when non content negotiation header header values differs', () => {
before(() => {
headersValidator = new HeadersJsonExample(
fixtures.sampleHeadersNonContentNegotiation,
fixtures.sampleHeadersWithNonContentNegotiationChanged
fixtures.sampleHeadersNonContentNegotiation
);
});

describe('and i run validate()', () => {
it("shouldn't return any errors", () => {
result = headersValidator.validate();
assert.equal(result.length, 0);
errors = headersValidator.validate(
fixtures.sampleHeadersWithNonContentNegotiationChanged
);
assert.equal(errors.length, 0);
});
});
});

describe('#validate()', () => {
output = null;
before(() => {
headersValidator = new HeadersJsonExample(
fixtures.sampleHeaders,
fixtures.sampleHeadersMissing
);
output = headersValidator.validate();
headersValidator = new HeadersJsonExample(fixtures.sampleHeaders);
errors = headersValidator.validate(fixtures.sampleHeadersMissing);
});

it('should return an obejct', () => {
assert.isObject(output);
assert.isObject(errors);
});
});

Expand Down
30 changes: 14 additions & 16 deletions test/unit/validators/text-diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('TextDiff', () => {
describe('when expected non-string data', () => {
it('should throw an exception', () => {
const fn = () => {
new TextDiff(null, '');
new TextDiff(null);
};
expect(fn).to.throw();
});
Expand All @@ -15,7 +15,7 @@ describe('TextDiff', () => {
describe('when given non-string actual data', () => {
it('should throw an exception', () => {
const fn = () => {
new TextDiff('', null);
new TextDiff('');
};
expect(fn).to.throw();
});
Expand All @@ -25,35 +25,34 @@ describe('TextDiff', () => {
const expected = 'Iñtërnâtiônàlizætiøn☃';

it('should resolve on matching actual string', () => {
const validator = new TextDiff(expected, expected);
expect(validator.validate()).to.be.true;
const validator = new TextDiff(expected);
expect(validator.validate(expected)).to.be.true;
});

it('should reject on non-matching actual string', () => {
const validator = new TextDiff(expected, 'Nâtiônàl');
expect(validator.validate()).to.be.false;
const validator = new TextDiff(expected);
expect(validator.validate('Nâtiônàl')).to.be.false;
});
});

describe('when expected textual data', () => {
const expected = 'john';

it('should resolve when given matching actual data', () => {
const validator = new TextDiff(expected, 'john');
expect(validator.validate()).to.be.true;
const validator = new TextDiff(expected);
expect(validator.validate('john')).to.be.true;
});

it('should reject when given non-matching actual data', () => {
const validator = new TextDiff(expected, 'barry');
expect(validator.validate()).to.be.false;
const validator = new TextDiff(expected);
expect(validator.validate('barry')).to.be.false;
});
});

describe('when evaluating output to results', () => {
describe('when expected and actual data match', () => {
const validator = new TextDiff('john', 'john');
validator.validate();
const result = validator.evaluateOutputToResults();
const validator = new TextDiff('john');
const result = validator.validate('john');

it('should return an empty array', () => {
expect(result).to.be.instanceOf(Array);
Expand All @@ -62,9 +61,8 @@ describe('TextDiff', () => {
});

describe('when expected and actual data do not match', () => {
const validator = new TextDiff('john', 'barry');
validator.validate();
const result = validator.evaluateOutputToResults();
const validator = new TextDiff('john');
const result = validator.validate('barry');

it('should return an array', () => {
expect(result).to.be.instanceOf(Array);
Expand Down

0 comments on commit f9f9357

Please sign in to comment.