-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy patherror-test.js
157 lines (126 loc) · 3.77 KB
/
error-test.js
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
import { module, test } from 'qunit';
import Pretender from 'pretender';
import fetch, { AbortController } from 'ember-fetch';
import {
isUnauthorizedResponse,
isForbiddenResponse,
isNotFoundResponse,
isGoneResponse,
isInvalidResponse,
isBadRequestResponse,
isServerErrorResponse,
isAbortError,
isConflictResponse
} from 'ember-fetch/errors';
module('Acceptance: Errors', function(hooks) {
var server;
hooks.beforeEach(function() {
server = new Pretender();
});
hooks.afterEach(function() {
server.shutdown();
});
test('isInvalidResponse', async function(assert) {
server.get('/invalid-response', function() {
return [
422,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'invalid-response' })
];
});
const response = await fetch('/invalid-response')
assert.ok(isInvalidResponse(response))
});
test('isUnauthorizedResponse', async function(assert) {
server.get('/unauthorized-response', function() {
return [
401,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'unauthorized-response' })
];
});
const response = await fetch('/unauthorized-response')
assert.ok(isUnauthorizedResponse(response))
});
test('isForbiddenResponse', async function(assert) {
server.get('/forbidden-response', function() {
return [
403,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'forbidden-response' })
];
});
const response = await fetch('/forbidden-response')
assert.ok(isForbiddenResponse(response))
});
test('isNotFoundResponse', async function(assert) {
server.get('/not-found-response', function() {
return [
404,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'not-found-response' })
];
});
const response = await fetch('/not-found-response')
assert.ok(isNotFoundResponse(response))
});
test('isGoneResponse', async function(assert) {
server.get('/gone-response', function() {
return [
410,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'gone-response' })
];
});
const response = await fetch('/gone-response')
assert.ok(isGoneResponse(response))
});
test('isBadRequestResponse', async function(assert) {
server.get('/bad-request-response', function() {
return [
400,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'bad-request-response' })
];
});
const response = await fetch('/bad-request-response')
assert.ok(isBadRequestResponse(response))
});
test('isServerErrorResponse', async function(assert) {
server.get('/server-error-response', function() {
return [
555,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'server-error-response' })
];
});
const response = await fetch('/server-error-response')
assert.ok(isServerErrorResponse(response))
});
test('isAbortError', async function(assert) {
server.get('/abort-error', function() {
return [
200,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'abort-error' })
];
}, 2000);
const controller = new AbortController();
const signal = controller.signal;
controller.abort()
assert.rejects(fetch('/abort-error', {signal}), function (error) {
return isAbortError(error)
})
});
test('isConflictResponse', async function(assert) {
server.get('/conflict-response', function() {
return [
409,
{ 'Content-Type': 'text/json'},
JSON.stringify({ name: 'conflict-response' })
];
});
const response = await fetch('/conflict-response')
assert.ok(isConflictResponse(response))
});
});