@alterior/http 3.13.3 → 3.13.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/dist/backend.d.ts +38 -38
  2. package/dist/backend.js +38 -38
  3. package/dist/client.d.ts +2108 -2108
  4. package/dist/client.js +265 -265
  5. package/dist/client.js.map +1 -1
  6. package/dist/headers.d.ts +57 -57
  7. package/dist/headers.js +187 -187
  8. package/dist/headers.js.map +1 -1
  9. package/dist/index.d.ts +18 -18
  10. package/dist/index.js +42 -42
  11. package/dist/interceptor.d.ts +63 -63
  12. package/dist/interceptor.js +43 -43
  13. package/dist/interceptor.js.map +1 -1
  14. package/dist/module.d.ts +101 -101
  15. package/dist/module.js +188 -188
  16. package/dist/module.js.map +1 -1
  17. package/dist/params.d.ts +98 -98
  18. package/dist/params.js +184 -184
  19. package/dist/params.js.map +1 -1
  20. package/dist/request.d.ts +130 -130
  21. package/dist/request.js +260 -260
  22. package/dist/request.js.map +1 -1
  23. package/dist/response.d.ts +267 -267
  24. package/dist/response.js +165 -165
  25. package/dist/response.js.map +1 -1
  26. package/dist/server.d.ts +28 -28
  27. package/dist/server.js +119 -119
  28. package/dist/server.js.map +1 -1
  29. package/dist/test/client.test.d.ts +8 -8
  30. package/dist/test/client.test.js +163 -163
  31. package/dist/test/client.test.js.map +1 -1
  32. package/dist/test/headers.test.d.ts +1 -1
  33. package/dist/test/headers.test.js +126 -126
  34. package/dist/test/module.test.d.ts +1 -1
  35. package/dist/test/module.test.js +117 -117
  36. package/dist/test/module.test.js.map +1 -1
  37. package/dist/test/params.test.d.ts +1 -1
  38. package/dist/test/params.test.js +63 -63
  39. package/dist/test/request.test.d.ts +8 -8
  40. package/dist/test/request.test.js +159 -159
  41. package/dist/test/request.test.js.map +1 -1
  42. package/dist/test/response.test.d.ts +1 -1
  43. package/dist/test/response.test.js +79 -79
  44. package/dist/test/response.test.js.map +1 -1
  45. package/dist/test/test.d.ts +1 -1
  46. package/dist/test/test.js +16 -16
  47. package/dist/test/xhr.mock.d.ts +48 -48
  48. package/dist/test/xhr.mock.js +94 -94
  49. package/dist/test/xhr.mock.js.map +1 -1
  50. package/dist/test/xhr.test.d.ts +1 -1
  51. package/dist/test/xhr.test.js +344 -344
  52. package/dist/test/xhr.test.js.map +1 -1
  53. package/dist/test/xsrf.test.d.ts +1 -1
  54. package/dist/test/xsrf.test.js +63 -63
  55. package/dist/test/xsrf.test.js.map +1 -1
  56. package/dist/testing/api.d.ts +102 -102
  57. package/dist/testing/api.js +19 -19
  58. package/dist/testing/backend.d.ts +63 -63
  59. package/dist/testing/backend.js +141 -141
  60. package/dist/testing/backend.js.map +1 -1
  61. package/dist/testing/index.d.ts +10 -10
  62. package/dist/testing/index.js +16 -16
  63. package/dist/testing/module.d.ts +16 -16
  64. package/dist/testing/module.js +38 -38
  65. package/dist/testing/module.js.map +1 -1
  66. package/dist/testing/request.d.ts +56 -56
  67. package/dist/testing/request.js +177 -177
  68. package/dist/testing/request.js.map +1 -1
  69. package/dist/testing/request.test.d.ts +8 -8
  70. package/dist/testing/request.test.js +24 -24
  71. package/dist/xhr.d.ts +42 -42
  72. package/dist/xhr.js +298 -298
  73. package/dist/xhr.js.map +1 -1
  74. package/dist/xsrf.d.ts +37 -37
  75. package/dist/xsrf.js +55 -55
  76. package/dist/xsrf.js.map +1 -1
  77. package/dist.esm/backend.d.ts +38 -38
  78. package/dist.esm/backend.js +33 -33
  79. package/dist.esm/client.d.ts +2108 -2108
  80. package/dist.esm/client.js +262 -262
  81. package/dist.esm/client.js.map +1 -1
  82. package/dist.esm/headers.d.ts +57 -57
  83. package/dist.esm/headers.js +183 -183
  84. package/dist.esm/headers.js.map +1 -1
  85. package/dist.esm/index.d.ts +18 -18
  86. package/dist.esm/index.js +18 -18
  87. package/dist.esm/interceptor.d.ts +63 -63
  88. package/dist.esm/interceptor.js +39 -39
  89. package/dist.esm/interceptor.js.map +1 -1
  90. package/dist.esm/module.d.ts +101 -101
  91. package/dist.esm/module.js +183 -183
  92. package/dist.esm/module.js.map +1 -1
  93. package/dist.esm/params.d.ts +98 -98
  94. package/dist.esm/params.js +179 -179
  95. package/dist.esm/params.js.map +1 -1
  96. package/dist.esm/request.d.ts +130 -130
  97. package/dist.esm/request.js +256 -256
  98. package/dist.esm/request.js.map +1 -1
  99. package/dist.esm/response.d.ts +267 -267
  100. package/dist.esm/response.js +158 -158
  101. package/dist.esm/response.js.map +1 -1
  102. package/dist.esm/server.d.ts +28 -28
  103. package/dist.esm/server.js +113 -113
  104. package/dist.esm/server.js.map +1 -1
  105. package/dist.esm/test/client.test.d.ts +8 -8
  106. package/dist.esm/test/client.test.js +161 -161
  107. package/dist.esm/test/client.test.js.map +1 -1
  108. package/dist.esm/test/headers.test.d.ts +1 -1
  109. package/dist.esm/test/headers.test.js +124 -124
  110. package/dist.esm/test/module.test.d.ts +1 -1
  111. package/dist.esm/test/module.test.js +115 -115
  112. package/dist.esm/test/module.test.js.map +1 -1
  113. package/dist.esm/test/params.test.d.ts +1 -1
  114. package/dist.esm/test/params.test.js +61 -61
  115. package/dist.esm/test/request.test.d.ts +8 -8
  116. package/dist.esm/test/request.test.js +157 -157
  117. package/dist.esm/test/request.test.js.map +1 -1
  118. package/dist.esm/test/response.test.d.ts +1 -1
  119. package/dist.esm/test/response.test.js +77 -77
  120. package/dist.esm/test/response.test.js.map +1 -1
  121. package/dist.esm/test/test.d.ts +1 -1
  122. package/dist.esm/test/test.js +14 -14
  123. package/dist.esm/test/xhr.mock.d.ts +48 -48
  124. package/dist.esm/test/xhr.mock.js +88 -88
  125. package/dist.esm/test/xhr.mock.js.map +1 -1
  126. package/dist.esm/test/xhr.test.d.ts +1 -1
  127. package/dist.esm/test/xhr.test.js +342 -342
  128. package/dist.esm/test/xhr.test.js.map +1 -1
  129. package/dist.esm/test/xsrf.test.d.ts +1 -1
  130. package/dist.esm/test/xsrf.test.js +61 -61
  131. package/dist.esm/test/xsrf.test.js.map +1 -1
  132. package/dist.esm/testing/api.d.ts +102 -102
  133. package/dist.esm/testing/api.js +15 -15
  134. package/dist.esm/testing/backend.d.ts +63 -63
  135. package/dist.esm/testing/backend.js +138 -138
  136. package/dist.esm/testing/backend.js.map +1 -1
  137. package/dist.esm/testing/index.d.ts +10 -10
  138. package/dist.esm/testing/index.js +10 -10
  139. package/dist.esm/testing/module.d.ts +16 -16
  140. package/dist.esm/testing/module.js +35 -35
  141. package/dist.esm/testing/module.js.map +1 -1
  142. package/dist.esm/testing/request.d.ts +56 -56
  143. package/dist.esm/testing/request.js +173 -173
  144. package/dist.esm/testing/request.js.map +1 -1
  145. package/dist.esm/testing/request.test.d.ts +8 -8
  146. package/dist.esm/testing/request.test.js +22 -22
  147. package/dist.esm/xhr.d.ts +42 -42
  148. package/dist.esm/xhr.js +294 -294
  149. package/dist.esm/xhr.js.map +1 -1
  150. package/dist.esm/xsrf.d.ts +37 -37
  151. package/dist.esm/xsrf.js +51 -51
  152. package/dist.esm/xsrf.js.map +1 -1
  153. package/package.json +9 -9
  154. package/tsconfig.esm.tsbuildinfo +1 -0
  155. package/tsconfig.json +0 -2
  156. package/tsconfig.tsbuildinfo +1 -8046
