{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/14402","repository_url":"https://api.github.com/repos/laravel/framework","labels_url":"https://api.github.com/repos/laravel/framework/issues/14402/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/14402/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/14402/events","html_url":"https://github.com/laravel/framework/issues/14402","id":166720995,"number":14402,"title":"[Test][Bug] Authentication services for consecutive requests are not working.","user":{"login":"CurosMJ","id":2637884,"avatar_url":"https://avatars.githubusercontent.com/u/2637884?v=3","gravatar_id":"","url":"https://api.github.com/users/CurosMJ","html_url":"https://github.com/CurosMJ","followers_url":"https://api.github.com/users/CurosMJ/followers","following_url":"https://api.github.com/users/CurosMJ/following{/other_user}","gists_url":"https://api.github.com/users/CurosMJ/gists{/gist_id}","starred_url":"https://api.github.com/users/CurosMJ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CurosMJ/subscriptions","organizations_url":"https://api.github.com/users/CurosMJ/orgs","repos_url":"https://api.github.com/users/CurosMJ/repos","events_url":"https://api.github.com/users/CurosMJ/events{/privacy}","received_events_url":"https://api.github.com/users/CurosMJ/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-07-21T02:00:17Z","updated_at":"2016-07-25T07:47:00Z","closed_at":null,"body":"When I try to make consecutive requests (in a TestCase), like this : \r\n```php\r\npublic function testMeEndpoint()\r\n{\r\n    $this->post('/authenticate', ['email' => 'email@email.com', 'pass' => '12345']);\r\n    $token = $this->lastResponse()['data']['access_token'];\r\n\r\n    $this->get('/users/me', ['Authorization' => \"Bearer $token\"])\r\n            ->seeStatusCode(200)\r\n            ->see('first_name')\r\n            ->see('created_at');\r\n}\r\n```\r\n\r\nThe `GET` call to `/users/me` would fail with an 403 (which implies the token was not understood by the application).\r\n\r\nIf I refresh the application between the calls(`$this->refreshApplication()`), this will work fine. Like this : \r\n\r\n```php\r\npublic function testMeEndpoint()\r\n{\r\n    $this->post('/authenticate', ['email' => 'email@email.com', 'pass' => '12345']);\r\n    $token = $this->lastResponse()['data']['access_token'];\r\n\r\n    $this->refreshApplication();\r\n\r\n    $this->get('/users/me', ['Authorization' => \"Bearer $token\"])\r\n            ->seeStatusCode(200)\r\n            ->see('first_name')\r\n            ->see('created_at');\r\n}\r\n```\r\n\r\n**What I think is going wrong :**\r\n\r\nThe Application instance is reused for requests, by default. So are the services that are binded inside the Container, including the  _Guard_. \r\n\r\nThe Guard that is used for the second `GET` request is the one that was created when the first `POST` was sent. This is where it all goes wrong. It should have created a new instance of Guard, instead of just using the last one.\r\n\r\n**Possible solutions :**\r\n\r\n - Refresh / Recreate the application after/before each call.\r\n - Force the application to generate a new Guard for each call.\r\n\r\nThis is just about the Guard service, but other services might also be failing in a similar manner. \r\n\r\nFrom my POV, testing framework should mimic the real-world situation, and re-create the whole application for each request."},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/234871163","html_url":"https://github.com/laravel/framework/issues/14402#issuecomment-234871163","issue_url":"https://api.github.com/repos/laravel/framework/issues/14402","id":234871163,"user":{"login":"srmklive","id":839335,"avatar_url":"https://avatars.githubusercontent.com/u/839335?v=3","gravatar_id":"","url":"https://api.github.com/users/srmklive","html_url":"https://github.com/srmklive","followers_url":"https://api.github.com/users/srmklive/followers","following_url":"https://api.github.com/users/srmklive/following{/other_user}","gists_url":"https://api.github.com/users/srmklive/gists{/gist_id}","starred_url":"https://api.github.com/users/srmklive/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/srmklive/subscriptions","organizations_url":"https://api.github.com/users/srmklive/orgs","repos_url":"https://api.github.com/users/srmklive/repos","events_url":"https://api.github.com/users/srmklive/events{/privacy}","received_events_url":"https://api.github.com/users/srmklive/received_events","type":"User","site_admin":false},"created_at":"2016-07-25T07:47:00Z","updated_at":"2016-07-25T07:47:00Z","body":"Why don't you create the application instance for each request for you test case? You can look at the framework test cases if you want on this [link](https://github.com/laravel/framework/blob/5.2/tests/Http/)."}}
{"actor":{"display_login":"srmklive"}}