Behatch / contexts

Behat extension with most custom helper steps
Other
393 stars 203 forks source link

Cookies are emptied between requests. #271

Open brice opened 5 years ago

brice commented 5 years ago

Hello to all,

Recently we upgraded behatch contexts from 2.5 version to 2.8.1 with several upgrade and i encounter a problem with my API's tests.

So basically i send a request to a page that connect an user then

  Scenario: Simple scenario that 
    Given I am connected as user
    When I send a "GET" request to "/rest/user"
    Then the response status code should be 200
    When I send a "GET" request to "/rest/user"
    Then the response status code should be 200

Before the update this code use to work, after it the last context return this message :

Current response status code is 403, but 200 expected. (Behat\Mink\Exception\ExpectationException)

The function related to the context I am connected as user extends RawMinkContext and simply set a session ID in cookies.

    public function iAmConnectedAs($login)
    {
        $loginUrl = $this->locatePath('/login.php');
        $this->getSession()->setCookie('PHPSESSID', uniqid());
        $this->request('POST', $loginUrl, ['login' => $login, 'passwd' => $login]);
    }

During a debug session i saw that Cookies are emptied between sessions. So that explains the 403 response but why cookies are emptied? Did anyone encountered this after an update? Let me know if i have something to add.

I also write a question on StackOverflow

Linkinou commented 5 years ago

Hi @brice

I'm pretty sure I'm facing the same issue, did you find a solution ?

Everytime the first request is working but then the others fail with that error : The string '' is not valid json (Exception)

If I remove the first request then the 2nd one works...etc

[EDIT]

Ok it seems to come from the resetHeaders happening between each step. Kind of complicating things

brice commented 5 years ago

Did you achieve to resolve it?

Linkinou commented 5 years ago

Not yet but still working on it. It might be something particular to my case : My firewall denies access to the API (because no more header) and send a 401 back. Then behatch (or something else) receives this 401 and considered it invalid because there's no content.

That's why I'm having this weird message : The string '' is not valid json (Exception)

kelleyc commented 3 years ago

I'm running into the same issue. I'm trying to login in the first step and use cookies in the response in subsequent steps within the same scenario, but due to resetHttpHeaders all cookies are cleared between requests.

It looks like this was introduced in https://github.com/Behatch/contexts/issues/163, but shouldn't that have happened between scenarios instead of after every request? Is it intentional that we should never be able to share cookies between steps in the same scenario?

gnutix commented 3 years ago

We are having the same issue. @vincentchalamon @sanpii any chance you might be able to help, as you both worked on https://github.com/Behatch/contexts/pull/164 ?