From 626c85c07d3abce66cda913aeb18efc07626d5c8 Mon Sep 17 00:00:00 2001 From: shibafu Date: Sat, 19 Jan 2019 02:17:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?URL=E6=AD=A3=E8=A6=8F=E5=8C=96=E3=81=AE?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Unit/Utilities/FormatterTest.php | 57 ++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/Unit/Utilities/FormatterTest.php diff --git a/tests/Unit/Utilities/FormatterTest.php b/tests/Unit/Utilities/FormatterTest.php new file mode 100644 index 0000000..dd9f0a2 --- /dev/null +++ b/tests/Unit/Utilities/FormatterTest.php @@ -0,0 +1,57 @@ +assertEquals($url, $formatter->normalizeUrl($url)); + } + + public function testNormalizeUrlWithSortedQuery() + { + $formatter = new Formatter(); + + $url = 'http://example.com/path/to?foo=bar&hoge=fuga'; + $this->assertEquals($url, $formatter->normalizeUrl($url)); + } + + public function testNormalizeUrlWithUnsortedQuery() + { + $formatter = new Formatter(); + + $url = 'http://example.com/path/to?hoge=fuga&foo=bar'; + $this->assertEquals('http://example.com/path/to?foo=bar&hoge=fuga', $formatter->normalizeUrl($url)); + } + + public function testNormalizeUrlWithSortedQueryAndFragment() + { + $formatter = new Formatter(); + + $url = 'http://example.com/path/to?foo=bar&hoge=fuga#fragment'; + $this->assertEquals($url, $formatter->normalizeUrl($url)); + } + + public function testNormalizeUrlWithFragment() + { + $formatter = new Formatter(); + + $url = 'http://example.com/path/to#fragment'; + $this->assertEquals($url, $formatter->normalizeUrl($url)); + } + + public function testNormalizeUrlWithSortedQueryAndZeroLengthFragment() + { + $formatter = new Formatter(); + + $url = 'http://example.com/path/to?foo=bar&hoge=fuga#'; + $this->assertEquals('http://example.com/path/to?foo=bar&hoge=fuga', $formatter->normalizeUrl($url)); + } +} \ No newline at end of file From 6d66425fc923760d0cdadc2813710a798d3d15d6 Mon Sep 17 00:00:00 2001 From: shibafu Date: Sat, 19 Jan 2019 02:10:18 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E6=AD=A3=E8=A6=8F=E5=8C=96=E5=AF=BE?= =?UTF-8?q?=E8=B1=A1=E3=81=AEURL=E3=81=AB=E3=82=AF=E3=82=A8=E3=83=AA?= =?UTF-8?q?=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC=E3=82=BF=E3=81=A8URL?= =?UTF-8?q?=E3=83=95=E3=83=A9=E3=82=B0=E3=83=A1=E3=83=B3=E3=83=88=E3=81=8C?= =?UTF-8?q?=E4=B8=A1=E6=96=B9=E3=81=A8=E3=82=82=E5=90=AB=E3=81=BE=E3=82=8C?= =?UTF-8?q?=E3=82=8B=E5=A0=B4=E5=90=88=E3=80=81=E6=AD=A3=E8=A6=8F=E5=8C=96?= =?UTF-8?q?=E6=99=82=E3=81=AB=E9=A0=86=E5=BA=8F=E3=81=8C=E5=B4=A9=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E4=B8=8D=E6=AD=A3=E3=81=AAURL=E3=81=AB=E3=81=AA?= =?UTF-8?q?=E3=81=A3=E3=81=A6=E3=81=97=E3=81=BE=E3=81=86=E3=83=90=E3=82=B0?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs #56 --- app/Utilities/Formatter.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/Utilities/Formatter.php b/app/Utilities/Formatter.php index b7ea0e7..8214894 100644 --- a/app/Utilities/Formatter.php +++ b/app/Utilities/Formatter.php @@ -52,12 +52,25 @@ class Formatter $url = preg_replace('~/#!/~u', '/', $url); // Sort query parameters - $query = parse_url($url, PHP_URL_QUERY); - if (!empty($query)) { - $url = str_replace_last('?' . $query, '', $url); - parse_str($query, $params); + $parts = parse_url($url); + if (!empty($parts['query'])) { + // Remove query parameters + $url = str_replace_last('?' . $parts['query'], '', $url); + if (!empty($parts['fragment'])) { + // Remove fragment identifier + $url = str_replace_last('#' . $parts['fragment'], '', $url); + } else { + // "http://example.com/?query#" の場合 $parts['fragment'] は unset になるので、個別に判定して除去する必要がある + $url = preg_replace('/#\z/u', '', $url); + } + + parse_str($parts['query'], $params); ksort($params); + $url = $url . '?' . http_build_query($params); + if (!empty($parts['fragment'])) { + $url .= '#' . $parts['fragment']; + } } return $url;