Skip to content

Commit 9193b95

Browse files
committed
chore: add body mixin tests
1 parent f021864 commit 9193b95

File tree

7 files changed

+321
-75
lines changed

7 files changed

+321
-75
lines changed

package-lock.json

Lines changed: 89 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
"eslint": "^6.5.1",
2222
"eslint-config-prettier": "^6.15.0",
2323
"eslint-plugin-flowtype": "^5.2.0",
24+
"execa": "^5.0.0",
2425
"flow-bin": "^0.136.0",
2526
"querystring": "^0.2.0",
2627
"react-native-polyfill-globals": "^1.0.7",
2728
"react-native-test-runner": "^1.0.8"
2829
},
2930
"files": [
3031
"src",
31-
"fetch.js"
32+
"fetch.js",
33+
"fetch.js.flow"
3234
],
3335
"scripts": {
3436
"flow": "flow",

run-tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const execa = require("execa");
4+
const createServer = require("./test/server");
5+
6+
async function run() {
7+
createServer();
8+
9+
try {
10+
const result = await execa("rn-test", process.argv.slice(2), {
11+
preferLocal: true,
12+
stdio: ["ignore", "inherit", "inherit"],
13+
});
14+
process.exit(result.exitCode);
15+
} catch (error) {
16+
process.exit(error.exitCode);
17+
}
18+
}
19+
20+
run();

src/Fetch.js

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ class Fetch {
4646

4747
constructor(resource, options = {}) {
4848
this._request = new Request(resource, options);
49-
this._nativeResponseType = options.reactNative?.textStreaming
50-
? "text"
51-
: this._nativeResponseType;
5249
this._abortFn = this.__abort.bind(this);
5350
this._deferredPromise = pDefer();
5451

@@ -60,11 +57,23 @@ class Fetch {
6057
throw new AbortError();
6158
}
6259

60+
this.__setNativeResponseType(options);
6361
this.__doFetch();
6462

6563
return this._deferredPromise.promise;
6664
}
6765

66+
__setNativeResponseType(options) {
67+
if (options.textStreaming) {
68+
this._nativeResponseType = "text";
69+
70+
return;
71+
}
72+
73+
this._nativeResponseType =
74+
options.__nativeResponseType ?? this._nativeResponseType;
75+
}
76+
6877
__subscribeToNetworkEvents() {
6978
[
7079
"didReceiveNetworkResponse",
@@ -113,17 +122,18 @@ class Fetch {
113122
}
114123

115124
__didCreateRequest(requestId) {
116-
console.log("fetch __didCreateRequest", { requestId });
125+
// console.log("fetch __didCreateRequest", { requestId });
117126
this._requestId = requestId;
118127
}
119128

120129
__didReceiveNetworkResponse(requestId, status, headers, url) {
121-
console.log("fetch __didReceiveNetworkResponse", {
122-
requestId,
123-
status,
124-
headers,
125-
url,
126-
});
130+
// console.log("fetch __didReceiveNetworkResponse", {
131+
// requestId,
132+
// status,
133+
// headers,
134+
// url,
135+
// });
136+
127137
if (requestId !== this._requestId) {
128138
return;
129139
}
@@ -146,7 +156,7 @@ class Fetch {
146156
}
147157

148158
__didReceiveNetworkData(requestId, response) {
149-
console.log("fetch __didReceiveNetworkData", { requestId, response });
159+
// console.log("fetch __didReceiveNetworkData", { requestId, response });
150160
if (requestId !== this._requestId) {
151161
return;
152162
}
@@ -160,12 +170,12 @@ class Fetch {
160170
progress,
161171
total
162172
) {
163-
console.log("fetch __didReceiveNetworkIncrementalData", {
164-
requestId,
165-
responseText,
166-
progress,
167-
total,
168-
});
173+
// console.log("fetch __didReceiveNetworkIncrementalData", {
174+
// requestId,
175+
// responseText,
176+
// progress,
177+
// total,
178+
// });
169179
if (requestId !== this._requestId) {
170180
return;
171181
}
@@ -184,11 +194,12 @@ class Fetch {
184194
// }
185195

186196
async __didCompleteNetworkResponse(requestId, errorMessage, didTimeOut) {
187-
console.log("fetch __didCompleteNetworkResponse", {
188-
requestId,
189-
errorMessage,
190-
didTimeOut,
191-
});
197+
// console.log("fetch __didCompleteNetworkResponse", {
198+
// requestId,
199+
// errorMessage,
200+
// didTimeOut,
201+
// });
202+
192203
if (requestId !== this._requestId) {
193204
return;
194205
}
@@ -223,19 +234,23 @@ class Fetch {
223234
ResponseClass = StreamArrayBufferResponse;
224235
}
225236

226-
this._response = await new ResponseClass(
227-
this._nativeResponse,
228-
this._stream,
229-
this._streamController,
230-
{
231-
status: this._responseStatus,
232-
url: this._responseUrl,
233-
headers: this._nativeResponseHeaders,
234-
}
235-
);
236-
237-
this._deferredPromise.resolve(this._response);
238-
this.__closeStream();
237+
try {
238+
this._response = await new ResponseClass(
239+
this._nativeResponse,
240+
this._stream,
241+
this._streamController,
242+
{
243+
status: this._responseStatus,
244+
url: this._responseUrl,
245+
headers: this._nativeResponseHeaders,
246+
}
247+
);
248+
this._deferredPromise.resolve(this._response);
249+
} catch (error) {
250+
this._deferredPromise.reject(error);
251+
} finally {
252+
this.__closeStream();
253+
}
239254
}
240255

241256
__closeStream() {

src/StreamBlobResponse.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@ import Response from "./Response";
33
import { createBlobReader } from "./utils";
44

55
class StreamBlobResponse {
6-
constructor(blobData, stream, streamController, options, blobArrayBuffer) {
6+
constructor(
7+
blobData,
8+
stream,
9+
streamController,
10+
options /* , blobArrayBuffer */
11+
) {
712
const blob = BlobManager.createFromOptions(blobData);
813
this._blobData = blobData;
914
this._blobResponse = new Response(blob, options);
1015
this._streamResponse = new Response(stream, options);
1116

12-
if (blobArrayBuffer) {
13-
this._blobArrayBuffer = blobArrayBuffer;
14-
this._arrayBufferResponse = new Response(blobArrayBuffer, options);
15-
streamController.enqueue(new Uint8Array(blobArrayBuffer));
17+
// if (blobArrayBuffer) {
18+
// this._blobArrayBuffer = blobArrayBuffer;
19+
// this._arrayBufferResponse = new Response(blobArrayBuffer, options);
20+
// streamController.enqueue(new Uint8Array(blobArrayBuffer));
1621

17-
return this;
18-
}
22+
// return this;
23+
// }
1924

2025
return createBlobReader(blob)
2126
.readAsArrayBuffer()
2227
.then((arrayBuffer) => {
23-
this._blobArrayBuffer = arrayBuffer;
28+
// this._blobArrayBuffer = arrayBuffer;
2429
this._arrayBufferResponse = new Response(arrayBuffer, options);
2530
streamController.enqueue(new Uint8Array(arrayBuffer));
2631

0 commit comments

Comments
 (0)