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 24d6899
Show file tree
Hide file tree
Showing 14 changed files with 114 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 @@ -60,6 +58,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.
32 changes: 16 additions & 16 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4823,13 +4823,13 @@ def test_preallocated_heap(self):
@also_with_wasm2js
def test_fetch_to_memory(self):
# Test error reporting in the negative case when the file URL doesn't exist. (http 404)
self.btest_exit('fetch/to_memory.cpp',
self.btest_exit('fetch/test_fetch_to_memory.cpp',
args=['-sFETCH_DEBUG', '-sFETCH', '-DFILE_DOES_NOT_EXIST'])

# Test the positive case when the file URL exists. (http 200)
shutil.copyfile(test_file('gears.png'), 'gears.png')
for arg in [[], ['-sFETCH_SUPPORT_INDEXEDDB=0']]:
self.btest_exit('fetch/to_memory.cpp',
self.btest_exit('fetch/test_fetch_to_memory.cpp',
args=['-sFETCH_DEBUG', '-sFETCH'] + arg)

@parameterized({
Expand All @@ -4840,30 +4840,28 @@ def test_fetch_to_memory(self):
@requires_threads
def test_fetch_from_thread(self, args):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/from_thread.cpp',
self.btest_exit('fetch/test_fetch_from_thread.cpp',
args=args + ['-pthread', '-sPROXY_TO_PTHREAD', '-sFETCH_DEBUG', '-sFETCH', '-DFILE_DOES_NOT_EXIST'],
also_wasm2js=True)

@also_with_wasm2js
def test_fetch_to_indexdb(self):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/to_indexeddb.cpp',
args=['-sFETCH_DEBUG', '-sFETCH'])
self.btest_exit('fetch/test_fetch_to_indexeddb.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])

# Tests emscripten_fetch() usage to persist an XHR into IndexedDB and subsequently load up from there.
@also_with_wasm2js
def test_fetch_cached_xhr(self):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/cached_xhr.cpp',
args=['-sFETCH_DEBUG', '-sFETCH'])
self.btest_exit('fetch/test_fetch_cached_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])

# Tests that response headers get set on emscripten_fetch_t values.
@no_firefox('https://github.com/emscripten-core/emscripten/issues/16868')
@also_with_wasm2js
@requires_threads
def test_fetch_response_headers(self):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/response_headers.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD'])
self.btest_exit('fetch/test_fetch_response_headers.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD'])

# Test emscripten_fetch() usage to stream a XHR in to memory without storing the full file in memory
@also_with_wasm2js
Expand All @@ -4877,15 +4875,16 @@ def test_fetch_stream_file(self):
with open('largefile.txt', 'w') as f:
for _ in range(1024):
f.write(s)
self.btest_exit('fetch/stream_file.cpp',
self.btest_exit('fetch/test_fetch_stream_file.cpp',
args=['-sFETCH_DEBUG', '-sFETCH', '-sINITIAL_MEMORY=536870912'])

def test_fetch_headers_received(self):
self.btest_exit('fetch/headers_received.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])
create_file('myfile.dat', 'hello world\n')
self.btest_exit('fetch/test_fetch_headers_received.c', args=['-sFETCH_DEBUG', '-sFETCH'])

def test_fetch_xhr_abort(self):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/xhr_abort.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])
self.btest_exit('fetch/test_fetch_xhr_abort.cpp', args=['-sFETCH_DEBUG', '-sFETCH'])

# Tests emscripten_fetch() usage in synchronous mode when used from the main
# thread proxied to a Worker with -sPROXY_TO_PTHREAD option.
Expand All @@ -4894,7 +4893,7 @@ def test_fetch_xhr_abort(self):
@requires_threads
def test_fetch_sync_xhr(self):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/sync_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD'])
self.btest_exit('fetch/test_fetch_sync_xhr.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-pthread', '-sPROXY_TO_PTHREAD'])

# Tests emscripten_fetch() usage when user passes none of the main 3 flags (append/replace/no_download).
# In that case, in append is implicitly understood.
Expand All @@ -4917,26 +4916,27 @@ def test_fetch_sync_xhr_in_wasm(self):
@requires_threads
def test_fetch_sync_xhr_in_proxy_to_worker(self):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/sync_xhr.cpp',
self.btest_exit('fetch/test_fetch_sync_xhr.cpp',
args=['-sFETCH_DEBUG', '-sFETCH', '--proxy-to-worker'])

# Tests waiting on EMSCRIPTEN_FETCH_WAITABLE request from a worker thread
@unittest.skip("emscripten_fetch_wait relies on an asm.js-based web worker")
@requires_threads
def test_fetch_sync_fetch_in_main_thread(self):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/sync_fetch_in_main_thread.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-pthread', '-sPROXY_TO_PTHREAD'])
self.btest_exit('fetch/test_fetch_sync_in_main_thread.cpp', args=['-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-pthread', '-sPROXY_TO_PTHREAD'])

@requires_threads
@disabled('https://github.com/emscripten-core/emscripten/issues/16746')
def test_fetch_idb_store(self):
self.btest_exit('fetch/idb_store.cpp', args=['-pthread', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD'])
self.btest_exit('fetch/test_fetch_idb_store.cpp', args=['-pthread', '-sFETCH', '-sPROXY_TO_PTHREAD'])

@requires_threads
@disabled('https://github.com/emscripten-core/emscripten/issues/16746')
def test_fetch_idb_delete(self):
shutil.copyfile(test_file('gears.png'), 'gears.png')
self.btest_exit('fetch/idb_delete.cpp', args=['-pthread', '-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD'])
self.btest_exit('fetch/test_fetch_idb_delete.cpp', args=['-pthread', '-sFETCH_DEBUG', '-sFETCH', '-sWASM=0', '-sPROXY_TO_PTHREAD'])


def test_fetch_post(self):
self.btest_exit('fetch/test_fetch_post.c', args=['-sFETCH'])
Expand Down

0 comments on commit 24d6899

Please sign in to comment.