Skip to content

[7.x] Add withToken() helper method#33075

Merged
taylorotwell merged 5 commits into
laravel:7.xfrom
aryehraber:testing-authorization-helper
Jun 3, 2020
Merged

[7.x] Add withToken() helper method#33075
taylorotwell merged 5 commits into
laravel:7.xfrom
aryehraber:testing-authorization-helper

Conversation

@aryehraber

Copy link
Copy Markdown
Contributor

This PR adds a small helper method for tests using Authorization Bearer tokens as part of request headers. It was inspired by the existing $request->bearerToken() method on the Illuminate\Http\Request class, which is also really useful.

I feel this allows requests inside tests to read a little nicer, as well as helps against typos when writing "Authorization" and "Bearer" over and over, and instead IDEs can autocomplete the method.

$response = $this->withHeader('Authorization', 'Bearer test_token')->postJson('/some-endpoint');

$response = $this->withBearerToken('test_token')->postJson('/some-endpoint');

There are no breaking changes, and I've added a small test to verify everything works as expected.

Happy to make changes if there's any feedback!

@aryehraber

Copy link
Copy Markdown
Contributor Author

I just noticed that the Illuminate\Http\Client\PendingRequest class uses withToken() which is even better/shorter and allows overriding the default "Bearer" type.

I'll push an update to make this method more dynamic shortly!

@aryehraber
aryehraber marked this pull request as draft June 2, 2020 19:34
@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Please also rebase this PR against the latest 7.x codebase. :)

@aryehraber

Copy link
Copy Markdown
Contributor Author

Whoops, I think I did something wrong while rebasing! Apologies if this sent out a bunch of notifications 🤦‍♂️

@GrahamCampbell Sorry about this, what's the best way to fix this? Should I create a new PR and ensure my branch is on latest 7.x?

@lcdss

lcdss commented Jun 2, 2020

Copy link
Copy Markdown

It can be easier just copy the your commits hash sequentially (since it's not many), reset hard the branch to the latest upstream commit, cherry pick your commits and then a push force.

@aryehraber

Copy link
Copy Markdown
Contributor Author

Thanks @lcdss, I ended up doing something similar -- looks ok now (I think)

@aryehraber

Copy link
Copy Markdown
Contributor Author

The PR is now ready for review!

The method changed slightly since my original message, so here's an updated snippet to demonstrate its usefulness:

$response = $this->withHeader('Authorization', 'Bearer test_token')->postJson('/some-endpoint');

$response = $this->withToken('test_token')->postJson('/some-endpoint');

Much shorter and more readable I think — hope you agree 😊

Let me know if there's any feedback!

@aryehraber
aryehraber marked this pull request as ready for review June 2, 2020 20:44
@aryehraber aryehraber changed the title [7.x] Add withBearerToken() helper method [7.x] Add withToken() helper method Jun 2, 2020
@taylorotwell
taylorotwell merged commit 188f89f into laravel:7.x Jun 3, 2020
@aryehraber
aryehraber deleted the testing-authorization-helper branch June 3, 2020 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants