Skip to content

Commit

Permalink
Fixed #16
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Kliment committed Nov 1, 2013
1 parent 4e91346 commit 58c52dc
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 116 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gavel",
"version": "0.2.5",
"version": "0.2.6",

This comment has been minimized.

Copy link
@Almad

Almad Nov 2, 2013

Contributor

Pedantic: version bump should be a separate commit.

"description": "Gavel HTTP validator JavaScript library",
"main": "lib/gavel.js",
"directories": {
Expand Down
68 changes: 33 additions & 35 deletions src/mixins/validatable-http-message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@ validatable =
unless typeof @body == 'string'
throw new Error "HTTP Body is not a String."

if !(@headers == undefined) and !(@headers['content-type'] == undefined) and @headers['content-type'] == 'application/json'
isJsonContentType = false

# TODO refactor to separate unit when adding more complicated logic
# e.g. for application/hal+json or or application/vnd.apiary.something
if !(@headers == undefined) and !(@headers['content-type'] == undefined)
isJsonContentType = @headers['content-type'].split(';')[0] == 'application/json'

if isJsonContentType
try
JSON.parse @body
@validation.body.realType = 'application/json'
Expand Down Expand Up @@ -163,31 +170,36 @@ validatable =
parsed = JSON.parse @expected.bodySchema
if typeof parsed != 'object' or Array.isArray parsed
message = {
message: 'JSON Schema provided, but it is not an Object'
message: 'Expected:JSON Schema provided, but it is not an Object'
severity: 'error'
}
@validation.body.results.push message
else
@validation.body.expectedType = 'application/schema+json'
catch error
message = {
message: 'JSON Schema provided, but it is not a parseable JSON'
message: 'Expected: JSON Schema provided, but it is not a parseable JSON'
severity: 'error'
}
@validation.body.results.push message
return
else
@validation.body.expectedType = 'application/schema+json'
else
if @headers != undefined and
@headers['content-type'] != undefined and
@headers['content-type'] == 'application/json'

isJsonContentType = false
# TODO refactor to separate unit when adding more complicated logic
# e.g. for application/hal+json or or application/vnd.apiary.something
if !(@expected.headers == undefined) and !(@expected.headers['content-type'] == undefined)
isJsonContentType = @expected.headers['content-type'].split(';')[0] == 'application/json'

if isJsonContentType
try
JSON.parse @expected.body
@validation.body.expectedType = 'application/json'
catch error
message = {
message: 'Content-Type is application/json but body is not a parseable JSON '
message: 'Expected: Content-Type is application/json but body is not a parseable JSON '
severity: 'error'
}
@validation.body.results.push message
Expand All @@ -205,53 +217,39 @@ validatable =
if @validation.body.results == undefined
@validation.body.results = []

if @validation.body.realType == null and
@validation.body.expectedType == null
message = {
message: 'Content-Type is application/json but body is not a parseable JSON '
severity: 'error'
}
@validation.body.results.push message
if @validation.body.realType == null and @validation.body.expectedType == null
message = {
message: 'Unknown real and expected type. No validator found.'
severity: 'error'
}
@validation.body.results.push message
else
if @validation.body.realType == 'application/json'
if @validation.body.expectedType == 'application/json'
@validation.body.validator = 'JsonExample'
else if @validation.body.expectedType == 'application/schema+json'
@validation.body.validator = 'JsonSchema'
else
message = {
message: 'No validator found for real data media type "' +
+ JSON.stringify(@validation.body.realType) +
'" and expected data media type "' +
+ JSON.stringify(@validation.body.expectedType) +
'".'
message =
message: "No validator found for real data media type '#{@validation.body.realType}' and expected data media type '#{@validation.body.expectedType}'."
severity: 'error'
}

@validation.body.results.push message

else if @validation.body.realType == 'text/plain'
if @validation.body.expectedType == 'text/plain'
@validation.body.validator = 'TextDiff'
else
message = {
message: 'No validator found for real data media type "' +
+ JSON.stringify(@validation.body.realType) +
'" and expected data media type "' +
+ JSON.stringify(@validation.body.expectedType) +
'".'
message =
message: "No validator found for real data media type '#{@validation.body.realType}' and expected data media type '#{@validation.body.expectedType}'."
severity: 'error'
}

@validation.body.results.push message

else
message = {
message: 'No validator found for real data media type "' +
+ JSON.stringify(@validation.headers.realType) +
'" and expected data media type "' +
+ JSON.stringify(@validation.headers.expectedType) +
'".'
message =
message: "No validator found for real data media type '#{@validation.body.realType}' and expected data media type '#{@validation.body.expectedType}'."
severity: 'error'
}
@validation.body.results.push message

runBodyValidator: () ->
Expand Down
175 changes: 95 additions & 80 deletions test/unit/mixins/validatable-http-message-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -427,50 +427,56 @@ describe "Http validatable mixin", () ->
assert.include message, 'String'


describe 'header content-type is an application/json', () ->
before () ->
instance = new HttpResponse {
headers: {'content-type': 'application/json'}
}
instance.validation = {}
instance.validation.body = {}
instance.validation.body.results = []
jsonContentTypes = [
'application/json',
'application/json; charset=utf-8'
]

describe 'body is a parseable JSON', () ->
before () ->
instance.body = '{"foo": "bar"}'
instance.setBodyRealType()
it 'should set real type to application/json', () ->
assert.equal instance.validation.body.realType,
'application/json'

it 'TBD: should set some warning'
jsonContentTypes.forEach (contentType) ->

describe "header content-type is '#{contentType}'", () ->
before () ->
instance = new HttpResponse {
headers: {'content-type': contentType}
}
instance.validation = {}
instance.validation.body = {}
instance.validation.body.results = []

describe 'body is not a parseable JSON', () ->
before () ->
instance.body = 'Boobooo foo bar john doe'
instance.setBodyRealType()
describe 'body is a parseable JSON', () ->
before () ->
instance.body = '{"foo": "bar"}'
instance.setBodyRealType()

it 'should set real type to application/json', () ->
assert.equal instance.validation.body.realType,
'application/json'


it 'should set realType to null', () ->
assert.equal instance.validation.body.realType,
null
describe 'body is not a parseable JSON', () ->
before () ->
instance.body = 'Boobooo foo bar john doe'
instance.setBodyRealType()

it 'should add error message to results with error severity', () ->
results = instance.validation.body.results
results.forEach (result) ->
results.push result.severity

assert.include results, 'error'
it 'should set realType to null', () ->
assert.equal instance.validation.body.realType,
null

it 'should not overwrite exitsing errors', () ->
instance.validation.body.results = [
{'message': 'Shit happen.', 'severity': 'error'}
]
before = instance.validation.body.results.length
instance.setBodyRealType()
after = instance.validation.body.results.length
assert.equal before + 1, after
it 'should add error message to results with error severity', () ->
results = instance.validation.body.results
results.forEach (result) ->
results.push result.severity

assert.include results, 'error'

it 'should not overwrite exitsing errors', () ->
instance.validation.body.results = [
{'message': 'Shit happen.', 'severity': 'error'}
]
before = instance.validation.body.results.length
instance.setBodyRealType()
after = instance.validation.body.results.length
assert.equal before + 1, after


describe 'any or no content-type header', () ->
Expand Down Expand Up @@ -593,50 +599,59 @@ describe "Http validatable mixin", () ->
null

describe 'JSON Schema for expected body is not provided', () ->
describe 'expected headers have content-type application/json', () ->
describe 'expected body is a parseable JSON', () ->
before () ->
instance = new HttpResponse {
headers:
'content-type': 'application/json'
expected:
body: "{}"
}
instance.validation = {}
instance.validation.body = {}
instance.setBodyExpectedType()

it 'should set expected type to application/json', () ->
assert.equal instance.validation.body.expectedType,
'application/json'

it 'should set no error message to result', () ->
assert.equal instance.validation.body.results.length, 0


describe 'expected body is not a parseable JSON', () ->
before () ->
instance = new HttpResponse {
headers:
'content-type': 'application/json'
expected:
body: "{Boo{Boo"
}
instance.validation = {}
instance.validation.body = {}
instance.setBodyExpectedType()

it 'should set an error message to results', () ->
results = instance.validation.body.results
results.forEach (result) ->
results.push result.severity

jsonContentTypes = [
'application/json',
'application/json; charset=utf-8'
]

jsonContentTypes.forEach (contentType) ->

describe "expected headers have content-type '#{contentType}'", () ->
describe 'expected body is a parseable JSON', () ->
before () ->
instance = new HttpResponse {
expected:
headers:
'content-type': contentType
body: "{}"
}
instance.validation = {}
instance.validation.body = {}
instance.setBodyExpectedType()

it 'should set expected type to application/json', () ->
assert.equal instance.validation.body.expectedType,
'application/json'

assert.include results, 'error'
it 'should set no error message to result', () ->
assert.equal instance.validation.body.results.length, 0

it 'should set expected type to null', () ->
assert.equal instance.validation.body.expectedType,
null

describe 'expected body is not a parseable JSON', () ->
before () ->
instance = new HttpResponse {
expected:
headers:
'content-type': contentType
body: "{Boo{Boo"
}

instance.validation = {}
instance.validation.body = {}
instance.setBodyExpectedType()

it 'should set an error message to results', () ->
results = instance.validation.body.results
results.forEach (result) ->
results.push result.severity

assert.include results, 'error'

it 'should set expected type to null', () ->
assert.equal instance.validation.body.expectedType,
null

describe 'expected headers have not content-type application/json', () ->
describe 'expected body is a parseable JSON', () ->
before () ->
Expand Down

0 comments on commit 58c52dc

Please sign in to comment.