/    Sign up×
Community /Pin to ProfileBookmark

Laravel – API Test cases returns 401 Unauthorized

Hi

I am using Laravel 5.2 with Dingo to write APIs for my application. My application uses JWT to authenticate users.

I also wanted to write test cases for the API endpoints. When I hit my api endpoints using POSTMAN, they work like a charm, but the same endpoints when being hit through phpunit test cases, respond with [B]401 Unauthorized[/B]. I am using the same token value as being used in POSTMAN so my token isn’t the problem.

Following is my test case code:

[code=php]
public function testSomeTest()
{
$token = ‘Bearer xxx’;

$response = $this->get(‘/users’, [‘Authorization’ => $token]);
print_r($response->dump());
}
[/code]

I have also tried the following, but no dice

[code=php]
public function testSomeTest()
{
$token = ‘Bearer xxx’;

$response = $this->call(‘GET’, ‘/users’, [],[],[], [‘Authorization’ => $token]);
print_r($response->getContent());
}
[/code]

Here is the error output: [url]http://pastebin.com/raw/Tjirez1V[/url]

Can someone please help me figure out what could be the problem?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@phantom007authorDec 05.2016 — Why would it be a session issue when I am using JWT for authentication?
Copy linkTweet thisAlerts:
@NogDogDec 05.2016 — Sorry, didn't realize what JWT was doing.

Here's a somewhat lengthy discussion from people having similar issues and what looks like a few possibly useful suggestions: https://github.com/tymondesigns/jwt-auth/issues/206
×

Success!

Help @phantom007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...