Validator of HTTP transactions (JavaScript implementation)
npm install gavel

Gavel tells you whether an actual HTTP message is valid against an expected HTTP message.
``bash`
npm install gavel
`bash(Optional) Record HTTP messages
curl -s --trace - http://httpbin.org/ip | curl-trace-parser > expected
curl -s --trace - http://httpbin.org/ip | curl-trace-parser > actual
curl-trace-parser.$3
`js
const gavel = require('gavel');// Define HTTP messages
const expected = {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
}
};
const actual = {
statusCode: 404,
headers: {
'Content-Type': 'application/json'
}
};
// Perform the validation
const result = gavel.validate(expected, actual);
`The code above would return the following validation
result:`js
{
valid: false,
fields: {
statusCode: {
valid: false,
kind: 'text',
values: {
expected: '200',
actual: '404'
},
errors: [
{
message: Expected status code '200', but got '404'.
}
]
},
headers: {
valid: true,
kind: 'json',
values: {
expected: {
'Content-Type': 'application/json'
},
actual: {
'Content-Type': 'application/json'
}
},
errors: []
}
}
}
`$3
> When a parsable JSON body is expected without an explicit schema the default schema is inferred.
You can describe the body expectations using JSON Schema by providing a valid schema to the
bodySchema property of the expected HTTP message:`js
const gavel = require('gavel');const expected = {
bodySchema: {
type: 'object',
properties: {
fruits: {
type: 'array',
items: {
type: 'string'
}
}
}
}
};
const actual = {
body: JSON.stringify({
fruits: ['apple', 'banana', 2]
})
};
const result = gavel.validate(expected, actual);
`The validation
result against the given JSON Schema will look as follows:`js
{
valid: false,
fields: {
body: {
valid: false,
kind: 'json',
values: {
actual: "{\"fruits\":[\"apple\",\"banana\",2]}"
},
errors: [
{
message: At '/fruits/2' Invalid type: number (expected string),
location: {
pointer: '/fruits/2'
}
}
]
}
}
}
`$3
- JSON Schema Draft 7
- JSON Schema Draft 6
- JSON Schema Draft 4
Examples
Take a look at the Gherkin specification, which describes on examples how validation of each field behaves:
method
- uri
- statusCode
- headers
- body
- bodySchemaType definitions
Gavel ships with TypeScript type definitions. Please refer to the definitions file for more details.
API
-
validate(expected: HttpMessage, actual: HttpMessage): ValidationResult`