From ba3e819513504f598a19465a69ae32863ff1ed86 Mon Sep 17 00:00:00 2001 From: Lukas Bestle Date: Sun, 24 Nov 2019 21:50:41 +0100 Subject: [PATCH] Remote: New basicAuth option #2322 --- src/Http/Remote.php | 26 ++++++++++++++++---------- tests/Http/RemoteTest.php | 32 ++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/Http/Remote.php b/src/Http/Remote.php index c5bd495159..c5ab7614e6 100644 --- a/src/Http/Remote.php +++ b/src/Http/Remote.php @@ -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, ]; /** @@ -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']; diff --git a/tests/Http/RemoteTest.php b/tests/Http/RemoteTest.php index a72d114602..45b6e8b1f8 100644 --- a/tests/Http/RemoteTest.php +++ b/tests/Http/RemoteTest.php @@ -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()); } }