Skip to content

Commit

Permalink
Record URLs changed in redirect responses as loaded
Browse files Browse the repository at this point in the history
But the E2E test fails.
  • Loading branch information
ledsun committed Nov 14, 2023
1 parent f939f2d commit 95e7afe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ext/js/lib/js/require_remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class RequireRemote

def initialize
set_base_url

# The loaded URLs are saved as Ruby strings.
@loaded_urls = Set.new
end

Expand All @@ -26,8 +28,15 @@ def load(relative_feature, use_maps: false)
response = JS.global.fetch(location.url).await

if response[:status].to_i == 200
# Check if the redirected URL has already loaded.
return false if @loaded_urls.include?(response[:url].to_s)

code = response.text().await.to_s
eval_code(code, location)

# The redirect that occurred may have been temporary.
# The original URL is not recorded.
# Only the URL after the redirect is recorded.
@loaded_urls << response[:url].to_s
true
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
expect(await resolve()).toBe(false);
});

test("JS::RequireRemote#load Identify by URL after redirect", async ({
page,
}) => {
// Stop tests immediately when an error occurs in the page.
page.on("pageerror", (error) => {throw error;});

const resolve = await resolveBinding(page, "checkResolved");
await page.goto(
"https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/",
);
await page.setContent(`
<script src="browser.script.iife.js"></script>
<script type="text/ruby" data-eval="async">
require 'js/require_remote'
JS::RequireRemote.instance.load 'redirect_to_error_on_load_twice'
JS.global.checkResolved JS::RequireRemote.instance.load 'error_on_load_twice'
</script>
`);

expect(await resolve()).toBe(false);
});

test("JS::RequireRemote#load throws error when gem is not found", async ({
page,
}) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/npm-packages/ruby-wasm-wasi/test-e2e/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const setupDebugLog = (context: BrowserContext) => {
console.log("<<", response.status(), response.url()),
);
context.on("console", (msg) => console.log("LOG:", msg.text()));
context.on("weberror", (error) => console.log("ERROR!!!!!:", error.message))
}
};

Expand All @@ -36,6 +37,13 @@ export const setupProxy = (context: BrowserContext) => {
ALREADY_LOADED = true`,
contentType: "text/ruby",
});
} else if (relativePath.match("redirect_to_error_on_load_twice.rb")) {
route.fulfill({
status: 302,
headers: {
location: "error_on_load_twice.rb",
},
});
} else if (fs.existsSync(mockedPath)) {
route.fulfill({
path: mockedPath,
Expand Down

0 comments on commit 95e7afe

Please sign in to comment.