Skip to content

Commit

Permalink
Rename/cleanup fetch tests. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Jan 30, 2024
1 parent 75892b3 commit 6f41cc7
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 118 deletions.
79 changes: 0 additions & 79 deletions test/fetch/response_headers.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
int result = 0;

// Fetch file without XHRing.
void fetchFromIndexedDB()
{
void fetchFromIndexedDB() {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
Expand All @@ -36,8 +35,7 @@ void fetchFromIndexedDB()
}

// XHR and store to cache.
int main()
{
int main() {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
Expand All @@ -50,6 +48,10 @@ int main()
// Test that the file now exists:
fetchFromIndexedDB();
};
attr.onerror = [](emscripten_fetch_t *fetch) {
printf("Error downloading\n");
abort();
};
attr.onprogress = [](emscripten_fetch_t *fetch) {
if (fetch->totalBytes > 0) {
printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes);
Expand All @@ -60,6 +62,8 @@ int main()
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_PERSIST_FILE;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
assert(fetch != 0);
memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call.
// emscripten_fetch() must be able to operate without referencing to this
// structure after the call.
memset(&attr, 0, sizeof(attr));
return 99;
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
#include <assert.h>
#include <emscripten/fetch.h>

void readyStateChange(emscripten_fetch_t *fetch)
{
if(fetch->readyState != 2) return;
void readyStateChange(emscripten_fetch_t *fetch) {
if (fetch->readyState != 2) return;

size_t headersLengthBytes = emscripten_fetch_get_response_headers_length(fetch) + 1;
char *headerString = new char[headersLengthBytes];
char *headerString = malloc(headersLengthBytes);

assert(headerString);
emscripten_fetch_get_response_headers(fetch, headerString, headersLengthBytes);
Expand All @@ -23,11 +22,10 @@ void readyStateChange(emscripten_fetch_t *fetch)
char **responseHeaders = emscripten_fetch_unpack_response_headers(headerString);
assert(responseHeaders);

delete[] headerString;
free(headerString);

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

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

int main()
{
void onerror(emscripten_fetch_t *fetch) {
printf("onerror: %d '%s'\n", fetch->status, fetch->statusText);
abort();
}

int main() {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_REPLACE;
attr.onsuccess = success;
attr.onerror = onerror;
attr.onreadystatechange = readyStateChange;
attr.timeoutMSecs = 2*60;
printf("Calling emscripten_fetch\n");
emscripten_fetch(&attr, "myfile.dat");
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ int main()
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_PERSIST_FILE;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
printf("fetch->status: %d\n", fetch->status);
assert(fetch->status == 200 && "Initial XHR GET of gears.png should have succeeded");
emscripten_fetch_close(fetch);

Expand Down
File renamed without changes.
71 changes: 71 additions & 0 deletions test/fetch/test_fetch_response_headers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2018 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <string.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <emscripten/fetch.h>

int result = 1;

int main() {
static const char* const headers[] = {
"X-Emscripten-Test",
"1",
0,
};
const size_t n_values = sizeof(headers) / sizeof(headers[0]) - 1;
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
attr.requestHeaders = headers;

attr.onsuccess = [] (emscripten_fetch_t *fetch) {
assert(fetch->__attributes.requestHeaders != 0);
assert(fetch->__attributes.requestHeaders != headers);
for (size_t i = 0; i < n_values; ++i) {
const char* origHeader = headers[i];
const char* header = fetch->__attributes.requestHeaders[i];
assert(origHeader != header);
assert(strcmp(origHeader, header) == 0);
}
assert(fetch->__attributes.requestHeaders[n_values] == 0);

printf("Finished downloading %llu bytes\n", fetch->numBytes);
// Compute rudimentary checksum of data
uint8_t checksum = 0;
for (int i = 0; i < fetch->numBytes; ++i) {
checksum ^= fetch->data[i];
}
printf("Data checksum: %02X\n", checksum);
assert(checksum == 0x08);
emscripten_fetch_close(fetch);

if (result == 1) result = 0;
};

attr.onprogress = [] (emscripten_fetch_t *fetch) {
if (fetch->totalBytes > 0) {
printf("Downloading.. %.2f%% complete.\n", (fetch->dataOffset + fetch->numBytes) * 100.0 / fetch->totalBytes);
} else {
printf("Downloading.. %lld bytes complete.\n", fetch->dataOffset + fetch->numBytes);
}
};

attr.onerror = [] (emscripten_fetch_t *fetch) {
printf("Download failed!\n");
emscripten_fetch_close(fetch);
assert(false && "Shouldn't fail!");
};

emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
if (result != 0) {
result = 2;
printf("emscripten_fetch() failed to run synchronously!\n");
}
return result;
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

int result = -1;

int main()
{
// If an exception is thrown from the user callback, it bubbles up to self.onerror but is otherwise completely
// swallowed by xhr.send.
int main() {
// If an exception is thrown from the user callback, it bubbles up to
// self.onerror but is otherwise completely swallowed by xhr.send.
EM_ASM({self.onerror = function() {
out('Got error');
HEAP32[$0 >> 2] = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#include <assert.h>
#include <emscripten/fetch.h>

int main()
{
int main() {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
Expand Down Expand Up @@ -45,6 +44,8 @@ int main()

emscripten_fetch_t *fetch = emscripten_fetch(&attr, "gears.png");
assert(fetch != 0);
memset(&attr, 0, sizeof(attr)); // emscripten_fetch() must be able to operate without referencing to this structure after the call.
// emscripten_fetch() must be able to operate without referencing to this
// structure after the call.
memset(&attr, 0, sizeof(attr));
return 99;
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 6f41cc7

Please sign in to comment.