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
55 changes: 55 additions & 0 deletions Tests/Transport/HeadRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Http\Tests\Transport;

use Joomla\Http\Transport\Curl;
use Joomla\Http\Transport\Socket;
use Joomla\Http\Transport\Stream;
use Joomla\Uri\Uri;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* Test class for HEAD requests, whose response announces a body that is not sent.
*/
#[CoversClass(Curl::class)]
#[CoversClass(Socket::class)]
#[CoversClass(Stream::class)]
class HeadRequestTest extends TestCase
{
public static function transportAndMethodProvider(): \Generator
{
foreach ([Curl::class, Socket::class, Stream::class] as $transport) {
foreach (['HEAD', 'head'] as $method) {
yield substr(strrchr($transport, '\\'), 1) . ', ' . $method => [$transport, $method];
}
}
}

#[DataProvider('transportAndMethodProvider')]
public function testHeadRequestDoesNotWaitForABody(string $transportClass, string $method): void
{
if (!$transportClass::isSupported()) {
$this->markTestSkipped(sprintf('The "%s" adapter is not supported in this environment.', $transportClass));
}

if (!\defined('JTEST_HTTP_STUB') && getenv('JTEST_HTTP_STUB') == '') {
$this->markTestSkipped('The Transport test stub has not been configured');
}

$stubUrl = \defined('JTEST_HTTP_STUB') ? JTEST_HTTP_STUB : getenv('JTEST_HTTP_STUB');
$url = substr($stubUrl, 0, strrpos($stubUrl, '/') + 1) . 'content_length.php';

$response = (new $transportClass([]))->request($method, new Uri($url), null, [], 5);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('100', $response->getHeaderLine('Content-Length'));
$this->assertSame('', (string) $response->getBody());
}
}
16 changes: 16 additions & 0 deletions Tests/stubs/content_length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

/**
* Stub for the transport tests that announces a 100 byte body with Content-Length. The web server
* omits the body for a HEAD request, so a client that waits for it after a HEAD request fails.
*/

header('Content-Type: text/plain');
header('Content-Length: 100');

echo str_repeat('x', 100);
4 changes: 3 additions & 1 deletion docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Additionally, the main `Http` class provides support for sending requests as cov

### Making a HEAD request

An HTTP HEAD request can be made using the head method passing a URL and an optional key-value array of header variables.
An HTTP HEAD request can be made using the head method passing a URL and an optional key-value array of header variables. The
response has no body, also when the server announces one with `Content-Length`; this holds for the method name in any
case, e.g. `head` in a PSR-7 request sent with `sendRequest()`.

```php
use Joomla\Http\HttpFactory;
Expand Down
2 changes: 1 addition & 1 deletion src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function request($method, UriInterface $uri, $data = null, array $headers
}

// Don't wait for body when $method is HEAD
$options[\CURLOPT_NOBODY] = ($method === 'HEAD');
$options[\CURLOPT_NOBODY] = (strtoupper($method) === 'HEAD');

// If data exists let's encode it and make sure our Content-type header is set.
if (isset($data)) {
Expand Down
Loading