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

use Joomla\Http\Http;
use Joomla\Http\HttpFactory;
use Joomla\Http\Response;
use Joomla\Http\TransportInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* Test class for the default user agent.
*/
#[CoversClass(Http::class)]
#[CoversClass(HttpFactory::class)]
class HttpUserAgentTest extends TestCase
{
/**
* Create a transport that expects the given user agent.
*
* @param string $userAgent The expected user agent.
*
* @return TransportInterface
*/
private function createTransportExpectingUserAgent(string $userAgent): TransportInterface
{
$transport = $this->createMock(TransportInterface::class);
$transport->expects($this->once())
->method('request')
->with('GET', $this->anything(), null, [], null, $userAgent)
->willReturn(new Response());

return $transport;
}

public function testDefaultUserAgentIsSentByAClientCreatedDirectly(): void
{
(new Http([], $this->createTransportExpectingUserAgent('Mozilla/5.0 Joomla!/Framework')))->get('http://example.com');
}

public function testConfiguredUserAgentIsSent(): void
{
(new Http(['userAgent' => 'MyApp/1.0'], $this->createTransportExpectingUserAgent('MyApp/1.0')))->get('http://example.com');
}

public function testFactoryDoesNotChangeTheOptionsObject(): void
{
$options = new \ArrayObject(['timeout' => 5]);

(new HttpFactory())->getHttp($options);

$this->assertSame(['timeout' => 5], $options->getArrayCopy());
}
}
4 changes: 4 additions & 0 deletions docs/classes/HttpFactory.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Note that an `InvalidArgumentException` is thrown if the `$options` argument is

Note that a `RuntimeException` is thrown if no `Joomla\Http\TransportInterface` objects are available for use.

Requests are sent with the user agent `Mozilla/5.0 Joomla!/Framework` unless the `userAgent` option sets another
one. `Http` applies this default itself, so it also holds for a client created with `new Http()`, and the options
array or object you pass in is not modified.

### Retrieving a Joomla\Http\TransportInterface object

The `getAvailableDriver()` method can be used to create [TransportInterface](TransportInterface.md) objects from the factory.
Expand Down
3 changes: 2 additions & 1 deletion src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ protected function makeTransportRequest($method, $url, $data = null, array $head
$timeout = $this->options['timeout'];
}

$userAgent = isset($this->options['userAgent']) ? $this->options['userAgent'] : null;
// Send the default user agent unless one is configured; the options are not changed.
$userAgent = $this->options['userAgent'] ?? 'Mozilla/5.0 Joomla!/Framework';

// Convert to a Uri object if we were given a string
if (\is_string($url)) {
Expand Down
5 changes: 0 additions & 5 deletions src/HttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public function getHttp($options = [], $adapters = null)
);
}

// Set default userAgent if nothing else is set
if (!isset($options['userAgent'])) {
$options['userAgent'] = 'Mozilla/5.0 Joomla!/Framework';
}

if (!$driver = $this->getAvailableDriver($options, $adapters)) {
throw new \RuntimeException('No transport driver available.');
}
Expand Down
Loading