-
-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathJSONResponse_test.js
More file actions
171 lines (150 loc) · 5.06 KB
/
JSONResponse_test.js
File metadata and controls
171 lines (150 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const chai = require('chai')
const expect = chai.expect
const joi = require('joi')
const JSONResponse = require('../../lib/helper/JSONResponse')
const Container = require('../../lib/container')
global.codeceptjs = require('../../lib')
const data = {
posts: [
{
id: 1,
title: 'json-server',
author: 'davert',
},
{
id: 2,
},
],
user: {
name: 'davert',
},
}
let restHelper
let I
describe('JSONResponse', () => {
beforeEach(() => {
Container.create({
helpers: {
REST: {},
},
})
I = new JSONResponse()
I._beforeSuite()
restHelper = Container.helpers('REST')
})
describe('response codes', () => {
it('should check 200x codes', async () => {
restHelper.config.onResponse({ status: 204 })
I.seeResponseCodeIs(204)
I.dontSeeResponseCodeIs(200)
I.seeResponseCodeIsSuccessful()
})
it('should check 300x codes', async () => {
restHelper.config.onResponse({ status: 304 })
I.seeResponseCodeIs(304)
I.dontSeeResponseCodeIs(200)
I.seeResponseCodeIsRedirection()
})
it('should check 400x codes', async () => {
restHelper.config.onResponse({ status: 404 })
I.seeResponseCodeIs(404)
I.dontSeeResponseCodeIs(200)
I.seeResponseCodeIsClientError()
})
it('should check 500x codes', async () => {
restHelper.config.onResponse({ status: 504 })
I.seeResponseCodeIs(504)
I.dontSeeResponseCodeIs(200)
I.seeResponseCodeIsServerError()
})
it('should throw error on invalid code', () => {
restHelper.config.onResponse({ status: 504 })
expect(() => I.seeResponseCodeIs(200)).to.throw('Response code')
})
})
describe('response data', () => {
it('should check for json inclusion', () => {
restHelper.config.onResponse({ data })
I.seeResponseContainsJson({
posts: [{ id: 2 }],
})
I.seeResponseContainsJson({
posts: [{ id: 1, author: 'davert' }],
})
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('No matching element found in array for {"id":2,"author":"boss"}')
})
it('should check for json inclusion - returned Array', () => {
const arrayData = [{ ...data }]
restHelper.config.onResponse({ data: arrayData })
I.seeResponseContainsJson({
posts: [{ id: 2 }],
})
I.seeResponseContainsJson({
posts: [{ id: 1, author: 'davert' }],
})
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('No elements in array matched {"posts":[{"id":2,"author":"boss"}]}')
})
it('should check for json inclusion - returned Array of 2 items', () => {
const arrayData = [{ ...data }, { posts: { id: 3 } }]
restHelper.config.onResponse({ data: arrayData })
I.seeResponseContainsJson({
posts: { id: 3 },
})
})
it('should simply check for json inclusion', () => {
restHelper.config.onResponse({ data: { user: { name: 'jon', email: 'jon@doe.com' } } })
I.seeResponseContainsJson({ user: { name: 'jon' } })
I.dontSeeResponseContainsJson({ user: { name: 'jo' } })
I.dontSeeResponseContainsJson({ name: 'joe' })
})
it('should simply check for json inclusion - returned Array', () => {
restHelper.config.onResponse({ data: [{ user: { name: 'jon', email: 'jon@doe.com' } }] })
I.seeResponseContainsJson({ user: { name: 'jon' } })
I.dontSeeResponseContainsJson({ user: { name: 'jo' } })
I.dontSeeResponseContainsJson({ name: 'joe' })
})
it('should simply check for json equality', () => {
restHelper.config.onResponse({ data: { user: 1 } })
I.seeResponseEquals({ user: 1 })
})
it('should simply check for json equality - returned Array', () => {
restHelper.config.onResponse({ data: [{ user: 1 }] })
I.seeResponseEquals([{ user: 1 }])
})
it('should check json contains keys', () => {
restHelper.config.onResponse({ data: { user: 1, post: 2 } })
I.seeResponseContainsKeys(['user', 'post'])
})
it('should check json contains keys - returned Array', () => {
restHelper.config.onResponse({ data: [{ user: 1, post: 2 }] })
I.seeResponseContainsKeys(['user', 'post'])
})
it('should check for json by callback', () => {
restHelper.config.onResponse({ data })
const fn = ({ assert, data }) => {
assert('posts' in data)
assert('user' in data)
}
I.seeResponseValidByCallback(fn)
expect(fn.toString()).to.include("assert('posts' in data)")
})
it('should check for json by joi schema', () => {
restHelper.config.onResponse({ data })
const schema = joi.object({
posts: joi.array().items({
id: joi.number(),
author: joi.string(),
title: joi.string(),
}),
user: joi.object({
name: joi.string(),
}),
})
const fn = () => {
return schema
}
I.seeResponseMatchesJsonSchema(fn)
I.seeResponseMatchesJsonSchema(schema)
})
})
})