Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Core/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ protected function buildRequest(
...$idempotencyHeaders,
];

// Generated methods always pass `body` for operations that define a request
// body, so only elide the content-type header when the operation has no body
// at all, not when an optional body param was omitted.
if (null === $body && !array_key_exists('Content-Type', $headers)) {
unset($mergedHeaders['Content-Type']);
}

$req = ['method' => strtoupper($method), 'path' => $uri, 'query' => $mergedQuery, 'headers' => $mergedHeaders, 'body' => $body];

return [$req, $options];
Expand Down
1 change: 0 additions & 1 deletion src/Services/WebhooksRawService.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ public function delete(
$params,
$requestOptions,
);

// @phpstan-ignore-next-line return.type
return $this->client->request(
method: 'delete',
Expand Down
89 changes: 89 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,93 @@ public function testDefaultHeaders(): void
$this->assertNotEmpty($sent);
}
}

public function testBodylessDeleteOmitsContentType(): void
{
$transporter = new Client;
$transporter->setDefaultResponse(
Psr17FactoryDiscovery::findResponseFactory()->createResponse()->withStatus(204)
);

$client = new \SentDm\Client(
baseUrl: 'http://localhost',
apiKey: 'My API Key',
requestOptions: ['transporter' => $transporter],
);

$client->webhooks->delete('d4f5a6b7-c8d9-4e0f-a1b2-c3d4e5f6a7b8');

$this->assertNotFalse($requested = $transporter->getRequests()[0] ?? false);
$this->assertSame('DELETE', $requested->getMethod());
$this->assertSame('', $requested->getHeaderLine('content-type'));
$this->assertSame('', (string) $requested->getBody());
}

public function testBodylessGetOmitsContentType(): void
{
$transporter = new Client;
$mockRsp = Psr17FactoryDiscovery::findResponseFactory()
->createResponse()
->withStatus(200)
->withHeader('Content-Type', 'application/json')
->withBody(Psr17FactoryDiscovery::findStreamFactory()->createStream(json_encode([], flags: Util::JSON_ENCODE_FLAGS) ?: ''))
;
$transporter->setDefaultResponse($mockRsp);

$client = new \SentDm\Client(
baseUrl: 'http://localhost',
apiKey: 'My API Key',
requestOptions: ['transporter' => $transporter],
);

$client->messages->retrieveStatus('msg_123');

$this->assertNotFalse($requested = $transporter->getRequests()[0] ?? false);
$this->assertSame('GET', $requested->getMethod());
$this->assertSame('', $requested->getHeaderLine('content-type'));
}

public function testDeleteWithExplicitBodyKeepsContentType(): void
{
$transporter = new Client;
$transporter->setDefaultResponse(
Psr17FactoryDiscovery::findResponseFactory()->createResponse()->withStatus(204)
);

$client = new \SentDm\Client(
baseUrl: 'http://localhost',
apiKey: 'My API Key',
requestOptions: ['transporter' => $transporter],
);

$client->profiles->delete('770e8400-e29b-41d4-a716-446655440002');

$this->assertNotFalse($requested = $transporter->getRequests()[0] ?? false);
$this->assertSame('DELETE', $requested->getMethod());
$this->assertSame('application/json', $requested->getHeaderLine('content-type'));
$this->assertSame('{}', (string) $requested->getBody());
}

public function testBodylessRequestKeepsAnExplicitContentTypeOverride(): void
{
$transporter = new Client;
$transporter->setDefaultResponse(
Psr17FactoryDiscovery::findResponseFactory()->createResponse()->withStatus(204)
);

$client = new \SentDm\Client(
baseUrl: 'http://localhost',
apiKey: 'My API Key',
requestOptions: ['transporter' => $transporter],
);

$client->request(
method: 'delete',
path: 'v3/webhooks/d4f5a6b7-c8d9-4e0f-a1b2-c3d4e5f6a7b8',
headers: ['Content-Type' => 'application/json'],
);

$this->assertNotFalse($requested = $transporter->getRequests()[0] ?? false);
$this->assertSame('application/json', $requested->getHeaderLine('content-type'));
}
}