File: test_fetch_xhr_abort.cpp

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 121,860 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,366; asm: 2,415; lisp: 1,869
file content (56 lines) | stat: -rw-r--r-- 2,037 bytes parent folder | download | duplicates (2)
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;
}