Skip to content

Commit 829f4ae

Browse files
authored
Use local server for test_fetch_redirect. NFC (#24638)
fixes redirect test to use localhost
1 parent 21a3a81 commit 829f4ae

File tree

2 files changed

+91
-17
lines changed

2 files changed

+91
-17
lines changed

test/common.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,20 @@ def do_POST(self):
21482148
create_file(filename, post_data, binary=True)
21492149
self.send_response(200)
21502150
self.end_headers()
2151+
elif urlinfo.path.startswith('/status/'):
2152+
code_str = urlinfo.path[len('/status/'):]
2153+
code = int(code_str)
2154+
if code in (301, 302, 303, 307, 308):
2155+
self.send_response(code)
2156+
self.send_header('Location', '/status/200')
2157+
self.end_headers()
2158+
elif code == 200:
2159+
self.send_response(200)
2160+
self.send_header('Content-type', 'text/plain')
2161+
self.end_headers()
2162+
self.wfile.write(b'OK')
2163+
else:
2164+
self.send_error(400, f'Not implemented for {code}')
21512165
else:
21522166
print(f'do_POST: unexpected POST: {urlinfo}')
21532167

@@ -2160,6 +2174,21 @@ def do_GET(self):
21602174
self.send_header('Content-type', 'text/html')
21612175
self.end_headers()
21622176
self.wfile.write(read_binary(test_file('browser_harness.html')))
2177+
elif info.path.startswith('/status/'):
2178+
code_str = info.path[len('/status/'):]
2179+
code = int(code_str)
2180+
if code in (301, 302, 303, 307, 308):
2181+
# Redirect to /status/200
2182+
self.send_response(code)
2183+
self.send_header('Location', '/status/200')
2184+
self.end_headers()
2185+
elif code == 200:
2186+
self.send_response(200)
2187+
self.send_header('Content-type', 'text/plain')
2188+
self.end_headers()
2189+
self.wfile.write(b'OK')
2190+
else:
2191+
self.send_error(400, f'Not implemented for {code}')
21632192
elif 'report_' in self.path:
21642193
# the test is reporting its result. first change dir away from the
21652194
# test dir, as it will be deleted now that the test is finishing, and

test/fetch/test_fetch_redirect.c

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,91 @@
99
#include <stdlib.h>
1010
#include <emscripten/fetch.h>
1111

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);
1332
emscripten_fetch_attr_t attr;
1433
emscripten_fetch_attr_init(&attr);
15-
strcpy(attr.requestMethod, "GET");
34+
strcpy(attr.requestMethod, method);
1635
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);
1837
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);
2440
}
2541

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+
2653
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");
3155
emscripten_fetch_close(fetch);
32-
fetchSync();
56+
start_next_async_fetch();
3357
}
3458

3559
void onreadystatechange(emscripten_fetch_t *fetch) {
3660
printf("Fetch readyState %d responseUrl %s\n", fetch->readyState, fetch->responseUrl ? fetch->responseUrl : "is null");
3761
if (fetch->readyState < 2) {
3862
assert(NULL == fetch->responseUrl);
3963
} else {
40-
assert(0 == strcmp(fetch->responseUrl, "https://httpbin.org/get"));
64+
assert(0 == strcmp(fetch->responseUrl, SERVER "/status/200"));
4165
}
4266
}
4367

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);
4586
emscripten_fetch_attr_t attr;
4687
emscripten_fetch_attr_init(&attr);
47-
strcpy(attr.requestMethod, "GET");
88+
strcpy(attr.requestMethod, method);
4889
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
4990
attr.onsuccess = onsuccess;
5091
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);
5293
assert(fetch);
94+
}
95+
96+
int main() {
97+
start_next_async_fetch();
5398
return 99;
5499
}

0 commit comments

Comments
 (0)