|
| 1 | +const { assert } = require('chai'); |
| 2 | +const validateBody = require('../../units/validateBody'); |
| 3 | + |
| 4 | +describe.only('validateBody', () => { |
| 5 | + describe('when given unsupported body type', () => { |
| 6 | + const scenarios = [ |
| 7 | + { name: 'number', value: 5 }, |
| 8 | + { name: 'array', value: ['foo', 'bar'] }, |
| 9 | + { name: 'object', value: { foo: 'bar' } }, |
| 10 | + { name: 'null', value: null }, |
| 11 | + { name: 'undefined', value: undefined } |
| 12 | + ]; |
| 13 | + |
| 14 | + scenarios.forEach(({ name, value }) => { |
| 15 | + it(`errors when given ${name}`, () => { |
| 16 | + assert.throw(() => validateBody({ body: value }, { body: value })); |
| 17 | + }); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('when given supported body type', () => { |
| 22 | + describe('with explicit Content-Type header', () => { |
| 23 | + describe('application/json', () => { |
| 24 | + describe('and the body type matches content-type', () => { |
| 25 | + const res = validateBody( |
| 26 | + { |
| 27 | + body: '{ "foo": "bar" }', |
| 28 | + headers: { 'Content-Type': 'application/json' } |
| 29 | + }, |
| 30 | + { body: '{ "foo": "bar" }' } |
| 31 | + ); |
| 32 | + |
| 33 | + it('has proper real type', () => { |
| 34 | + assert.propertyVal(res, 'realType', 'application/json'); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('and the body does not match content-type', () => { |
| 39 | + const res = validateBody( |
| 40 | + { |
| 41 | + body: 'foo', |
| 42 | + headers: { 'content-type': 'application/json' } |
| 43 | + }, |
| 44 | + { body: '{ "foo": "bar" }' } |
| 45 | + ); |
| 46 | + |
| 47 | + it('uses plain/text as real type', () => { |
| 48 | + assert.propertyVal(res, 'realType', 'text/plain'); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('produces error', () => { |
| 52 | + it('has proper severity', () => { |
| 53 | + assert.propertyVal(res.results[0], 'severity', 'error'); |
| 54 | + }); |
| 55 | + it('has explanatory message', () => { |
| 56 | + assert.match( |
| 57 | + res.results[0].message, |
| 58 | + /^Real body 'Content-Type' header is 'application\/json' but body is not a parseable JSON:/ |
| 59 | + ); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('without explicit Content-Type header', () => { |
| 67 | + describe('text/plain', () => { |
| 68 | + describe('and the bodies match', () => { |
| 69 | + const res = validateBody({ body: 'foo' }, { body: 'foo' }); |
| 70 | + |
| 71 | + it('has proper real type', () => { |
| 72 | + assert.propertyVal(res, 'realType', 'text/plain'); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('and the bodies do not match', () => { |
| 77 | + const res = validateBody({ body: 'foo ' }, { body: 'bar' }); |
| 78 | + |
| 79 | + it('has proper real type', () => { |
| 80 | + assert.propertyVal(res, 'realType', 'text/plain'); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('application/json', () => { |
| 86 | + describe('and the bodies match', () => { |
| 87 | + const res = validateBody( |
| 88 | + { body: '{ "foo": "bar" }' }, |
| 89 | + { body: '{ "foo": "bar" }' } |
| 90 | + ); |
| 91 | + |
| 92 | + it('has proper real type', () => { |
| 93 | + assert.propertyVal(res, 'realType', 'application/json'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('and the bodies do not match', () => { |
| 98 | + const res = validateBody( |
| 99 | + { body: '{ "foo": "bar" }' }, |
| 100 | + { body: '{ "bar": null }' } |
| 101 | + ); |
| 102 | + |
| 103 | + it('has proper real type', () => { |
| 104 | + assert.propertyVal(res, 'realType', 'application/json'); |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments