Skip to content

Commit f723936

Browse files
committed
Update the HTTP client to reflect the upstream B/C break
1 parent 5ed6f4f commit f723936

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

src/Twilio/Http/LaravelHttpClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BabDev\Twilio\Twilio\Http;
44

55
use Illuminate\Http\Client\Factory;
6+
use Twilio\AuthStrategy\AuthStrategy;
67
use Twilio\Exceptions\HttpException;
78
use Twilio\Http\Client;
89
use Twilio\Http\Response;
@@ -25,11 +26,14 @@ public function request(
2526
string $user = null,
2627
string $password = null,
2728
int $timeout = null,
29+
?AuthStrategy $authStrategy = null,
2830
): Response {
2931
$request = $this->httpFactory->asForm();
3032

3133
if ($user && $password) {
3234
$request->withBasicAuth($user, $password);
35+
} elseif ($authStrategy instanceof AuthStrategy) {
36+
$request->withHeader('Authorization', $authStrategy->getAuthString());
3337
}
3438

3539
$request->withHeaders($headers);

tests/Twilio/Http/LaravelHttpClientTest.php

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Http\Client\Request;
88
use Illuminate\Http\Client\Response;
99
use Orchestra\Testbench\TestCase;
10+
use Twilio\AuthStrategy\BasicAuthStrategy;
1011
use Twilio\Exceptions\HttpException;
1112

1213
final class LaravelHttpClientTest extends TestCase
@@ -23,11 +24,9 @@ public function testARequestCanBeSentToTheTwilioApiWithoutCredentials(): void
2324

2425
/** @var Factory $factory */
2526
$factory = $this->app->make(Factory::class);
26-
$factory->fake(
27-
[
28-
$url => $factory->response('', 200, []),
29-
]
30-
);
27+
$factory->fake([
28+
$url => $factory->response('', 200, []),
29+
]);
3130

3231
(new LaravelHttpClient($factory))->request(
3332
'POST',
@@ -37,9 +36,11 @@ public function testARequestCanBeSentToTheTwilioApiWithoutCredentials(): void
3736
$headers
3837
);
3938

40-
$factory->assertSent(static fn(Request $request, Response $response): bool => !$request->hasHeader('Authorization')
39+
$factory->assertSent(
40+
static fn(Request $request, Response $response): bool => !$request->hasHeader('Authorization')
4141
&& $request->url() === $url
42-
&& $request->data() === $messageData);
42+
&& $request->data() === $messageData
43+
);
4344
}
4445

4546
public function testARequestCanBeSentToTheTwilioApiWithCredentials(): void
@@ -54,11 +55,9 @@ public function testARequestCanBeSentToTheTwilioApiWithCredentials(): void
5455

5556
/** @var Factory $factory */
5657
$factory = $this->app->make(Factory::class);
57-
$factory->fake(
58-
[
59-
$url => $factory->response('', 200, []),
60-
]
61-
);
58+
$factory->fake([
59+
$url => $factory->response('', 200, []),
60+
]);
6261

6362
(new LaravelHttpClient($factory))->request(
6463
'POST',
@@ -70,9 +69,46 @@ public function testARequestCanBeSentToTheTwilioApiWithCredentials(): void
7069
'password'
7170
);
7271

73-
$factory->assertSent(static fn(Request $request, Response $response): bool => $request->hasHeader('Authorization')
72+
$factory->assertSent(
73+
static fn(Request $request, Response $response): bool => $request->hasHeader('Authorization')
7474
&& $request->url() === $url
75-
&& $request->data() === $messageData);
75+
&& $request->data() === $messageData
76+
);
77+
}
78+
79+
public function testARequestCanBeSentToTheTwilioApiWithAuthStrategy(): void
80+
{
81+
$url = 'https://api.twilio.com/2010-04-01/Accounts/SID/Messages.json';
82+
$headers = [];
83+
$messageData = [
84+
'From' => '+16512432364',
85+
'To' => '+18003285920',
86+
'Body' => 'Test Message',
87+
];
88+
89+
/** @var Factory $factory */
90+
$factory = $this->app->make(Factory::class);
91+
$factory->fake([
92+
$url => $factory->response('', 200, []),
93+
]);
94+
95+
(new LaravelHttpClient($factory))->request(
96+
'POST',
97+
$url,
98+
[],
99+
$messageData,
100+
$headers,
101+
null,
102+
null,
103+
null,
104+
new BasicAuthStrategy('username', 'password'),
105+
);
106+
107+
$factory->assertSent(
108+
static fn(Request $request, Response $response): bool => $request->hasHeader('Authorization')
109+
&& $request->url() === $url
110+
&& $request->data() === $messageData
111+
);
76112
}
77113

78114
public function testAnExceptionIsThrownWhenThereIsAnErrorPerformingTheRequest(): void
@@ -89,13 +125,11 @@ public function testAnExceptionIsThrownWhenThereIsAnErrorPerformingTheRequest():
89125

90126
/** @var Factory $factory */
91127
$factory = $this->app->make(Factory::class);
92-
$factory->fake(
93-
[
94-
$url => static function (): void {
95-
throw new \RuntimeException('Testing');
96-
},
97-
]
98-
);
128+
$factory->fake([
129+
$url => static function (): void {
130+
throw new \RuntimeException('Testing');
131+
},
132+
]);
99133

100134
(new LaravelHttpClient($factory))->request(
101135
'POST',

0 commit comments

Comments
 (0)