@@ -1,343 +1,343 @@
1
- /**
2
- * @license
3
- * Copyright Google Inc. All Rights Reserved.
4
- *
5
- * Use of this source code is governed by an MIT-style license that can be
6
- * found in the LICENSE file at https://angular.io/license
7
- */
8
- import { describe, it, beforeEach } from 'razmin';
9
- import { expect } from 'chai';
10
- import { toArray } from 'rxjs/operators';
11
- import { HttpRequest } from '../request';
12
- import { HttpErrorResponse, HttpEventType, HttpResponse } from '../response';
13
- import { HttpXhrBackend } from '../xhr';
14
- import { MockXhrFactory } from './xhr.mock';
15
- function trackEvents(obs) {
16
- const events = [];
17
- obs.subscribe(event => events.push(event), err => events.push(err));
18
- return events;
19
- }
20
- const TEST_POST = new HttpRequest('POST', '/test', 'some body', {
21
- responseType: 'text',
22
- });
23
- const XSSI_PREFIX = ')]}\'\n';
24
- {
25
- describe('XhrBackend', () => {
26
- let factory = null;
27
- let backend = null;
28
- beforeEach(() => {
29
- factory = new MockXhrFactory();
30
- backend = new HttpXhrBackend(factory);
31
- });
32
- it('emits status immediately', () => {
33
- const events = trackEvents(backend.handle(TEST_POST));
34
- expect(events.length).to.equal(1);
35
- expect(events[0].type).to.equal(HttpEventType.Sent);
36
- });
37
- it('sets method, url, and responseType correctly', () => {
38
- backend.handle(TEST_POST).subscribe();
39
- expect(factory.mock.method).to.equal('POST');
40
- expect(factory.mock.responseType).to.equal('text');
41
- expect(factory.mock.url).to.equal('/test');
42
- });
43
- it('sets outgoing body correctly', () => {
44
- backend.handle(TEST_POST).subscribe();
45
- expect(factory.mock.body).to.equal('some body');
46
- });
47
- it('sets outgoing headers, including default headers', () => {
48
- const post = TEST_POST.clone({
49
- setHeaders: {
50
- 'Test': 'Test header',
51
- },
52
- });
53
- backend.handle(post).subscribe();
54
- expect(factory.mock.mockHeaders).to.eql({
55
- 'Test': 'Test header',
56
- 'Accept': 'application/json, text/plain, */*',
57
- 'Content-Type': 'text/plain',
58
- });
59
- });
60
- it('sets outgoing headers, including overriding defaults', () => {
61
- const setHeaders = {
62
- 'Test': 'Test header',
63
- 'Accept': 'text/html',
64
- 'Content-Type': 'text/css',
65
- };
66
- backend.handle(TEST_POST.clone({ setHeaders })).subscribe();
67
- expect(factory.mock.mockHeaders).to.eql(setHeaders);
68
- });
69
- it('passes withCredentials through', () => {
70
- backend.handle(TEST_POST.clone({ withCredentials: true })).subscribe();
71
- expect(factory.mock.withCredentials).to.equal(true);
72
- });
73
- it('handles a text response', () => {
74
- const events = trackEvents(backend.handle(TEST_POST));
75
- factory.mock.mockFlush(200, 'OK', 'some response');
76
- expect(events.length).to.equal(2);
77
- expect(events[1].type).to.equal(HttpEventType.Response);
78
- expect(events[1] instanceof HttpResponse).to.be.ok;
79
- const res = events[1];
80
- expect(res.body).to.equal('some response');
81
- expect(res.status).to.equal(200);
82
- expect(res.statusText).to.equal('OK');
83
- });
84
- it('handles a json response', () => {
85
- const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
86
- factory.mock.mockFlush(200, 'OK', JSON.stringify({ data: 'some data' }));
87
- expect(events.length).to.equal(2);
88
- const res = events[1];
89
- expect(res.body.data).to.equal('some data');
90
- });
91
- it('handles a blank json response', () => {
92
- const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
93
- factory.mock.mockFlush(200, 'OK', '');
94
- expect(events.length).to.equal(2);
95
- const res = events[1];
96
- expect(res.body).to.be.null;
97
- });
98
- it('handles a json error response', () => {
99
- const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
100
- factory.mock.mockFlush(500, 'Error', JSON.stringify({ data: 'some data' }));
101
- expect(events.length).to.equal(2);
102
- const res = events[1];
103
- expect(res.error.data).to.equal('some data');
104
- });
105
- it('handles a json error response with XSSI prefix', () => {
106
- const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
107
- factory.mock.mockFlush(500, 'Error', XSSI_PREFIX + JSON.stringify({ data: 'some data' }));
108
- expect(events.length).to.equal(2);
109
- const res = events[1];
110
- expect(res.error.data).to.equal('some data');
111
- });
112
- it('handles a json string response', () => {
113
- const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
114
- expect(factory.mock.responseType).to.equal('text');
115
- factory.mock.mockFlush(200, 'OK', JSON.stringify('this is a string'));
116
- expect(events.length).to.equal(2);
117
- const res = events[1];
118
- expect(res.body).to.equal('this is a string');
119
- });
120
- it('handles a json response with an XSSI prefix', () => {
121
- const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
122
- factory.mock.mockFlush(200, 'OK', XSSI_PREFIX + JSON.stringify({ data: 'some data' }));
123
- expect(events.length).to.equal(2);
124
- const res = events[1];
125
- expect(res.body.data).to.equal('some data');
126
- });
127
- it('emits unsuccessful responses via the error path', done => {
128
- backend.handle(TEST_POST).subscribe(undefined, (err) => {
129
- expect(err instanceof HttpErrorResponse).to.equal(true);
130
- expect(err.error).to.equal('this is the error');
131
- done();
132
- });
133
- factory.mock.mockFlush(400, 'Bad Request', 'this is the error');
134
- });
135
- it('emits real errors via the error path', done => {
136
- backend.handle(TEST_POST).subscribe(undefined, (err) => {
137
- expect(err instanceof HttpErrorResponse).to.equal(true);
138
- expect(err.error instanceof Error);
139
- done();
140
- });
141
- factory.mock.mockErrorEvent(new Error('blah'));
142
- });
143
- describe('progress events', () => {
144
- it('are emitted for download progress', done => {
145
- backend.handle(TEST_POST.clone({ reportProgress: true }))
146
- .pipe(toArray())
147
- .subscribe(events => {
148
- expect(events.map(event => event.type)).to.eql([
149
- HttpEventType.Sent,
150
- HttpEventType.ResponseHeader,
151
- HttpEventType.DownloadProgress,
152
- HttpEventType.DownloadProgress,
153
- HttpEventType.Response,
154
- ]);
155
- const [progress1, progress2, response] = [
156
- events[2], events[3],
157
- events[4]
158
- ];
159
- expect(progress1.partialText).to.equal('down');
160
- expect(progress1.loaded).to.equal(100);
161
- expect(progress1.total).to.equal(300);
162
- expect(progress2.partialText).to.equal('download');
163
- expect(progress2.loaded).to.equal(200);
164
- expect(progress2.total).to.equal(300);
165
- expect(response.body).to.equal('downloaded');
166
- done();
167
- });
168
- factory.mock.responseText = 'down';
169
- factory.mock.mockDownloadProgressEvent(100, 300);
170
- factory.mock.responseText = 'download';
171
- factory.mock.mockDownloadProgressEvent(200, 300);
172
- factory.mock.mockFlush(200, 'OK', 'downloaded');
173
- });
174
- it('are emitted for upload progress', done => {
175
- backend.handle(TEST_POST.clone({ reportProgress: true }))
176
- .pipe(toArray())
177
- .subscribe(events => {
178
- expect(events.map(event => event.type)).to.eql([
179
- HttpEventType.Sent,
180
- HttpEventType.UploadProgress,
181
- HttpEventType.UploadProgress,
182
- HttpEventType.Response,
183
- ]);
184
- const [progress1, progress2] = [
185
- events[1],
186
- events[2],
187
- ];
188
- expect(progress1.loaded).to.equal(100);
189
- expect(progress1.total).to.equal(300);
190
- expect(progress2.loaded).to.equal(200);
191
- expect(progress2.total).to.equal(300);
192
- done();
193
- });
194
- factory.mock.mockUploadProgressEvent(100, 300);
195
- factory.mock.mockUploadProgressEvent(200, 300);
196
- factory.mock.mockFlush(200, 'OK', 'Done');
197
- });
198
- it('are emitted when both upload and download progress are available', done => {
199
- backend.handle(TEST_POST.clone({ reportProgress: true }))
200
- .pipe(toArray())
201
- .subscribe(events => {
202
- expect(events.map(event => event.type)).to.eql([
203
- HttpEventType.Sent,
204
- HttpEventType.UploadProgress,
205
- HttpEventType.ResponseHeader,
206
- HttpEventType.DownloadProgress,
207
- HttpEventType.Response,
208
- ]);
209
- done();
210
- });
211
- factory.mock.mockUploadProgressEvent(100, 300);
212
- factory.mock.mockDownloadProgressEvent(200, 300);
213
- factory.mock.mockFlush(200, 'OK', 'Done');
214
- });
215
- it('are emitted even if length is not computable', done => {
216
- backend.handle(TEST_POST.clone({ reportProgress: true }))
217
- .pipe(toArray())
218
- .subscribe(events => {
219
- expect(events.map(event => event.type)).to.eql([
220
- HttpEventType.Sent,
221
- HttpEventType.UploadProgress,
222
- HttpEventType.ResponseHeader,
223
- HttpEventType.DownloadProgress,
224
- HttpEventType.Response,
225
- ]);
226
- done();
227
- });
228
- factory.mock.mockUploadProgressEvent(100);
229
- factory.mock.mockDownloadProgressEvent(200);
230
- factory.mock.mockFlush(200, 'OK', 'Done');
231
- });
232
- it('include ResponseHeader with headers and status', done => {
233
- backend.handle(TEST_POST.clone({ reportProgress: true }))
234
- .pipe(toArray())
235
- .subscribe(events => {
236
- expect(events.map(event => event.type)).to.eql([
237
- HttpEventType.Sent,
238
- HttpEventType.ResponseHeader,
239
- HttpEventType.DownloadProgress,
240
- HttpEventType.Response,
241
- ]);
242
- const partial = events[1];
243
- expect(partial.headers.get('Content-Type')).to.equal('text/plain');
244
- expect(partial.headers.get('Test')).to.equal('Test header');
245
- done();
246
- });
247
- factory.mock.mockResponseHeaders = 'Test: Test header\nContent-Type: text/plain\n';
248
- factory.mock.mockDownloadProgressEvent(200);
249
- factory.mock.mockFlush(200, 'OK', 'Done');
250
- });
251
- it('are unsubscribed along with the main request', () => {
252
- const sub = backend.handle(TEST_POST.clone({ reportProgress: true })).subscribe();
253
- expect(factory.mock.listeners.progress).not.to.be.undefined;
254
- sub.unsubscribe();
255
- expect(factory.mock.listeners.progress).to.be.undefined;
256
- });
257
- it('do not cause headers to be re-parsed on main response', done => {
258
- backend.handle(TEST_POST.clone({ reportProgress: true }))
259
- .pipe(toArray())
260
- .subscribe(events => {
261
- events
262
- .filter(event => event.type === HttpEventType.Response ||
263
- event.type === HttpEventType.ResponseHeader)
264
- .map(event => event)
265
- .forEach(event => {
266
- expect(event.status).to.equal(203);
267
- expect(event.headers.get('Test')).to.equal('This is a test');
268
- });
269
- done();
270
- });
271
- factory.mock.mockResponseHeaders = 'Test: This is a test\n';
272
- factory.mock.status = 203;
273
- factory.mock.mockDownloadProgressEvent(100, 300);
274
- factory.mock.mockResponseHeaders = 'Test: should never be read\n';
275
- factory.mock.mockFlush(203, 'OK', 'Testing 1 2 3');
276
- });
277
- });
278
- describe('gets response URL', () => {
279
- it('from XHR.responsesURL', done => {
280
- backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
281
- expect(events.length).to.equal(2);
282
- expect(events[1].type).to.equal(HttpEventType.Response);
283
- const response = events[1];
284
- expect(response.url).to.equal('/response/url');
285
- done();
286
- });
287
- factory.mock.responseURL = '/response/url';
288
- factory.mock.mockFlush(200, 'OK', 'Test');
289
- });
290
- it('from X-Request-URL header if XHR.responseURL is not present', done => {
291
- backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
292
- expect(events.length).to.equal(2);
293
- expect(events[1].type).to.equal(HttpEventType.Response);
294
- const response = events[1];
295
- expect(response.url).to.equal('/response/url');
296
- done();
297
- });
298
- factory.mock.mockResponseHeaders = 'X-Request-URL: /response/url\n';
299
- factory.mock.mockFlush(200, 'OK', 'Test');
300
- });
301
- it('falls back on Request.url if neither are available', done => {
302
- backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
303
- expect(events.length).to.equal(2);
304
- expect(events[1].type).to.equal(HttpEventType.Response);
305
- const response = events[1];
306
- expect(response.url).to.equal('/test');
307
- done();
308
- });
309
- factory.mock.mockFlush(200, 'OK', 'Test');
310
- });
311
- });
312
- describe('corrects for quirks', () => {
313
- it('by normalizing 1223 status to 204', done => {
314
- backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
315
- expect(events.length).to.equal(2);
316
- expect(events[1].type).to.equal(HttpEventType.Response);
317
- const response = events[1];
318
- expect(response.status).to.equal(204);
319
- done();
320
- });
321
- factory.mock.mockFlush(1223, 'IE Special Status', 'Test');
322
- });
323
- it('by normalizing 0 status to 200 if a body is present', done => {
324
- backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
325
- expect(events.length).to.equal(2);
326
- expect(events[1].type).to.equal(HttpEventType.Response);
327
- const response = events[1];
328
- expect(response.status).to.equal(200);
329
- done();
330
- });
331
- factory.mock.mockFlush(0, 'CORS 0 status', 'Test');
332
- });
333
- it('by leaving 0 status as 0 if a body is not present', done => {
334
- backend.handle(TEST_POST).pipe(toArray()).subscribe(undefined, (error) => {
335
- expect(error.status).to.equal(0);
336
- done();
337
- });
338
- factory.mock.mockFlush(0, 'CORS 0 status');
339
- });
340
- });
341
- });
342
- }
1
+ /**
2
+ * @license
3
+ * Copyright Google Inc. All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+ import { describe, it, beforeEach } from 'razmin';
9
+ import { expect } from 'chai';
10
+ import { toArray } from 'rxjs/operators';
11
+ import { HttpRequest } from '../request';
12
+ import { HttpErrorResponse, HttpEventType, HttpResponse } from '../response';
13
+ import { HttpXhrBackend } from '../xhr';
14
+ import { MockXhrFactory } from './xhr.mock';
15
+ function trackEvents(obs) {
16
+ const events = [];
17
+ obs.subscribe(event => events.push(event), err => events.push(err));
18
+ return events;
19
+ }
20
+ const TEST_POST = new HttpRequest('POST', '/test', 'some body', {
21
+ responseType: 'text',
22
+ });
23
+ const XSSI_PREFIX = ')]}\'\n';
24
+ {
25
+ describe('XhrBackend', () => {
26
+ let factory = null;
27
+ let backend = null;
28
+ beforeEach(() => {
29
+ factory = new MockXhrFactory();
30
+ backend = new HttpXhrBackend(factory);
31
+ });
32
+ it('emits status immediately', () => {
33
+ const events = trackEvents(backend.handle(TEST_POST));
34
+ expect(events.length).to.equal(1);
35
+ expect(events[0].type).to.equal(HttpEventType.Sent);
36
+ });
37
+ it('sets method, url, and responseType correctly', () => {
38
+ backend.handle(TEST_POST).subscribe();
39
+ expect(factory.mock.method).to.equal('POST');
40
+ expect(factory.mock.responseType).to.equal('text');
41
+ expect(factory.mock.url).to.equal('/test');
42
+ });
43
+ it('sets outgoing body correctly', () => {
44
+ backend.handle(TEST_POST).subscribe();
45
+ expect(factory.mock.body).to.equal('some body');
46
+ });
47
+ it('sets outgoing headers, including default headers', () => {
48
+ const post = TEST_POST.clone({
49
+ setHeaders: {
50
+ 'Test': 'Test header',
51
+ },
52
+ });
53
+ backend.handle(post).subscribe();
54
+ expect(factory.mock.mockHeaders).to.eql({
55
+ 'Test': 'Test header',
56
+ 'Accept': 'application/json, text/plain, */*',
57
+ 'Content-Type': 'text/plain',
58
+ });
59
+ });
60
+ it('sets outgoing headers, including overriding defaults', () => {
61
+ const setHeaders = {
62
+ 'Test': 'Test header',
63
+ 'Accept': 'text/html',
64
+ 'Content-Type': 'text/css',
65
+ };
66
+ backend.handle(TEST_POST.clone({ setHeaders })).subscribe();
67
+ expect(factory.mock.mockHeaders).to.eql(setHeaders);
68
+ });
69
+ it('passes withCredentials through', () => {
70
+ backend.handle(TEST_POST.clone({ withCredentials: true })).subscribe();
71
+ expect(factory.mock.withCredentials).to.equal(true);
72
+ });
73
+ it('handles a text response', () => {
74
+ const events = trackEvents(backend.handle(TEST_POST));
75
+ factory.mock.mockFlush(200, 'OK', 'some response');
76
+ expect(events.length).to.equal(2);
77
+ expect(events[1].type).to.equal(HttpEventType.Response);
78
+ expect(events[1] instanceof HttpResponse).to.be.ok;
79
+ const res = events[1];
80
+ expect(res.body).to.equal('some response');
81
+ expect(res.status).to.equal(200);
82
+ expect(res.statusText).to.equal('OK');
83
+ });
84
+ it('handles a json response', () => {
85
+ const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
86
+ factory.mock.mockFlush(200, 'OK', JSON.stringify({ data: 'some data' }));
87
+ expect(events.length).to.equal(2);
88
+ const res = events[1];
89
+ expect(res.body.data).to.equal('some data');
90
+ });
91
+ it('handles a blank json response', () => {
92
+ const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
93
+ factory.mock.mockFlush(200, 'OK', '');
94
+ expect(events.length).to.equal(2);
95
+ const res = events[1];
96
+ expect(res.body).to.be.null;
97
+ });
98
+ it('handles a json error response', () => {
99
+ const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
100
+ factory.mock.mockFlush(500, 'Error', JSON.stringify({ data: 'some data' }));
101
+ expect(events.length).to.equal(2);
102
+ const res = events[1];
103
+ expect(res.error.data).to.equal('some data');
104
+ });
105
+ it('handles a json error response with XSSI prefix', () => {
106
+ const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
107
+ factory.mock.mockFlush(500, 'Error', XSSI_PREFIX + JSON.stringify({ data: 'some data' }));
108
+ expect(events.length).to.equal(2);
109
+ const res = events[1];
110
+ expect(res.error.data).to.equal('some data');
111
+ });
112
+ it('handles a json string response', () => {
113
+ const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
114
+ expect(factory.mock.responseType).to.equal('text');
115
+ factory.mock.mockFlush(200, 'OK', JSON.stringify('this is a string'));
116
+ expect(events.length).to.equal(2);
117
+ const res = events[1];
118
+ expect(res.body).to.equal('this is a string');
119
+ });
120
+ it('handles a json response with an XSSI prefix', () => {
121
+ const events = trackEvents(backend.handle(TEST_POST.clone({ responseType: 'json' })));
122
+ factory.mock.mockFlush(200, 'OK', XSSI_PREFIX + JSON.stringify({ data: 'some data' }));
123
+ expect(events.length).to.equal(2);
124
+ const res = events[1];
125
+ expect(res.body.data).to.equal('some data');
126
+ });
127
+ it('emits unsuccessful responses via the error path', done => {
128
+ backend.handle(TEST_POST).subscribe(undefined, (err) => {
129
+ expect(err instanceof HttpErrorResponse).to.equal(true);
130
+ expect(err.error).to.equal('this is the error');
131
+ done();
132
+ });
133
+ factory.mock.mockFlush(400, 'Bad Request', 'this is the error');
134
+ });
135
+ it('emits real errors via the error path', done => {
136
+ backend.handle(TEST_POST).subscribe(undefined, (err) => {
137
+ expect(err instanceof HttpErrorResponse).to.equal(true);
138
+ expect(err.error instanceof Error);
139
+ done();
140
+ });
141
+ factory.mock.mockErrorEvent(new Error('blah'));
142
+ });
143
+ describe('progress events', () => {
144
+ it('are emitted for download progress', done => {
145
+ backend.handle(TEST_POST.clone({ reportProgress: true }))
146
+ .pipe(toArray())
147
+ .subscribe(events => {
148
+ expect(events.map(event => event.type)).to.eql([
149
+ HttpEventType.Sent,
150
+ HttpEventType.ResponseHeader,
151
+ HttpEventType.DownloadProgress,
152
+ HttpEventType.DownloadProgress,
153
+ HttpEventType.Response,
154
+ ]);
155
+ const [progress1, progress2, response] = [
156
+ events[2], events[3],
157
+ events[4]
158
+ ];
159
+ expect(progress1.partialText).to.equal('down');
160
+ expect(progress1.loaded).to.equal(100);
161
+ expect(progress1.total).to.equal(300);
162
+ expect(progress2.partialText).to.equal('download');
163
+ expect(progress2.loaded).to.equal(200);
164
+ expect(progress2.total).to.equal(300);
165
+ expect(response.body).to.equal('downloaded');
166
+ done();
167
+ });
168
+ factory.mock.responseText = 'down';
169
+ factory.mock.mockDownloadProgressEvent(100, 300);
170
+ factory.mock.responseText = 'download';
171
+ factory.mock.mockDownloadProgressEvent(200, 300);
172
+ factory.mock.mockFlush(200, 'OK', 'downloaded');
173
+ });
174
+ it('are emitted for upload progress', done => {
175
+ backend.handle(TEST_POST.clone({ reportProgress: true }))
176
+ .pipe(toArray())
177
+ .subscribe(events => {
178
+ expect(events.map(event => event.type)).to.eql([
179
+ HttpEventType.Sent,
180
+ HttpEventType.UploadProgress,
181
+ HttpEventType.UploadProgress,
182
+ HttpEventType.Response,
183
+ ]);
184
+ const [progress1, progress2] = [
185
+ events[1],
186
+ events[2],
187
+ ];
188
+ expect(progress1.loaded).to.equal(100);
189
+ expect(progress1.total).to.equal(300);
190
+ expect(progress2.loaded).to.equal(200);
191
+ expect(progress2.total).to.equal(300);
192
+ done();
193
+ });
194
+ factory.mock.mockUploadProgressEvent(100, 300);
195
+ factory.mock.mockUploadProgressEvent(200, 300);
196
+ factory.mock.mockFlush(200, 'OK', 'Done');
197
+ });
198
+ it('are emitted when both upload and download progress are available', done => {
199
+ backend.handle(TEST_POST.clone({ reportProgress: true }))
200
+ .pipe(toArray())
201
+ .subscribe(events => {
202
+ expect(events.map(event => event.type)).to.eql([
203
+ HttpEventType.Sent,
204
+ HttpEventType.UploadProgress,
205
+ HttpEventType.ResponseHeader,
206
+ HttpEventType.DownloadProgress,
207
+ HttpEventType.Response,
208
+ ]);
209
+ done();
210
+ });
211
+ factory.mock.mockUploadProgressEvent(100, 300);
212
+ factory.mock.mockDownloadProgressEvent(200, 300);
213
+ factory.mock.mockFlush(200, 'OK', 'Done');
214
+ });
215
+ it('are emitted even if length is not computable', done => {
216
+ backend.handle(TEST_POST.clone({ reportProgress: true }))
217
+ .pipe(toArray())
218
+ .subscribe(events => {
219
+ expect(events.map(event => event.type)).to.eql([
220
+ HttpEventType.Sent,
221
+ HttpEventType.UploadProgress,
222
+ HttpEventType.ResponseHeader,
223
+ HttpEventType.DownloadProgress,
224
+ HttpEventType.Response,
225
+ ]);
226
+ done();
227
+ });
228
+ factory.mock.mockUploadProgressEvent(100);
229
+ factory.mock.mockDownloadProgressEvent(200);
230
+ factory.mock.mockFlush(200, 'OK', 'Done');
231
+ });
232
+ it('include ResponseHeader with headers and status', done => {
233
+ backend.handle(TEST_POST.clone({ reportProgress: true }))
234
+ .pipe(toArray())
235
+ .subscribe(events => {
236
+ expect(events.map(event => event.type)).to.eql([
237
+ HttpEventType.Sent,
238
+ HttpEventType.ResponseHeader,
239
+ HttpEventType.DownloadProgress,
240
+ HttpEventType.Response,
241
+ ]);
242
+ const partial = events[1];
243
+ expect(partial.headers.get('Content-Type')).to.equal('text/plain');
244
+ expect(partial.headers.get('Test')).to.equal('Test header');
245
+ done();
246
+ });
247
+ factory.mock.mockResponseHeaders = 'Test: Test header\nContent-Type: text/plain\n';
248
+ factory.mock.mockDownloadProgressEvent(200);
249
+ factory.mock.mockFlush(200, 'OK', 'Done');
250
+ });
251
+ it('are unsubscribed along with the main request', () => {
252
+ const sub = backend.handle(TEST_POST.clone({ reportProgress: true })).subscribe();
253
+ expect(factory.mock.listeners.progress).not.to.be.undefined;
254
+ sub.unsubscribe();
255
+ expect(factory.mock.listeners.progress).to.be.undefined;
256
+ });
257
+ it('do not cause headers to be re-parsed on main response', done => {
258
+ backend.handle(TEST_POST.clone({ reportProgress: true }))
259
+ .pipe(toArray())
260
+ .subscribe(events => {
261
+ events
262
+ .filter(event => event.type === HttpEventType.Response ||
263
+ event.type === HttpEventType.ResponseHeader)
264
+ .map(event => event)
265
+ .forEach(event => {
266
+ expect(event.status).to.equal(203);
267
+ expect(event.headers.get('Test')).to.equal('This is a test');
268
+ });
269
+ done();
270
+ });
271
+ factory.mock.mockResponseHeaders = 'Test: This is a test\n';
272
+ factory.mock.status = 203;
273
+ factory.mock.mockDownloadProgressEvent(100, 300);
274
+ factory.mock.mockResponseHeaders = 'Test: should never be read\n';
275
+ factory.mock.mockFlush(203, 'OK', 'Testing 1 2 3');
276
+ });
277
+ });
278
+ describe('gets response URL', () => {
279
+ it('from XHR.responsesURL', done => {
280
+ backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
281
+ expect(events.length).to.equal(2);
282
+ expect(events[1].type).to.equal(HttpEventType.Response);
283
+ const response = events[1];
284
+ expect(response.url).to.equal('/response/url');
285
+ done();
286
+ });
287
+ factory.mock.responseURL = '/response/url';
288
+ factory.mock.mockFlush(200, 'OK', 'Test');
289
+ });
290
+ it('from X-Request-URL header if XHR.responseURL is not present', done => {
291
+ backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
292
+ expect(events.length).to.equal(2);
293
+ expect(events[1].type).to.equal(HttpEventType.Response);
294
+ const response = events[1];
295
+ expect(response.url).to.equal('/response/url');
296
+ done();
297
+ });
298
+ factory.mock.mockResponseHeaders = 'X-Request-URL: /response/url\n';
299
+ factory.mock.mockFlush(200, 'OK', 'Test');
300
+ });
301
+ it('falls back on Request.url if neither are available', done => {
302
+ backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
303
+ expect(events.length).to.equal(2);
304
+ expect(events[1].type).to.equal(HttpEventType.Response);
305
+ const response = events[1];
306
+ expect(response.url).to.equal('/test');
307
+ done();
308
+ });
309
+ factory.mock.mockFlush(200, 'OK', 'Test');
310
+ });
311
+ });
312
+ describe('corrects for quirks', () => {
313
+ it('by normalizing 1223 status to 204', done => {
314
+ backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
315
+ expect(events.length).to.equal(2);
316
+ expect(events[1].type).to.equal(HttpEventType.Response);
317
+ const response = events[1];
318
+ expect(response.status).to.equal(204);
319
+ done();
320
+ });
321
+ factory.mock.mockFlush(1223, 'IE Special Status', 'Test');
322
+ });
323
+ it('by normalizing 0 status to 200 if a body is present', done => {
324
+ backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
325
+ expect(events.length).to.equal(2);
326
+ expect(events[1].type).to.equal(HttpEventType.Response);
327
+ const response = events[1];
328
+ expect(response.status).to.equal(200);
329
+ done();
330
+ });
331
+ factory.mock.mockFlush(0, 'CORS 0 status', 'Test');
332
+ });
333
+ it('by leaving 0 status as 0 if a body is not present', done => {
334
+ backend.handle(TEST_POST).pipe(toArray()).subscribe(undefined, (error) => {
335
+ expect(error.status).to.equal(0);
336
+ done();
337
+ });
338
+ factory.mock.mockFlush(0, 'CORS 0 status');
339
+ });
340
+ });
341
+ });
342
+ }
343
343
  //# sourceMappingURL=xhr.test.js.map