Skip to content

Commit

Permalink
Remote: New basicAuth option #2322
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Nov 24, 2019
1 parent c010eba commit ba3e819
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
26 changes: 16 additions & 10 deletions src/Http/Remote.php
Expand Up @@ -22,16 +22,17 @@ class Remote
* @var array
*/
public static $defaults = [
'agent' => null,
'body' => true,
'data' => [],
'encoding' => 'utf-8',
'file' => null,
'headers' => [],
'method' => 'GET',
'progress' => null,
'test' => false,
'timeout' => 10,
'agent' => null,
'basicAuth' => null,
'body' => true,
'data' => [],
'encoding' => 'utf-8',
'file' => null,
'headers' => [],
'method' => 'GET',
'progress' => null,
'test' => false,
'timeout' => 10,
];

/**
Expand Down Expand Up @@ -183,6 +184,11 @@ public function fetch()
$this->curlopt[CURLOPT_HTTPHEADER] = $headers;
}

// add HTTP Basic authentication
if (empty($this->options['basicAuth']) === false) {
$this->curlopt[CURLOPT_USERPWD] = $this->options['basicAuth'];
}

// add the user agent
if (empty($this->options['agent']) === false) {
$this->curlopt[CURLOPT_USERAGENT] = $this->options['agent'];
Expand Down
32 changes: 20 additions & 12 deletions tests/Http/RemoteTest.php
Expand Up @@ -36,71 +36,79 @@ public function testOptionsHeaders()
], $request->curlopt[CURLOPT_HTTPHEADER]);
}

public function testOptionsBasicAuth()
{
$request = Remote::get('https://getkirby.com', [
'basicAuth' => 'user:pw'
]);
$this->assertSame('user:pw', $request->curlopt[CURLOPT_USERPWD]);
}

public function testContent()
{
$request = Remote::put('https://getkirby.com');
$this->assertEquals(null, $request->content());
$this->assertSame(null, $request->content());
}

public function testCode()
{
$request = Remote::put('https://getkirby.com');
$this->assertEquals(null, $request->code());
$this->assertSame(null, $request->code());
}

public function testDelete()
{
$request = Remote::delete('https://getkirby.com');
$this->assertEquals('DELETE', $request->method());
$this->assertSame('DELETE', $request->method());
}

public function testGet()
{
$request = Remote::get('https://getkirby.com');
$this->assertEquals('GET', $request->method());
$this->assertSame('GET', $request->method());
}

public function testHead()
{
$request = Remote::head('https://getkirby.com');
$this->assertEquals('HEAD', $request->method());
$this->assertSame('HEAD', $request->method());
}

public function testHeaders()
{
$request = new Remote('https://getkirby.com');
$this->assertEquals([], $request->headers());
$this->assertSame([], $request->headers());
}

public function testInfo()
{
$request = new Remote('https://getkirby.com');
$this->assertEquals([], $request->info());
$this->assertSame([], $request->info());
}

public function testPatch()
{
$request = Remote::patch('https://getkirby.com');
$this->assertEquals('PATCH', $request->method());
$this->assertSame('PATCH', $request->method());
}

public function testPost()
{
$request = Remote::post('https://getkirby.com');
$this->assertEquals('POST', $request->method());
$this->assertSame('POST', $request->method());
}

public function testPut()
{
$request = Remote::put('https://getkirby.com');
$this->assertEquals('PUT', $request->method());
$this->assertSame('PUT', $request->method());
}

public function testRequest()
{
$request = new Remote($url = 'https://getkirby.com');

$this->assertEquals($url, $request->url());
$this->assertEquals('GET', $request->method());
$this->assertSame($url, $request->url());
$this->assertSame('GET', $request->method());
}
}

0 comments on commit ba3e819

Please sign in to comment.