Browse Source

Added `protocolRelative` option to `buildUrl()` method.

Robert Pustułka 8 years ago
parent
commit
fef1ebfdd1
2 changed files with 18 additions and 4 deletions
  1. 4 2
      src/Http/Client.php
  2. 14 2
      tests/TestCase/Http/ClientTest.php

+ 4 - 2
src/Http/Client.php

@@ -402,7 +402,8 @@ class Client
                 $locationUrl = $this->buildUrl($location, [], [
                 $locationUrl = $this->buildUrl($location, [], [
                     'host' => $url->getHost(),
                     'host' => $url->getHost(),
                     'port' => $url->getPort(),
                     'port' => $url->getPort(),
-                    'scheme' => $url->getScheme()
+                    'scheme' => $url->getScheme(),
+                    'protocolRelative' => true
                 ]);
                 ]);
 
 
                 $request = $request->withUri(new Uri($locationUrl));
                 $request = $request->withUri(new Uri($locationUrl));
@@ -452,10 +453,11 @@ class Client
             'host' => null,
             'host' => null,
             'port' => null,
             'port' => null,
             'scheme' => 'http',
             'scheme' => 'http',
+            'protocolRelative' => false
         ];
         ];
         $options += $defaults;
         $options += $defaults;
 
 
-        if (preg_match('#^//#', $url)) {
+        if ($options['protocolRelative'] && preg_match('#^//#', $url)) {
             $url = $options['scheme'] . ':' . $url;
             $url = $options['scheme'] . ':' . $url;
         }
         }
         if (preg_match('#^https?://#', $url)) {
         if (preg_match('#^https?://#', $url)) {

+ 14 - 2
tests/TestCase/Http/ClientTest.php

@@ -139,12 +139,24 @@ class ClientTest extends TestCase
             ],
             ],
             [
             [
                 'http://example.com/test.html',
                 'http://example.com/test.html',
+                '//test.html',
+                [],
+                [
+                    'scheme' => 'http',
+                    'host' => 'example.com',
+                    'protocolRelative' => false
+                ],
+                'url with a double slash',
+            ],
+            [
+                'http://example.com/test.html',
                 '//example.com/test.html',
                 '//example.com/test.html',
                 [],
                 [],
                 [
                 [
-                    'scheme' => 'http'
+                    'scheme' => 'http',
+                    'protocolRelative' => true
                 ],
                 ],
-                'url without a scheme',
+                'protocol relative url',
             ],
             ],
         ];
         ];
     }
     }