7
7
use Illuminate \Http \Client \Request ;
8
8
use Illuminate \Http \Client \Response ;
9
9
use Orchestra \Testbench \TestCase ;
10
+ use Twilio \AuthStrategy \BasicAuthStrategy ;
10
11
use Twilio \Exceptions \HttpException ;
11
12
12
13
final class LaravelHttpClientTest extends TestCase
@@ -23,11 +24,9 @@ public function testARequestCanBeSentToTheTwilioApiWithoutCredentials(): void
23
24
24
25
/** @var Factory $factory */
25
26
$ 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
+ ]);
31
30
32
31
(new LaravelHttpClient ($ factory ))->request (
33
32
'POST ' ,
@@ -37,9 +36,11 @@ public function testARequestCanBeSentToTheTwilioApiWithoutCredentials(): void
37
36
$ headers
38
37
);
39
38
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 ' )
41
41
&& $ request ->url () === $ url
42
- && $ request ->data () === $ messageData );
42
+ && $ request ->data () === $ messageData
43
+ );
43
44
}
44
45
45
46
public function testARequestCanBeSentToTheTwilioApiWithCredentials (): void
@@ -54,11 +55,9 @@ public function testARequestCanBeSentToTheTwilioApiWithCredentials(): void
54
55
55
56
/** @var Factory $factory */
56
57
$ 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
+ ]);
62
61
63
62
(new LaravelHttpClient ($ factory ))->request (
64
63
'POST ' ,
@@ -70,9 +69,46 @@ public function testARequestCanBeSentToTheTwilioApiWithCredentials(): void
70
69
'password '
71
70
);
72
71
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 ' )
74
74
&& $ 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
+ );
76
112
}
77
113
78
114
public function testAnExceptionIsThrownWhenThereIsAnErrorPerformingTheRequest (): void
@@ -89,13 +125,11 @@ public function testAnExceptionIsThrownWhenThereIsAnErrorPerformingTheRequest():
89
125
90
126
/** @var Factory $factory */
91
127
$ 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
+ ]);
99
133
100
134
(new LaravelHttpClient ($ factory ))->request (
101
135
'POST ' ,
0 commit comments