-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathHttpClientTest.php
More file actions
85 lines (72 loc) · 2.49 KB
/
HttpClientTest.php
File metadata and controls
85 lines (72 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
// @oagen-ignore-file
namespace Tests;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use WorkOS\Exception\ApiException;
use WorkOS\HttpClient;
class HttpClientTest extends TestCase
{
public function testDecodeResponseThrowsOnNonJsonBody(): void
{
$html = '<html><body>Redirect</body></html>';
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'text/html'], $html),
]);
$handler = HandlerStack::create($mock);
$client = new HttpClient(
apiKey: 'test_key',
clientId: null,
baseUrl: 'https://api.workos.com',
timeout: 10,
maxRetries: 0,
handler: $handler,
);
$this->expectException(ApiException::class);
$this->expectExceptionMessage('Expected JSON response but received non-JSON body');
$client->request('GET', '/test');
}
public function testBuildUrlOmitsQuestionMarkForEmptyQuery(): void
{
$client = new HttpClient(
apiKey: 'test_key',
clientId: null,
baseUrl: 'https://api.workos.com',
timeout: 10,
maxRetries: 0,
);
$url = $client->buildUrl('sso/authorize', []);
$this->assertSame('https://api.workos.com/sso/authorize', $url);
}
public function testBuildUrlOmitsQuestionMarkForEmptyArrayValues(): void
{
$client = new HttpClient(
apiKey: 'test_key',
clientId: null,
baseUrl: 'https://api.workos.com',
timeout: 10,
maxRetries: 0,
);
// http_build_query returns '' for arrays containing only empty arrays
$url = $client->buildUrl('sso/authorize', ['scopes' => []]);
$this->assertStringNotContainsString('?', $url);
}
public function testBuildUrlAppendsQueryString(): void
{
$client = new HttpClient(
apiKey: 'test_key',
clientId: null,
baseUrl: 'https://api.workos.com',
timeout: 10,
maxRetries: 0,
);
$url = $client->buildUrl('sso/authorize', ['client_id' => 'abc', 'response_type' => 'code']);
$this->assertStringContainsString('?', $url);
parse_str(parse_url($url, PHP_URL_QUERY) ?? '', $query);
$this->assertSame('abc', $query['client_id']);
$this->assertSame('code', $query['response_type']);
}
}