From 23842549152621b81547b960b345ae0c5e1a547e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Haas?= Date: Thu, 13 Nov 2014 18:28:23 +0100 Subject: [PATCH] Allow more sophisticated base_url If the base_url contains a query and/or a fragment, then the locatePath() delivers incorrect results. Example: ``` base_url: http://www.example.com/subsite/?language=de&abc=1#anchor path to visit: /user Result wrong: http://www.example.com/subsite/?language=de&abc=1#anchor/user Expected result: http://www.example.com/subsite/user?language=de&abc=1#anchor ``` This code is going to deal with all possible cases. --- .../MinkExtension/Context/RawMinkContext.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Behat/MinkExtension/Context/RawMinkContext.php b/src/Behat/MinkExtension/Context/RawMinkContext.php index e7c1726c..4c997f6c 100644 --- a/src/Behat/MinkExtension/Context/RawMinkContext.php +++ b/src/Behat/MinkExtension/Context/RawMinkContext.php @@ -134,9 +134,22 @@ public function visitPath($path, $sessionName = null) */ public function locatePath($path) { - $startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/'; - - return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path; + if (0 === strpos($path, 'http')) { + return $path; + } + + $parts = parse_url($this->getMinkParameter('base_url')); + $path = ltrim($path, '/'); + $parts['path'] = empty($parts['path']) ? '/' : rtrim($parts['path'], '/') . '/'; + + $url = empty($parts['scheme']) ? 'http' : $parts['scheme']; + $url .= '://'; + $url .= empty($parts['user']) ? '' : ($parts['user'] . (empty($parts['pass']) ? '' : ':' . $parts['pass']) . '@'); + $url .= $parts['host'] . $parts['path'] . $path; + $url .= empty($parts['query']) ? '' : '?' . $parts['query']; + $url .= empty($parts['fragment']) ? '' : '#' . $parts['fragment']; + + return $url; } /**