Skip to content

Commit fb2c764

Browse files
wundiiawu
andauthored
fix: httpclient - header return 100-continue and write queue with empty value (#29)
Co-authored-by: awu <[email protected]>
1 parent d1a199e commit fb2c764

File tree

5 files changed

+110
-15
lines changed

5 files changed

+110
-15
lines changed

src/Stream/CurlFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ public static function create(
3434
$contentType = null;
3535
$options[CURLOPT_HEADERFUNCTION] = function (?CurlHandle $curlHandle, string $header) use (&$queueHeader, &$contentType): int {
3636
$queueHeader->write($header);
37+
3738
if (preg_match('/^Content-Type:\s*(.+)$/i', $header, $matches)) {
3839
$contentType = strtolower(trim($matches[1]));
3940
}
41+
4042
return strlen($header);
4143
};
4244

src/Stream/HttpClient.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,46 @@ public function buildUri(string $uri): string
3131
return $buildUri;
3232
}
3333

34-
public function get(string $uri, ?string $apiToken = null): Response
34+
public function buildHeaders(?string $apiToken, null|array|object $body = null): array
3535
{
36-
$header = $apiToken !== null ? ['Authorization: Bearer ' . $apiToken] : [];
36+
$headers = ['Expect:'];
37+
if ($apiToken !== null) {
38+
$headers[] = 'Authorization: Bearer ' . $apiToken;
39+
}
40+
if ($body !== null) {
41+
$headers[] = 'Content-Type: application/json';
42+
}
43+
44+
return $headers;
45+
}
46+
47+
public function buildBody(null|array|object $body): string
48+
{
49+
if ($body === null) {
50+
return '';
51+
}
3752

53+
return json_encode($body);
54+
}
55+
56+
public function get(string $uri, ?string $apiToken = null): Response
57+
{
3858
$request = new Request(
3959
'GET',
4060
$this->buildUri($uri),
41-
$header,
61+
$this->buildHeaders($apiToken),
4262
);
4363

4464
return $this->sendRequest($request);
4565
}
4666

47-
public function post(string $uri, ?string $apiToken = null, null|array|object $jsonValue = null): Response
67+
public function post(string $uri, ?string $apiToken = null, null|array|object $body = null): Response
4868
{
49-
$header = [];
50-
if ($apiToken !== null) {
51-
$header[] = 'Authorization: Bearer ' . $apiToken;
52-
}
53-
if ($jsonValue !== null) {
54-
$header[] = 'Content-Type: application/json';
55-
}
56-
5769
$request = new Request(
5870
'POST',
5971
$this->buildUri($uri),
60-
$header,
61-
json_encode($jsonValue),
72+
$this->buildHeaders($apiToken, $body),
73+
$this->buildBody($body),
6274
);
6375

6476
return $this->sendRequest($request);

src/Stream/Queue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function read(): string
3232

3333
public function write(string $data): void
3434
{
35-
if ($data === '') {
35+
if (trim($data) === '') {
3636
return;
3737
}
3838

tests/Stream/HttpClientTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,75 @@ public static function callContentTypes(): \Iterator
9797
false,
9898
];
9999
}
100+
101+
public function testReturnsOnlyAuthorizationHeader(): void
102+
{
103+
$httpClient = new HttpClient();
104+
$headers = $httpClient->buildHeaders('secret');
105+
106+
$this->assertCount(2, $headers);
107+
$this->assertContains('Expect:', $headers, 'Ignore curl response header 100-continue');
108+
$this->assertContains('Authorization: Bearer secret', $headers);
109+
}
110+
111+
public function testReturnsAuthorizationAndJsonHeaderWithArrayBody(): void
112+
{
113+
$httpClient = new HttpClient();
114+
$headers = $httpClient->buildHeaders('secret', [
115+
'foo' => 'bar',
116+
]);
117+
118+
$this->assertCount(3, $headers);
119+
$this->assertContains('Expect:', $headers, 'Ignore curl response header 100-continue');
120+
$this->assertContains('Authorization: Bearer secret', $headers);
121+
$this->assertContains('Content-Type: application/json', $headers);
122+
}
123+
124+
public function testReturnsJsonHeaderWithoutToken(): void
125+
{
126+
$httpClient = new HttpClient();
127+
$headers = $httpClient->buildHeaders(null, [
128+
'data' => 123,
129+
]);
130+
131+
$this->assertCount(2, $headers);
132+
$this->assertContains('Expect:', $headers, 'Ignore curl response header 100-continue');
133+
$this->assertContains('Content-Type: application/json', $headers);
134+
}
135+
136+
public function testReturnsEmptyHeadersWhenNoArgumentsGiven(): void
137+
{
138+
$httpClient = new HttpClient();
139+
$headers = $httpClient->buildHeaders(null);
140+
$this->assertSame(['Expect:'], $headers);
141+
}
142+
143+
public function testReturnsEmptyStringForNull(): void
144+
{
145+
$httpClient = new HttpClient();
146+
$result = $httpClient->buildBody(null);
147+
$this->assertSame('', $result);
148+
}
149+
150+
public function testReturnsJsonEncodedStringForArray(): void
151+
{
152+
$array = [
153+
'foo' => 'bar',
154+
];
155+
156+
$httpClient = new HttpClient();
157+
$result = $httpClient->buildBody($array);
158+
$this->assertSame(json_encode($array), $result);
159+
}
160+
161+
public function testReturnsJsonEncodedStringForObject(): void
162+
{
163+
$obj = (object) [
164+
'baz' => 'qux',
165+
];
166+
167+
$httpClient = new HttpClient();
168+
$result = $httpClient->buildBody($obj);
169+
$this->assertSame(json_encode($obj), $result);
170+
}
100171
}

tests/Stream/QueueTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,14 @@ public function testGetIteratorReturnsAllItemsInOrder(): void
6262
$items = iterator_to_array($queue);
6363
$this->assertSame(['first', 'second'], $items);
6464
}
65+
66+
public function testWriteTabAndLineBreak(): void
67+
{
68+
$queue = new Queue();
69+
$queue->write("\n");
70+
$queue->write("\t");
71+
$queue->write("\t\n");
72+
73+
$this->assertTrue($queue->isEmpty());
74+
}
6575
}

0 commit comments

Comments
 (0)