Skip to content

[PHP-wasm] allow requests to follow redirects #2206

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

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from

Conversation

bgrgicak
Copy link
Collaborator

@bgrgicak bgrgicak commented May 10, 2025

Motivation for the change, related issues

Browsers and JS fetch automatically follow (301-302) redirects, but the PHPRequestHandler doesn't.
To follow redirects, developers need to implement a loop that checks the response and run another request if the location header is present.

This isn't a big issue, but we already have this implemented in multiple places around Playground, and anyone who is looking into building integration tests with Playground needs to implement it.

This PR suggests that we add support for following redirects to the PHPRequestHandler.
Because this is new behavior, I made it manual by default, so users will need to enable it by setting redirect: 'follow'.

Implementation details

This PR adds a redirect option to the PHPRequest interface.
Depending on the option, PHPRequestHandler.request will follow the redirect, thrown and error, or return the 301/302 response and give manual control to the user. manual is the default.

Testing Instructions (or ideally a Blueprint)

  • CI

/**
* Whether to follow redirects.
* - `follow`: Follow redirects.
* - `manual`: Return the redirect response. (Default)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should manual stay the default behavior or should we follow by default like JS fetch and browsers do?

});
expect(response.httpStatusCode).toBe(200);
expect(response.text).toContain('Edit site');
expect(response.text).toContain('Edit Site');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This label changed in a recent version of WP, and the test stopped working.

[301, 302].includes(response.httpStatusCode)
) {
if (request.redirect === 'error') {
throw new Error(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to throw? Or could we just have two modes: follow and not follow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, fetch Rejects the promise with TypeError: NetworkError when attempting to fetch resource., so I made it work in the same way now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or could we just have two modes: follow and not follow?

We could, but I wanted to match the fetch. Would you prefer to only add follow and manual for now and add error if users ask for it?

@adamziel
Copy link
Collaborator

adamziel commented May 12, 2025

I really like the idea. At the same time, I'm worried this may not be easy. When you log in, the http response redirects you and sets the auth cookies in one go. If we discard that response and redirect internally, the user won't be logged in. Also, the iframe src won't point to the target URL. I suspect this is why web servers typically do a network trip instead of internalizing the redirection like this PR proposes.

@bgrgicak
Copy link
Collaborator Author

When you log in, the http response redirects you and sets the auth cookies in one go. If we discard that response and redirect internally, the user won't be logged in.

I see now that this works well with the Node version of Playgorund, where cookies are stored internally, but not with the browser version.
But is this an issue if we use manual by default, and a comment explaining it?

There is a good use case for this in the Node version of Playground, as you can see from our tests, but I'm not sure if the request method is the best place to implement it.

@bgrgicak bgrgicak marked this pull request as ready for review May 15, 2025 05:17
Base automatically changed from add/run-playground-cli-function to trunk May 19, 2025 10:44
@adamziel
Copy link
Collaborator

Documenting our call today:

  • There are still valid cases to use cookies with a local Node dev server.
  • It would be useful to have a testing package expose a thin client, such as PlaygroundClient.request(), that could work directly with the request handler and support for cookies and redirects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Inbox
Development

Successfully merging this pull request may close these issues.

2 participants