|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +use function Fetch\Http\fetch; |
| 4 | + |
3 | 5 | use Fetch\Http\Response;
|
4 | 6 | use GuzzleHttp\Client;
|
5 | 7 | use GuzzleHttp\Exception\RequestException;
|
|
17 | 19 | $mockClient = mock(Client::class, function (MockInterface $mock) {
|
18 | 20 | $mock->shouldReceive('request')
|
19 | 21 | ->once()
|
20 |
| - ->with('GET', 'https://example.com', \Mockery::type('array')) |
| 22 | + ->with('GET', 'http://localhost', \Mockery::type('array')) |
21 | 23 | ->andReturn(new Response(200, [], json_encode(['success' => true])));
|
22 | 24 | });
|
23 | 25 |
|
24 |
| - $response = fetch('https://example.com', ['client' => $mockClient]); |
| 26 | + $response = fetch('http://localhost', ['client' => $mockClient]); |
25 | 27 |
|
26 | 28 | expect($response->json())->toBe(['success' => true]);
|
27 | 29 | expect($response->getStatusCode())->toBe(200);
|
|
34 | 36 | $mockClient = mock(Client::class, function (MockInterface $mock) {
|
35 | 37 | $mock->shouldReceive('request')
|
36 | 38 | ->once()
|
37 |
| - ->with('GET', 'https://example.com', \Mockery::type('array')) |
| 39 | + ->with('GET', 'http://localhost', \Mockery::type('array')) |
38 | 40 | ->andReturn(new Response(200, [], json_encode(['async' => 'result'])));
|
39 | 41 | });
|
40 | 42 |
|
41 |
| - async(fn () => fetch('https://example.com', ['client' => $mockClient])) |
| 43 | + async(fn () => fetch('http://localhost', ['client' => $mockClient])) |
42 | 44 | ->then(function (Response $response) {
|
43 | 45 | expect($response->json())->toBe(['async' => 'result']);
|
44 | 46 | expect($response->getStatusCode())->toBe(200);
|
|
48 | 50 | });
|
49 | 51 | });
|
50 | 52 |
|
| 53 | +test('fetch makes successful synchronous POST request using fluent API', function () { |
| 54 | + $mockClient = mock(Client::class, function (MockInterface $mock) { |
| 55 | + $mock->shouldReceive('request') |
| 56 | + ->once() |
| 57 | + ->with('POST', 'http://localhost/posts', \Mockery::type('array')) |
| 58 | + ->andReturn(new Response(201, [], json_encode(['success' => true]))); |
| 59 | + }); |
| 60 | + |
| 61 | + $response = fetch() |
| 62 | + ->setSyncClient($mockClient) // Set the mock client |
| 63 | + ->baseUri('http://localhost') |
| 64 | + ->withHeaders(['Content-Type' => 'application/json']) |
| 65 | + ->withBody(json_encode(['key' => 'value'])) |
| 66 | + ->withToken('fake-bearer-auth-token') |
| 67 | + ->post('/posts'); |
| 68 | + |
| 69 | + expect($response->json())->toBe(['success' => true]); |
| 70 | + expect($response->getStatusCode())->toBe(201); |
| 71 | +}); |
| 72 | + |
51 | 73 | /*
|
52 | 74 | * Test for sending headers with a GET request using fetch.
|
53 | 75 | */
|
54 | 76 | test('fetch sends headers with a GET request', function () {
|
55 | 77 | $mockClient = mock(Client::class, function (MockInterface $mock) {
|
56 | 78 | $mock->shouldReceive('request')
|
57 | 79 | ->once()
|
58 |
| - ->with('GET', 'https://example.com', \Mockery::on(function ($options) { |
| 80 | + ->with('GET', 'http://localhost', \Mockery::on(function ($options) { |
59 | 81 | return $options['headers']['Authorization'] === 'Bearer token';
|
60 | 82 | }))
|
61 | 83 | ->andReturn(new Response(200, [], 'Headers checked'));
|
62 | 84 | });
|
63 | 85 |
|
64 |
| - $response = fetch('https://example.com', [ |
| 86 | + $response = fetch('http://localhost', [ |
65 | 87 | 'headers' => ['Authorization' => 'Bearer token'],
|
66 | 88 | 'client' => $mockClient
|
67 | 89 | ]);
|
|
76 | 98 | $mockClient = mock(Client::class, function (MockInterface $mock) {
|
77 | 99 | $mock->shouldReceive('request')
|
78 | 100 | ->once()
|
79 |
| - ->with('GET', 'https://example.com', \Mockery::on(function ($options) { |
| 101 | + ->with('GET', 'http://localhost', \Mockery::on(function ($options) { |
80 | 102 | return $options['query'] === ['foo' => 'bar', 'baz' => 'qux'];
|
81 | 103 | }))
|
82 | 104 | ->andReturn(new Response(200, [], 'Query params checked'));
|
83 | 105 | });
|
84 | 106 |
|
85 |
| - $response = fetch('https://example.com', [ |
| 107 | + $response = fetch('http://localhost', [ |
86 | 108 | 'query' => ['foo' => 'bar', 'baz' => 'qux'],
|
87 | 109 | 'client' => $mockClient
|
88 | 110 | ]);
|
|
97 | 119 | $mockClient = mock(Client::class, function (MockInterface $mock) {
|
98 | 120 | $mock->shouldReceive('request')
|
99 | 121 | ->once()
|
100 |
| - ->with('GET', 'https://example.com', \Mockery::on(function ($options) { |
| 122 | + ->with('GET', 'http://localhost', \Mockery::on(function ($options) { |
101 | 123 | return $options['timeout'] === 1;
|
102 | 124 | }))
|
103 |
| - ->andThrow(new RequestException('Timeout', new Request('GET', 'https://example.com'))); |
| 125 | + ->andThrow(new RequestException('Timeout', new Request('GET', 'http://localhost'))); |
104 | 126 | });
|
105 | 127 |
|
106 | 128 | try {
|
107 |
| - fetch('https://example.com', ['timeout' => 1, 'client' => $mockClient]); |
| 129 | + fetch('http://localhost', ['timeout' => 1, 'client' => $mockClient]); |
108 | 130 | } catch (RequestException $e) {
|
109 | 131 | expect($e->getMessage())->toContain('Timeout');
|
110 | 132 | }
|
|
117 | 139 | $mockClient = mock(Client::class, function (MockInterface $mock) {
|
118 | 140 | $mock->shouldReceive('request')
|
119 | 141 | ->times(1) // Expecting 2 calls: 1 failed, 1 retry
|
120 |
| - ->with('GET', 'https://example.com', \Mockery::type('array')) |
121 |
| - ->andThrow(new RequestException('Failed request', new Request('GET', 'https://example.com'))); |
| 142 | + ->with('GET', 'http://localhost', \Mockery::type('array')) |
| 143 | + ->andThrow(new RequestException('Failed request', new Request('GET', 'http://localhost'))); |
122 | 144 | $mock->shouldReceive('request')
|
123 | 145 | ->times(1) // Expecting 2 calls: 1 failed, 1 retry
|
124 |
| - ->with('GET', 'https://example.com', \Mockery::type('array')) |
| 146 | + ->with('GET', 'http://localhost', \Mockery::type('array')) |
125 | 147 | ->andReturn(new Response(200, [], 'Success after retry'));
|
126 | 148 | });
|
127 | 149 |
|
128 |
| - $response = fetch('https://example.com', ['retries' => 2, 'client' => $mockClient]); |
| 150 | + $response = fetch('http://localhost', ['retries' => 2, 'client' => $mockClient]); |
129 | 151 |
|
130 | 152 | expect($response->text())->toBe('Success after retry');
|
131 | 153 | });
|
|
137 | 159 | $mockClient = mock(Client::class, function (MockInterface $mock) {
|
138 | 160 | $mock->shouldReceive('request')
|
139 | 161 | ->once()
|
140 |
| - ->with('POST', 'https://example.com/users', \Mockery::on(function ($options) { |
| 162 | + ->with('POST', 'http://localhost/users', \Mockery::on(function ($options) { |
141 | 163 | return $options['body'] === json_encode(['name' => 'John']);
|
142 | 164 | }))
|
143 | 165 | ->andReturn(new Response(201, [], 'Created'));
|
144 | 166 | });
|
145 | 167 |
|
146 |
| - $response = fetch('https://example.com/users', [ |
| 168 | + $response = fetch('http://localhost/users', [ |
147 | 169 | 'method' => 'POST',
|
148 | 170 | 'body' => json_encode(['name' => 'John']),
|
149 | 171 | 'client' => $mockClient
|
|
160 | 182 | $mockClient = mock(Client::class, function (MockInterface $mock) {
|
161 | 183 | $mock->shouldReceive('request')
|
162 | 184 | ->times(1)
|
163 |
| - ->with('GET', 'https://example.com', \Mockery::type('array')) |
164 |
| - ->andThrow(new RequestException('Failed request', new Request('GET', 'https://example.com'))); |
| 185 | + ->with('GET', 'http://localhost', \Mockery::type('array')) |
| 186 | + ->andThrow(new RequestException('Failed request', new Request('GET', 'http://localhost'))); |
165 | 187 | $mock->shouldReceive('request')
|
166 | 188 | ->times(1)
|
167 |
| - ->with('GET', 'https://example.com', \Mockery::type('array')) |
| 189 | + ->with('GET', 'http://localhost', \Mockery::type('array')) |
168 | 190 | ->andReturn(new Response(200, [], 'Success after retry'));
|
169 | 191 | });
|
170 | 192 |
|
171 |
| - async(fn () => fetch('https://example.com', ['retries' => 2, 'client' => $mockClient])) |
| 193 | + async(fn () => fetch('http://localhost', ['retries' => 2, 'client' => $mockClient])) |
172 | 194 | ->then(function (Response $response) {
|
173 | 195 | expect($response->text())->toBe('Success after retry');
|
174 | 196 | })
|
|
0 commit comments