forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fetch_xhr_abort.cpp
56 lines (51 loc) · 1.99 KB
/
test_fetch_xhr_abort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2022 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 <stdio.h>
#include <string.h>
#include <assert.h>
#include <emscripten/fetch.h>
#include <emscripten/em_asm.h>
void handleSuccess(emscripten_fetch_t *fetch) {
assert(false && "Should not succeed");
emscripten_fetch_close(fetch);
}
void handleError(emscripten_fetch_t *fetch) {
printf("handleError: %d %s\n", fetch->status, fetch->statusText);
bool isAbortedStatus = fetch->status == (unsigned short) -1;
assert(isAbortedStatus); // should have aborted status
EM_ASM({
const xhr = Fetch.xhrs.get($0);
const oldReadyStateChangeHandler = xhr.onreadystatechange;
// Overriding xhr handlers to check if xhr.abort() was called
xhr.onreadystatechange = (e) => {
console.log("fetch: xhr.readyState: " + xhr.readyState + ', status: ' + xhr.status);
assert(xhr.readyState === 0 || xhr.status === 0, "readyState should be equal to UNSENT(0) or status should be equal to 0 when calling xhr.abort()");
oldReadyStateChangeHandler(e);
};
xhr.onload = () => {
assert(false, "xhr.onload should not be called after xhr.abort()");
};
xhr.onerror = () => {
assert(false, "xhr.onerror should not be called after xhr.abort()");
};
xhr.onprogress = () => {
assert(false, "xhr.onprogress should not be called after xhr.abort()");
};
}, fetch->id);
}
void handleStateChange(emscripten_fetch_t *fetch) {
emscripten_fetch_close(fetch); // Abort the fetch
}
int main() {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_REPLACE;
attr.onsuccess = handleSuccess;
attr.onerror = handleError;
attr.onreadystatechange = handleStateChange;
auto fetch = emscripten_fetch(&attr, "gears.png");
return 0;
}