|
9 | 9 | #include <stdlib.h>
|
10 | 10 | #include <emscripten/fetch.h>
|
11 | 11 |
|
12 |
| -int fetchSync() { |
| 12 | +#define SERVER "http://localhost:8888" |
| 13 | + |
| 14 | +// 301: Moved Permanently - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301 |
| 15 | +// 302: Found (Previously "Moved Temporarily") - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302 |
| 16 | +// 303: See Other - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303 |
| 17 | +// 307: Temporary Redirect - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307 |
| 18 | +// 308: Permanent Redirect - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308 |
| 19 | +const int redirect_codes[] = {301, 302, 303, 307, 308}; |
| 20 | +const int num_codes = sizeof(redirect_codes) / sizeof(redirect_codes[0]); |
| 21 | + |
| 22 | +void check_fetch_result(emscripten_fetch_t *fetch, int expected_status, const char *expected_url) { |
| 23 | + printf("Fetch finished with status %d\n", fetch->status); |
| 24 | + assert(fetch->status == expected_status); |
| 25 | + printf("Downloaded %llu bytes\n", fetch->numBytes); |
| 26 | + assert(strcmp(fetch->responseUrl, expected_url) == 0); |
| 27 | +} |
| 28 | + |
| 29 | +void fetchSyncTest(int code, const char *method) { |
| 30 | + char url[128]; |
| 31 | + snprintf(url, sizeof(url), SERVER "/status/%d", code); |
13 | 32 | emscripten_fetch_attr_t attr;
|
14 | 33 | emscripten_fetch_attr_init(&attr);
|
15 |
| - strcpy(attr.requestMethod, "GET"); |
| 34 | + strcpy(attr.requestMethod, method); |
16 | 35 | attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_REPLACE;
|
17 |
| - emscripten_fetch_t *fetch = emscripten_fetch(&attr, "https://httpbin.org/status/307"); |
| 36 | + emscripten_fetch_t *fetch = emscripten_fetch(&attr, url); |
18 | 37 | assert(fetch);
|
19 |
| - printf("Fetch sync finished with status %d\n", fetch->status); |
20 |
| - assert(fetch->status == 200); |
21 |
| - printf("Downloaded %llu bytes", fetch->numBytes); |
22 |
| - assert(strcmp(fetch->responseUrl, "https://httpbin.org/get") == 0); |
23 |
| - exit(0); |
| 38 | + check_fetch_result(fetch, 200, SERVER "/status/200"); |
| 39 | + emscripten_fetch_close(fetch); |
24 | 40 | }
|
25 | 41 |
|
| 42 | +void onsuccess(emscripten_fetch_t *fetch); |
| 43 | +void onreadystatechange(emscripten_fetch_t *fetch); |
| 44 | + |
| 45 | +// State for async test |
| 46 | +static int async_code_idx = 0; |
| 47 | +static int async_method_idx = 0; |
| 48 | +const char *methods[] = {"GET", "POST"}; |
| 49 | +const int num_methods = 2; |
| 50 | + |
| 51 | +void start_next_async_fetch(); |
| 52 | + |
26 | 53 | void onsuccess(emscripten_fetch_t *fetch) {
|
27 |
| - printf("Fetch async finished with status %d\n", fetch->status); |
28 |
| - assert(fetch->status == 200); |
29 |
| - printf("Downloaded %llu bytes", fetch->numBytes); |
30 |
| - assert(strcmp(fetch->responseUrl, "https://httpbin.org/get") == 0); |
| 54 | + check_fetch_result(fetch, 200, SERVER "/status/200"); |
31 | 55 | emscripten_fetch_close(fetch);
|
32 |
| - fetchSync(); |
| 56 | + start_next_async_fetch(); |
33 | 57 | }
|
34 | 58 |
|
35 | 59 | void onreadystatechange(emscripten_fetch_t *fetch) {
|
36 | 60 | printf("Fetch readyState %d responseUrl %s\n", fetch->readyState, fetch->responseUrl ? fetch->responseUrl : "is null");
|
37 | 61 | if (fetch->readyState < 2) {
|
38 | 62 | assert(NULL == fetch->responseUrl);
|
39 | 63 | } else {
|
40 |
| - assert(0 == strcmp(fetch->responseUrl, "https://httpbin.org/get")); |
| 64 | + assert(0 == strcmp(fetch->responseUrl, SERVER "/status/200")); |
41 | 65 | }
|
42 | 66 | }
|
43 | 67 |
|
44 |
| -int main() { |
| 68 | +void start_next_async_fetch() { |
| 69 | + if (async_code_idx >= num_codes) { |
| 70 | + async_code_idx = 0; |
| 71 | + async_method_idx++; |
| 72 | + if (async_method_idx >= num_methods) { |
| 73 | + // All async tests done, now run sync tests |
| 74 | + for (int m = 0; m < num_methods; ++m) { |
| 75 | + for (int i = 0; i < num_codes; ++i) { |
| 76 | + fetchSyncTest(redirect_codes[i], methods[m]); |
| 77 | + } |
| 78 | + } |
| 79 | + exit(0); |
| 80 | + } |
| 81 | + } |
| 82 | + int code = redirect_codes[async_code_idx++]; |
| 83 | + const char *method = methods[async_method_idx]; |
| 84 | + char url[128]; |
| 85 | + snprintf(url, sizeof(url), SERVER "/status/%d", code); |
45 | 86 | emscripten_fetch_attr_t attr;
|
46 | 87 | emscripten_fetch_attr_init(&attr);
|
47 |
| - strcpy(attr.requestMethod, "GET"); |
| 88 | + strcpy(attr.requestMethod, method); |
48 | 89 | attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
|
49 | 90 | attr.onsuccess = onsuccess;
|
50 | 91 | attr.onreadystatechange = onreadystatechange;
|
51 |
| - emscripten_fetch_t *fetch = emscripten_fetch(&attr, "https://httpbin.org/status/307"); |
| 92 | + emscripten_fetch_t *fetch = emscripten_fetch(&attr, url); |
52 | 93 | assert(fetch);
|
| 94 | +} |
| 95 | + |
| 96 | +int main() { |
| 97 | + start_next_async_fetch(); |
53 | 98 | return 99;
|
54 | 99 | }
|
0 commit comments