77use Illuminate \Http \Client \Request ;
88use Illuminate \Http \Client \Response ;
99use Orchestra \Testbench \TestCase ;
10+ use Twilio \AuthStrategy \BasicAuthStrategy ;
1011use Twilio \Exceptions \HttpException ;
1112
1213final 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