|
4 | 4 |
|
5 | 5 | use Illuminate\Http\Client\Factory;
|
6 | 6 | use Illuminate\Http\Client\PendingRequest;
|
| 7 | +use Illuminate\Http\Client\Request; |
7 | 8 | use Illuminate\Support\Str;
|
8 | 9 | use OutOfBoundsException;
|
9 | 10 | use PHPUnit\Framework\TestCase;
|
10 | 11 |
|
11 | 12 | class HttpClientTest extends TestCase
|
12 | 13 | {
|
| 14 | + /** |
| 15 | + * @var \Illuminate\Http\Client\Factory |
| 16 | + */ |
| 17 | + protected $factory; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + $this->factory = new Factory; |
| 24 | + } |
| 25 | + |
13 | 26 | public function testStubbedResponsesAreReturnedAfterFaking()
|
14 | 27 | {
|
15 |
| - $factory = new Factory; |
16 |
| - $factory->fake(); |
| 28 | + $this->factory->fake(); |
17 | 29 |
|
18 |
| - $response = $factory->post('http://laravel.com/test-missing-page'); |
| 30 | + $response = $this->factory->post('http://laravel.com/test-missing-page'); |
19 | 31 |
|
20 | 32 | $this->assertTrue($response->ok());
|
21 | 33 | }
|
22 | 34 |
|
23 | 35 | public function testUrlsCanBeStubbedByPath()
|
24 | 36 | {
|
25 |
| - $factory = new Factory; |
26 |
| - |
27 |
| - $factory->fake([ |
| 37 | + $this->factory->fake([ |
28 | 38 | 'foo.com/*' => ['page' => 'foo'],
|
29 | 39 | 'bar.com/*' => ['page' => 'bar'],
|
30 | 40 | '*' => ['page' => 'fallback'],
|
31 | 41 | ]);
|
32 | 42 |
|
33 |
| - $fooResponse = $factory->post('http://foo.com/test'); |
34 |
| - $barResponse = $factory->post('http://bar.com/test'); |
35 |
| - $fallbackResponse = $factory->post('http://fallback.com/test'); |
| 43 | + $fooResponse = $this->factory->post('http://foo.com/test'); |
| 44 | + $barResponse = $this->factory->post('http://bar.com/test'); |
| 45 | + $fallbackResponse = $this->factory->post('http://fallback.com/test'); |
36 | 46 |
|
37 |
| - $this->assertEquals('foo', $fooResponse['page']); |
38 |
| - $this->assertEquals('bar', $barResponse['page']); |
39 |
| - $this->assertEquals('fallback', $fallbackResponse['page']); |
| 47 | + $this->assertSame('foo', $fooResponse['page']); |
| 48 | + $this->assertSame('bar', $barResponse['page']); |
| 49 | + $this->assertSame('fallback', $fallbackResponse['page']); |
40 | 50 |
|
41 |
| - $factory->assertSent(function ($request) { |
| 51 | + $this->factory->assertSent(function (Request $request) { |
42 | 52 | return $request->url() === 'http://foo.com/test' &&
|
43 | 53 | $request->hasHeader('Content-Type', 'application/json');
|
44 | 54 | });
|
45 | 55 | }
|
46 | 56 |
|
47 | 57 | public function testCanSendJsonData()
|
48 | 58 | {
|
49 |
| - $factory = new Factory; |
50 |
| - |
51 |
| - $factory->fake(); |
| 59 | + $this->factory->fake(); |
52 | 60 |
|
53 |
| - $fooResponse = $factory->withHeaders([ |
| 61 | + $this->factory->withHeaders([ |
54 | 62 | 'X-Test-Header' => 'foo',
|
55 | 63 | ])->post('http://foo.com/json', [
|
56 | 64 | 'name' => 'Taylor',
|
57 | 65 | ]);
|
58 | 66 |
|
59 |
| - $factory->assertSent(function ($request) { |
| 67 | + $this->factory->assertSent(function (Request $request) { |
60 | 68 | return $request->url() === 'http://foo.com/json' &&
|
61 | 69 | $request->hasHeader('Content-Type', 'application/json') &&
|
62 | 70 | $request->hasHeader('X-Test-Header', 'foo') &&
|
63 |
| - $request['name'] == 'Taylor'; |
| 71 | + $request['name'] === 'Taylor'; |
64 | 72 | });
|
65 | 73 | }
|
66 | 74 |
|
67 | 75 | public function testCanSendFormData()
|
68 | 76 | {
|
69 |
| - $factory = new Factory; |
| 77 | + $this->factory->fake(); |
70 | 78 |
|
71 |
| - $factory->fake(); |
72 |
| - |
73 |
| - $fooResponse = $factory->asForm()->post('http://foo.com/form', [ |
| 79 | + $this->factory->asForm()->post('http://foo.com/form', [ |
74 | 80 | 'name' => 'Taylor',
|
75 | 81 | 'title' => 'Laravel Developer',
|
76 | 82 | ]);
|
77 | 83 |
|
78 |
| - $factory->assertSent(function ($request) { |
| 84 | + $this->factory->assertSent(function (Request $request) { |
79 | 85 | return $request->url() === 'http://foo.com/form' &&
|
80 | 86 | $request->hasHeader('Content-Type', 'application/x-www-form-urlencoded') &&
|
81 |
| - $request['name'] == 'Taylor'; |
| 87 | + $request['name'] === 'Taylor'; |
82 | 88 | });
|
83 | 89 | }
|
84 | 90 |
|
85 | 91 | public function testCanSendMultipartData()
|
86 | 92 | {
|
87 |
| - $factory = new Factory; |
88 |
| - |
89 |
| - $factory->fake(); |
| 93 | + $this->factory->fake(); |
90 | 94 |
|
91 |
| - $fooResponse = $factory->asMultipart()->post('http://foo.com/multipart', [ |
| 95 | + $this->factory->asMultipart()->post('http://foo.com/multipart', [ |
92 | 96 | [
|
93 | 97 | 'name' => 'foo',
|
94 | 98 | 'contents' => 'data',
|
95 | 99 | 'headers' => ['X-Test-Header' => 'foo'],
|
96 | 100 | ],
|
97 | 101 | ]);
|
98 | 102 |
|
99 |
| - $factory->assertSent(function ($request) { |
| 103 | + $this->factory->assertSent(function (Request $request) { |
100 | 104 | return $request->url() === 'http://foo.com/multipart' &&
|
101 | 105 | Str::startsWith($request->header('Content-Type')[0], 'multipart') &&
|
102 |
| - $request[0]['name'] == 'foo'; |
| 106 | + $request[0]['name'] === 'foo'; |
103 | 107 | });
|
104 | 108 | }
|
105 | 109 |
|
106 | 110 | public function testFilesCanBeAttached()
|
107 | 111 | {
|
108 |
| - $factory = new Factory; |
| 112 | + $this->factory->fake(); |
109 | 113 |
|
110 |
| - $factory->fake(); |
| 114 | + $this->factory->attach('foo', 'data', 'file.txt', ['X-Test-Header' => 'foo']) |
| 115 | + ->post('http://foo.com/file'); |
111 | 116 |
|
112 |
| - $fooResponse = $factory |
113 |
| - ->attach('foo', 'data', 'file.txt', ['X-Test-Header' => 'foo']) |
114 |
| - ->post('http://foo.com/file'); |
115 |
| - |
116 |
| - $factory->assertSent(function ($request) { |
| 117 | + $this->factory->assertSent(function (Request $request) { |
117 | 118 | return $request->url() === 'http://foo.com/file' &&
|
118 | 119 | Str::startsWith($request->header('Content-Type')[0], 'multipart') &&
|
119 |
| - $request[0]['name'] == 'foo' && |
| 120 | + $request[0]['name'] === 'foo' && |
120 | 121 | $request->hasFile('foo', 'data', 'file.txt');
|
121 | 122 | });
|
122 | 123 | }
|
123 | 124 |
|
124 | 125 | public function testSequenceBuilder()
|
125 | 126 | {
|
126 |
| - $factory = new Factory; |
127 |
| - |
128 |
| - $factory->fake([ |
129 |
| - '*' => $factory->sequence() |
| 127 | + $this->factory->fake([ |
| 128 | + '*' => $this->factory->sequence() |
130 | 129 | ->push('Ok', 201)
|
131 | 130 | ->push(['fact' => 'Cats are great!'])
|
132 | 131 | ->pushFile(__DIR__.'/fixtures/test.txt')
|
133 | 132 | ->pushStatus(403),
|
134 | 133 | ]);
|
135 | 134 |
|
136 |
| - /** @var PendingRequest $factory */ |
137 |
| - $response = $factory->get('https://example.com'); |
| 135 | + /** @var PendingRequest */ |
| 136 | + $response = $this->factory->get('https://example.com'); |
138 | 137 | $this->assertSame('Ok', $response->body());
|
139 | 138 | $this->assertSame(201, $response->status());
|
140 | 139 |
|
141 |
| - $response = $factory->get('https://example.com'); |
| 140 | + $response = $this->factory->get('https://example.com'); |
142 | 141 | $this->assertSame(['fact' => 'Cats are great!'], $response->json());
|
143 | 142 | $this->assertSame(200, $response->status());
|
144 | 143 |
|
145 |
| - $response = $factory->get('https://example.com'); |
146 |
| - $this->assertSame("This is a story about something that happened long ago when your grandfather was a child.\n", $response->body()); |
| 144 | + $response = $this->factory->get('https://example.com'); |
| 145 | + $this->assertSame('This is a story about something that happened long ago when your grandfather was a child.'.PHP_EOL, $response->body()); |
147 | 146 | $this->assertSame(200, $response->status());
|
148 | 147 |
|
149 |
| - $response = $factory->get('https://example.com'); |
| 148 | + $response = $this->factory->get('https://example.com'); |
150 | 149 | $this->assertSame('', $response->body());
|
151 | 150 | $this->assertSame(403, $response->status());
|
152 | 151 |
|
153 | 152 | $this->expectException(OutOfBoundsException::class);
|
154 | 153 |
|
155 | 154 | // The sequence is empty, it should throw an exception.
|
156 |
| - $factory->get('https://example.com'); |
| 155 | + $this->factory->get('https://example.com'); |
157 | 156 | }
|
158 | 157 |
|
159 | 158 | public function testSequenceBuilderCanKeepGoingWhenEmpty()
|
160 | 159 | {
|
161 |
| - $factory = new Factory; |
162 |
| - |
163 |
| - $factory->fake([ |
164 |
| - '*' => $factory->sequence() |
| 160 | + $this->factory->fake([ |
| 161 | + '*' => $this->factory->sequence() |
165 | 162 | ->dontFailWhenEmpty()
|
166 | 163 | ->push('Ok'),
|
167 | 164 | ]);
|
168 | 165 |
|
169 |
| - /** @var PendingRequest $factory */ |
170 |
| - $response = $factory->get('https://laravel.com'); |
| 166 | + $response = $this->factory->get('https://laravel.com'); |
171 | 167 | $this->assertSame('Ok', $response->body());
|
172 | 168 |
|
173 | 169 | // The sequence is empty, but it should not fail.
|
174 |
| - $factory->get('https://laravel.com'); |
| 170 | + $this->factory->get('https://laravel.com'); |
175 | 171 | }
|
176 | 172 |
|
177 | 173 | public function testAssertSequencesAreEmpty()
|
178 | 174 | {
|
179 |
| - $factory = new Factory; |
180 |
| - |
181 |
| - $factory->fake([ |
182 |
| - '*' => $factory->sequence() |
| 175 | + $this->factory->fake([ |
| 176 | + '*' => $this->factory->sequence() |
183 | 177 | ->push('1')
|
184 | 178 | ->push('2'),
|
185 | 179 | ]);
|
186 | 180 |
|
187 |
| - /** @var PendingRequest $factory */ |
188 |
| - $factory->get('https://example.com'); |
189 |
| - $factory->get('https://example.com'); |
| 181 | + $this->factory->get('https://example.com'); |
| 182 | + $this->factory->get('https://example.com'); |
190 | 183 |
|
191 |
| - $factory->assertSequencesAreEmpty(); |
| 184 | + $this->factory->assertSequencesAreEmpty(); |
192 | 185 | }
|
193 | 186 |
|
194 | 187 | public function testFakeSequence()
|
195 | 188 | {
|
196 |
| - $factory = new Factory; |
197 |
| - |
198 |
| - $factory->fakeSequence() |
| 189 | + $this->factory->fakeSequence() |
199 | 190 | ->pushStatus(201)
|
200 | 191 | ->pushStatus(301);
|
201 | 192 |
|
202 |
| - /** @var PendingRequest $factory */ |
203 |
| - $this->assertSame(201, $factory->get('https://example.com')->status()); |
204 |
| - $this->assertSame(301, $factory->get('https://example.com')->status()); |
| 193 | + $this->assertSame(201, $this->factory->get('https://example.com')->status()); |
| 194 | + $this->assertSame(301, $this->factory->get('https://example.com')->status()); |
205 | 195 | }
|
206 | 196 | }
|
0 commit comments