-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Use local server for test_fetch_redirect. NFC #24638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6ce3e26
fixes redirect test to use localhost
dipidoo c4636f2
adds missed sections
dipidoo 150e3b1
fixes bad merge
dipidoo 9210d5d
Merge branch 'main' into main
dipidoo 4e9417a
Merge branch 'main' into main
dipidoo aee399e
Merge branch 'main' into main
dipidoo 5729fd5
Merge branch 'emscripten-core:main' into main
dipidoo 4e03fba
addresses feedback
dipidoo e33a854
Merge branch 'main' into main
dipidoo 95bb9f3
Merge branch 'main' into main
dipidoo 272af30
Merge branch 'main' into main
dipidoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,46 +9,93 @@ | |
#include <stdlib.h> | ||
#include <emscripten/fetch.h> | ||
|
||
int fetchSync() { | ||
#define SERVER "http://localhost:8888" | ||
|
||
// 301: Moved Permanently - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301 | ||
// 302: Found (Previously "Moved Temporarily") - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302 | ||
// 303: See Other - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/303 | ||
// 307: Temporary Redirect - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307 | ||
// 308: Permanent Redirect - https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308 | ||
const int redirect_codes[] = {301, 302, 303, 307, 308}; | ||
const int num_codes = sizeof(redirect_codes) / sizeof(redirect_codes[0]); | ||
|
||
void check_fetch_result(emscripten_fetch_t *fetch, int expected_status, const char *expected_url) { | ||
printf("Fetch finished with status %d\n", fetch->status); | ||
assert(fetch->status == expected_status); | ||
printf("Downloaded %llu bytes\n", fetch->numBytes); | ||
assert(strcmp(fetch->responseUrl, expected_url) == 0); | ||
} | ||
|
||
void fetchSyncTest(int code, const char *method) { | ||
char url[128]; | ||
snprintf(url, sizeof(url), SERVER "/status/%d", code); | ||
emscripten_fetch_attr_t attr; | ||
emscripten_fetch_attr_init(&attr); | ||
strcpy(attr.requestMethod, "GET"); | ||
strcpy(attr.requestMethod, method); | ||
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_REPLACE; | ||
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "https://httpbin.org/status/307"); | ||
emscripten_fetch_t *fetch = emscripten_fetch(&attr, url); | ||
assert(fetch); | ||
printf("Fetch sync finished with status %d\n", fetch->status); | ||
assert(fetch->status == 200); | ||
printf("Downloaded %llu bytes", fetch->numBytes); | ||
assert(strcmp(fetch->responseUrl, "https://httpbin.org/get") == 0); | ||
exit(0); | ||
check_fetch_result(fetch, 200, SERVER "/status/200"); | ||
emscripten_fetch_close(fetch); | ||
} | ||
|
||
void onsuccess(emscripten_fetch_t *fetch); | ||
void onreadystatechange(emscripten_fetch_t *fetch); | ||
|
||
// State for async test | ||
static int async_code_idx = 0; | ||
static int async_method_idx = 0; | ||
const char *methods[] = {"GET", "POST"}; | ||
const int num_methods = 2; | ||
|
||
void start_next_async_fetch(); | ||
|
||
void onsuccess(emscripten_fetch_t *fetch) { | ||
printf("Fetch async finished with status %d\n", fetch->status); | ||
assert(fetch->status == 200); | ||
printf("Downloaded %llu bytes", fetch->numBytes); | ||
assert(strcmp(fetch->responseUrl, "https://httpbin.org/get") == 0); | ||
check_fetch_result(fetch, 200, SERVER "/status/200"); | ||
emscripten_fetch_close(fetch); | ||
fetchSync(); | ||
start_next_async_fetch(); | ||
} | ||
|
||
void onreadystatechange(emscripten_fetch_t *fetch) { | ||
printf("Fetch readyState %d responseUrl %s\n", fetch->readyState, fetch->responseUrl ? fetch->responseUrl : "is null"); | ||
if (fetch->readyState < 2) { | ||
assert(NULL == fetch->responseUrl); | ||
} else { | ||
assert(0 == strcmp(fetch->responseUrl, "https://httpbin.org/get")); | ||
assert(0 == strcmp(fetch->responseUrl, SERVER "/status/200")); | ||
} | ||
} | ||
|
||
int main() { | ||
void start_next_async_fetch() { | ||
if (async_code_idx >= num_codes) { | ||
async_code_idx = 0; | ||
async_method_idx++; | ||
if (async_method_idx >= num_methods) { | ||
// All async tests done, now run sync tests | ||
for (int m = 0; m < num_methods; ++m) { | ||
for (int i = 0; i < num_codes; ++i) { | ||
fetchSyncTest(redirect_codes[i], methods[m]); | ||
} | ||
} | ||
exit(0); | ||
} | ||
} | ||
int code = redirect_codes[async_code_idx++]; | ||
const char *method = methods[async_method_idx]; | ||
char url[128]; | ||
snprintf(url, sizeof(url), SERVER "/status/%d", code); | ||
emscripten_fetch_attr_t attr; | ||
emscripten_fetch_attr_init(&attr); | ||
strcpy(attr.requestMethod, "GET"); | ||
strcpy(attr.requestMethod, method); | ||
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; | ||
attr.onsuccess = onsuccess; | ||
attr.onreadystatechange = onreadystatechange; | ||
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "https://httpbin.org/status/307"); | ||
emscripten_fetch_t *fetch = emscripten_fetch(&attr, url); | ||
assert(fetch); | ||
} | ||
|
||
int main() { | ||
async_code_idx = 0; | ||
async_method_idx = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two lines are not needed since they are already initialized to zero above. |
||
start_next_async_fetch(); | ||
return 99; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.