Skip to content

Commit f7651be

Browse files
authored
Rename/cleanup fetch tests. NFC (#21218)
1 parent 329aa7f commit f7651be

14 files changed

+117
-118
lines changed

test/fetch/response_headers.cpp

Lines changed: 0 additions & 79 deletions
This file was deleted.

test/fetch/cached_xhr.cpp renamed to test/fetch/test_fetch_cached_xhr.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
int result = 0;
1313

1414
// Fetch file without XHRing.
15-
void fetchFromIndexedDB()
16-
{
15+
void fetchFromIndexedDB() {
1716
emscripten_fetch_attr_t attr;
1817
emscripten_fetch_attr_init(&attr);
1918
strcpy(attr.requestMethod, "GET");
@@ -36,8 +35,7 @@ void fetchFromIndexedDB()
3635
}
3736

3837
// XHR and store to cache.
39-
int main()
40-
{
38+
int main() {
4139
emscripten_fetch_attr_t attr;
4240
emscripten_fetch_attr_init(&attr);
4341
strcpy(attr.requestMethod, "GET");
@@ -50,6 +48,10 @@ int main()
5048
// Test that the file now exists:
5149
fetchFromIndexedDB();
5250
};
51+
attr.onerror = [](emscripten_fetch_t *fetch) {
52+
printf("Error downloading\n");
53+
abort();
54+
};
5355
attr.onprogress = [](emscripten_fetch_t *fetch) {
5456
if (fetch->totalBytes > 0) {
5557
printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes);
@@ -60,6 +62,8 @@ int main()
6062
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_PERSIST_FILE;
6163
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
6264
assert(fetch != 0);
63-
memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call.
65+
// emscripten_fetch() must be able to operate without referencing to this
66+
// structure after the call.
67+
memset(&attr, 0, sizeof(attr));
6468
return 99;
6569
}
File renamed without changes.

test/fetch/headers_received.cpp renamed to test/fetch/test_fetch_headers_received.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
#include <assert.h>
1010
#include <emscripten/fetch.h>
1111

12-
void readyStateChange(emscripten_fetch_t *fetch)
13-
{
14-
if(fetch->readyState != 2) return;
12+
void readyStateChange(emscripten_fetch_t *fetch) {
13+
if (fetch->readyState != 2) return;
1514

1615
size_t headersLengthBytes = emscripten_fetch_get_response_headers_length(fetch) + 1;
17-
char *headerString = new char[headersLengthBytes];
16+
char *headerString = malloc(headersLengthBytes);
1817

1918
assert(headerString);
2019
emscripten_fetch_get_response_headers(fetch, headerString, headersLengthBytes);
@@ -23,11 +22,10 @@ void readyStateChange(emscripten_fetch_t *fetch)
2322
char **responseHeaders = emscripten_fetch_unpack_response_headers(headerString);
2423
assert(responseHeaders);
2524

26-
delete[] headerString;
25+
free(headerString);
2726

2827
int numHeaders = 0;
29-
for(; responseHeaders[numHeaders * 2]; ++numHeaders)
30-
{
28+
for (; responseHeaders[numHeaders * 2]; ++numHeaders) {
3129
// Check both the header and its value are present.
3230
assert(responseHeaders[(numHeaders * 2) + 1]);
3331
printf("Got response header: %s:%s\n", responseHeaders[numHeaders * 2], responseHeaders[(numHeaders * 2) + 1]);
@@ -38,22 +36,27 @@ void readyStateChange(emscripten_fetch_t *fetch)
3836
emscripten_fetch_free_unpacked_response_headers(responseHeaders);
3937
}
4038

41-
void success(emscripten_fetch_t *fetch)
42-
{
39+
void success(emscripten_fetch_t *fetch) {
4340
printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);
4441
// The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
4542
emscripten_fetch_close(fetch); // Free data associated with the fetch.
4643
}
4744

48-
int main()
49-
{
45+
void onerror(emscripten_fetch_t *fetch) {
46+
printf("onerror: %d '%s'\n", fetch->status, fetch->statusText);
47+
abort();
48+
}
49+
50+
int main() {
5051
emscripten_fetch_attr_t attr;
5152
emscripten_fetch_attr_init(&attr);
5253
strcpy(attr.requestMethod, "GET");
5354
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_REPLACE;
5455
attr.onsuccess = success;
56+
attr.onerror = onerror;
5557
attr.onreadystatechange = readyStateChange;
5658
attr.timeoutMSecs = 2*60;
59+
printf("Calling emscripten_fetch\n");
5760
emscripten_fetch(&attr, "myfile.dat");
5861
return 0;
5962
}

test/fetch/idb_delete.cpp renamed to test/fetch/test_fetch_idb_delete.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int main()
1717
strcpy(attr.requestMethod, "GET");
1818
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_PERSIST_FILE;
1919
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
20+
printf("fetch->status: %d\n", fetch->status);
2021
assert(fetch->status == 200 && "Initial XHR GET of gears.png should have succeeded");
2122
emscripten_fetch_close(fetch);
2223

File renamed without changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2018 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
#include <string.h>
7+
#include <stdio.h>
8+
#include <math.h>
9+
#include <assert.h>
10+
#include <emscripten/fetch.h>
11+
12+
int result = 1;
13+
14+
int main() {
15+
static const char* const headers[] = {
16+
"X-Emscripten-Test",
17+
"1",
18+
0,
19+
};
20+
const size_t n_values = sizeof(headers) / sizeof(headers[0]) - 1;
21+
emscripten_fetch_attr_t attr;
22+
emscripten_fetch_attr_init(&attr);
23+
strcpy(attr.requestMethod, "GET");
24+
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
25+
attr.requestHeaders = headers;
26+
27+
attr.onsuccess = [] (emscripten_fetch_t *fetch) {
28+
assert(fetch->__attributes.requestHeaders != 0);
29+
assert(fetch->__attributes.requestHeaders != headers);
30+
for (size_t i = 0; i < n_values; ++i) {
31+
const char* origHeader = headers[i];
32+
const char* header = fetch->__attributes.requestHeaders[i];
33+
assert(origHeader != header);
34+
assert(strcmp(origHeader, header) == 0);
35+
}
36+
assert(fetch->__attributes.requestHeaders[n_values] == 0);
37+
38+
printf("Finished downloading %llu bytes\n", fetch->numBytes);
39+
// Compute rudimentary checksum of data
40+
uint8_t checksum = 0;
41+
for (int i = 0; i < fetch->numBytes; ++i) {
42+
checksum ^= fetch->data[i];
43+
}
44+
printf("Data checksum: %02X\n", checksum);
45+
assert(checksum == 0x08);
46+
emscripten_fetch_close(fetch);
47+
48+
if (result == 1) result = 0;
49+
};
50+
51+
attr.onprogress = [] (emscripten_fetch_t *fetch) {
52+
if (fetch->totalBytes > 0) {
53+
printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes);
54+
} else {
55+
printf("Downloading.. %lld bytes complete.\n", fetch->dataOffset + fetch->numBytes);
56+
}
57+
};
58+
59+
attr.onerror = [] (emscripten_fetch_t *fetch) {
60+
printf("Download failed!\n");
61+
emscripten_fetch_close(fetch);
62+
assert(false && "Shouldn't fail!");
63+
};
64+
65+
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
66+
if (result != 0) {
67+
result = 2;
68+
printf("emscripten_fetch() failed to run synchronously!\n");
69+
}
70+
return result;
71+
}
File renamed without changes.

test/fetch/sync_xhr.cpp renamed to test/fetch/test_fetch_sync_xhr.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212

1313
int result = -1;
1414

15-
int main()
16-
{
17-
// If an exception is thrown from the user callback, it bubbles up to self.onerror but is otherwise completely
18-
// swallowed by xhr.send.
15+
int main() {
16+
// If an exception is thrown from the user callback, it bubbles up to
17+
// self.onerror but is otherwise completely swallowed by xhr.send.
1918
EM_ASM({self.onerror = function() {
2019
out('Got error');
2120
HEAP32[$0 >> 2] = 2;

test/fetch/to_indexeddb.cpp renamed to test/fetch/test_fetch_to_indexeddb.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#include <assert.h>
1010
#include <emscripten/fetch.h>
1111

12-
int main()
13-
{
12+
int main() {
1413
emscripten_fetch_attr_t attr;
1514
emscripten_fetch_attr_init(&attr);
1615
strcpy(attr.requestMethod, "GET");
@@ -45,6 +44,8 @@ int main()
4544

4645
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
4746
assert(fetch != 0);
48-
memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call.
47+
// emscripten_fetch() must be able to operate without referencing to this
48+
// structure after the call.
49+
memset(&attr, 0, sizeof(attr));
4950
return 99;
5051
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)