-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathAuthenticatorTest.php
More file actions
158 lines (111 loc) · 6.03 KB
/
AuthenticatorTest.php
File metadata and controls
158 lines (111 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
declare(strict_types=1);
use Saloon\Http\PendingRequest;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\Http\Auth\TokenAuthenticator;
use Saloon\Tests\Fixtures\Requests\UserRequest;
use Saloon\Tests\Fixtures\Connectors\TestConnector;
use Saloon\Exceptions\MissingAuthenticatorException;
use Saloon\Tests\Fixtures\Requests\RequiresAuthRequest;
use Saloon\Tests\Fixtures\Authenticators\PizzaAuthenticator;
use Saloon\Tests\Fixtures\Requests\BootAuthenticatorRequest;
use Saloon\Tests\Fixtures\Requests\AuthenticatorPluginRequest;
use Saloon\Tests\Fixtures\Requests\DefaultAuthenticatorRequest;
use Saloon\Tests\Fixtures\Connectors\DefaultAuthenticatorConnector;
use Saloon\Tests\Fixtures\Requests\DefaultPizzaAuthenticatorRequest;
test('you can add an authenticator to a request and it will be applied', function () {
$request = new DefaultAuthenticatorRequest();
$pendingRequest = connector()->createPendingRequest($request);
expect($pendingRequest->headers()->get('Authorization'))->toEqual('Bearer yee-haw-request');
});
test('you can provide a default authenticator on the connector', function () {
$request = new UserRequest();
$connector = new DefaultAuthenticatorConnector;
$pendingRequest = $connector->createPendingRequest($request);
expect($pendingRequest->headers()->get('Authorization'))->toEqual('Bearer yee-haw-connector');
});
test('you can provide a default authenticator on the request and it takes priority over the connector', function () {
$request = new DefaultAuthenticatorRequest();
$connector = new DefaultAuthenticatorConnector;
$pendingRequest = $connector->createPendingRequest($request);
expect($pendingRequest->headers()->get('Authorization'))->toEqual('Bearer yee-haw-request');
});
test('you can provide an authenticator on the fly and it will take priority over all defaults', function () {
$request = new DefaultAuthenticatorRequest();
$connector = new DefaultAuthenticatorConnector;
$request->withTokenAuth('yee-haw-on-the-fly', 'PewPew');
$pendingRequest = $connector->createPendingRequest($request);
expect($pendingRequest->headers()->get('Authorization'))->toEqual('PewPew yee-haw-on-the-fly');
});
test('the RequiresAuth trait will throw an exception if an authenticator is not found', function () {
$mockClient = new MockClient([
MockResponse::make(),
]);
$this->expectException(MissingAuthenticatorException::class);
$this->expectExceptionMessage('The "Saloon\Tests\Fixtures\Requests\RequiresAuthRequest" request requires authentication.');
$request = new RequiresAuthRequest();
connector()->send($request, $mockClient);
});
test('you can use your own authenticators', function () {
$request = new UserRequest();
$request->authenticate(new PizzaAuthenticator('Margherita', 'San Pellegrino'));
$pendingRequest = connector()->createPendingRequest($request);
$headers = $pendingRequest->headers()->all();
expect($headers['X-Pizza'])->toEqual('Margherita');
expect($headers['X-Drink'])->toEqual('San Pellegrino');
expect($pendingRequest->config()->get('debug'))->toBeTrue();
});
test('you can use your own authenticators as default', function () {
$request = new DefaultPizzaAuthenticatorRequest();
$pendingRequest = connector()->createPendingRequest($request);
$headers = $pendingRequest->headers()->all();
expect($headers['X-Pizza'])->toEqual('BBQ Chicken');
expect($headers['X-Drink'])->toEqual('Lemonade');
expect($pendingRequest->config()->get('debug'))->toBeTrue();
});
test('you can customise the authenticator inside of the boot method', function () {
$request = new BootAuthenticatorRequest();
expect($request->getAuthenticator())->toBeNull();
$pendingRequest = connector()->createPendingRequest($request);
expect($pendingRequest->getAuthenticator())->toEqual(new TokenAuthenticator('howdy-partner'));
expect($pendingRequest->headers()->get('Authorization'))->toEqual('Bearer howdy-partner');
});
test('you can customise the authenticator inside of plugins', function () {
$request = new AuthenticatorPluginRequest();
expect($request->getAuthenticator())->toBeNull();
$pendingRequest = connector()->createPendingRequest($request);
expect($pendingRequest->getAuthenticator())->toEqual(new TokenAuthenticator('plugin-auth'));
expect($pendingRequest->headers()->get('Authorization'))->toEqual('Bearer plugin-auth');
});
test('you can customise the authenticator inside of a middleware pipeline', function () {
$request = new UserRequest;
expect($request->getAuthenticator())->toBeNull();
$request->middleware()
->onRequest(function (PendingRequest $pendingRequest) {
$pendingRequest->withTokenAuth('ooh-this-is-cool');
});
$pendingRequest = connector()->createPendingRequest($request);
expect($pendingRequest->getAuthenticator())->toEqual(new TokenAuthenticator('ooh-this-is-cool'));
expect($pendingRequest->headers()->get('Authorization'))->toEqual('Bearer ooh-this-is-cool');
});
test('you can add an authenticator inside of request middleware', function () {
$request = new UserRequest;
$request->middleware()->onRequest(function (PendingRequest $pendingRequest) {
return $pendingRequest->withTokenAuth('yee-haw-request');
});
$pendingRequest = connector()->createPendingRequest($request);
expect($pendingRequest->headers()->get('Authorization'))->toEqual('Bearer yee-haw-request');
});
test('if you use the authenticate method on a fully constructed pending request it will authenticate right away', function () {
$connector = new TestConnector();
$pendingRequest = $connector->createPendingRequest(new UserRequest);
expect($pendingRequest->headers()->all())->toEqual([
'Accept' => 'application/json',
]);
$pendingRequest->authenticate(new TokenAuthenticator('yee-haw-request'));
expect($pendingRequest->headers()->all())->toEqual([
'Accept' => 'application/json',
'Authorization' => 'Bearer yee-haw-request',
]);
});