symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.91k stars 213 forks source link

ajax post 404 not found, even though the path/method is valid #547

Closed arderyp closed 2 years ago

arderyp commented 2 years ago

I have a link on my page with a specific class. a javascript listener waits for links with this class to be clicked and, when clicked, grabs the associated data-* attributes and submits a POST ajax query to the links href. This works for me when browsing my site. However, when the following, I get a 404 Not Found in the console log, even though the path is indeed there:

$this->client->executeScript("document.querySelector('.submit-on-link').click()");

The fact that I'm seeing the POST 404 in the console log suggests my javascript is indeed being executed, so that's not the issue. Also, when I browse the the page in question in my browser, and I paste the following into my console bar, the ajax POST is issued and completed successfully:

document.querySelector('.submit-on-link').click();

Is there some configuration I need to enable on the Panther client to allow ajax requests or something?

arderyp commented 2 years ago

I'm wondering if authentication may be the issue. When I changed the endpoint from json to html and rePOST, I get a 302 redirect to my authentication page.

Is there any way around this?

arderyp commented 2 years ago

im using the symfony 4.4 recomended approach for logging my client in (not logging in via clicking through the actual login page)

       // Create CasUser object from first admin Users entity found in DB
        $this->user = $this->getFirstAdminUserEntity();
        $guardUser = new GuardUser();
        $guardUser->hydrateFromUsersEntity($this->user);

        // Configure client
        $this->client->followRedirects();
        $token = new UsernamePasswordToken($guardUser, null, $this->firewall, $guardUser->getRoles());
        self::$container->get('security.token_storage')->setToken($token);
        $session = self::$container->get('session');
        $session->set("_security_{$this->firewall}", serialize($token));
        $session->save();
        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
arderyp commented 2 years ago

Posting via a traditional form works with no issue.

arderyp commented 2 years ago

I've tracked down the issue. I don'y think it has to do with panther, but is an somehow related to the _format special routing token: https://symfony.com/doc/current/routing.html#special-parameters

If I strip that logic from my route, the ajax posts just fine.