Skip to content
This repository has been archived by the owner on Mar 28, 2021. It is now read-only.

Commit

Permalink
improve Factory::create
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 27, 2017
1 parent 19e4627 commit 79e64cc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ protected function removeDotSegments(string $path): string
$new_path .= '/';
}

if (strpos($path, '/') === 0 && strpos($new_path, '/') !== 0) {
return '/'.$new_path;
}

return $new_path;
}

Expand Down
62 changes: 62 additions & 0 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,66 @@ public function uriBaseUriProvider()
],
];
}

/**
* @covers \League\Uri\create
* @covers \League\Uri\Factory
*
* @dataProvider resolveProvider
*
* @param string $base_uri
* @param string $uri
* @param string $expected
*/
public function testCreateResolve(string $base_uri, string $uri, string $expected)
{
$this->assertSame($expected, (string) Uri\create($uri, $base_uri));
}

public function resolveProvider()
{
$base_uri = 'http://a/b/c/d;p?q';

return [
'base uri' => [$base_uri, '', $base_uri],
'scheme' => [$base_uri, 'http://d/e/f', 'http://d/e/f'],
'path 1' => [$base_uri, 'g', 'http://a/b/c/g'],
'path 2' => [$base_uri, './g', 'http://a/b/c/g'],
'path 3' => [$base_uri, 'g/', 'http://a/b/c/g/'],
'path 4' => [$base_uri, '/g', 'http://a/g'],
'authority' => [$base_uri, '//g', 'http://g'],
'query' => [$base_uri, '?y', 'http://a/b/c/d;p?y'],
'path + query' => [$base_uri, 'g?y', 'http://a/b/c/g?y'],
'fragment' => [$base_uri, '#s', 'http://a/b/c/d;p?q#s'],
'path + fragment' => [$base_uri, 'g#s', 'http://a/b/c/g#s'],
'path + query + fragment' => [$base_uri, 'g?y#s', 'http://a/b/c/g?y#s'],
'single dot 1' => [$base_uri, '.', 'http://a/b/c/'],
'single dot 2' => [$base_uri, './', 'http://a/b/c/'],
'single dot 3' => [$base_uri, './g/.', 'http://a/b/c/g/'],
'single dot 4' => [$base_uri, 'g/./h', 'http://a/b/c/g/h'],
'double dot 1' => [$base_uri, '..', 'http://a/b/'],
'double dot 2' => [$base_uri, '../', 'http://a/b/'],
'double dot 3' => [$base_uri, '../g', 'http://a/b/g'],
'double dot 4' => [$base_uri, '../..', 'http://a/'],
'double dot 5' => [$base_uri, '../../', 'http://a/'],
'double dot 6' => [$base_uri, '../../g', 'http://a/g'],
'double dot 7' => [$base_uri, '../../../g', 'http://a/g'],
'double dot 8' => [$base_uri, '../../../../g', 'http://a/g'],
'double dot 9' => [$base_uri, 'g/../h' , 'http://a/b/c/h'],
'mulitple slashes' => [$base_uri, 'foo////g', 'http://a/b/c/foo////g'],
'complex path 1' => [$base_uri, ';x', 'http://a/b/c/;x'],
'complex path 2' => [$base_uri, 'g;x', 'http://a/b/c/g;x'],
'complex path 3' => [$base_uri, 'g;x?y#s', 'http://a/b/c/g;x?y#s'],
'complex path 4' => [$base_uri, 'g;x=1/./y', 'http://a/b/c/g;x=1/y'],
'complex path 5' => [$base_uri, 'g;x=1/../y', 'http://a/b/c/y'],
'dot segments presence 1' => [$base_uri, '/./g', 'http://a/g'],
'dot segments presence 2' => [$base_uri, '/../g', 'http://a/g'],
'dot segments presence 3' => [$base_uri, 'g.', 'http://a/b/c/g.'],
'dot segments presence 4' => [$base_uri, '.g', 'http://a/b/c/.g'],
'dot segments presence 5' => [$base_uri, 'g..', 'http://a/b/c/g..'],
'dot segments presence 6' => [$base_uri, '..g', 'http://a/b/c/..g'],
'origin uri without path' => ['http://h:b@a', 'b/../y', 'http://h:b@a/y'],
'uri without auhtority' => ['mailto:[email protected]', '[email protected]?subject=baz', 'mailto:[email protected]?subject=baz'],
];
}
}

0 comments on commit 79e64cc

Please sign in to comment.