/** * Parse/expand request parameters and create a fully qualified * request uri. * @static * @param string $restPath * @param array $params * @return string $requestUrl */ public function createRequestUri($restPath, $params) { // code for leading slash $requestUrl = $this->servicePath . $restPath; if ($this->rootUrl) { if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) { $requestUrl = '/' . $requestUrl; } $requestUrl = $this->rootUrl . $requestUrl; } $uriTemplateVars = array(); $queryVars = array(); foreach ($params as $paramName => $paramSpec) { if ($paramSpec['type'] == 'boolean') { $paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false'; } if ($paramSpec['location'] == 'path') { $uriTemplateVars[$paramName] = $paramSpec['value']; } else { if ($paramSpec['location'] == 'query') { if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) { foreach ($paramSpec['value'] as $value) { $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value)); } } else { $queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value'])); } } } } if (count($uriTemplateVars)) { $uriTemplateParser = new Google_Utils_UriTemplate(); $requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars); } if (count($queryVars)) { $requestUrl .= '?' . implode($queryVars, '&'); } return $requestUrl; }
/** * This test test against the JSON files defined in * https://github.com/uri-templates/uritemplate-test * * We don't ship these tests with it, so they'll just silently * skip unless provided - this is mainly for use when * making specific URI template changes and wanting * to do a full regression check. */ public function testAgainstStandardTests() { $location = "../../uritemplate-test/*.json"; $urit = new Google_Utils_UriTemplate(); foreach (glob($location) as $file) { $test = json_decode(file_get_contents($file), true); foreach ($test as $title => $testsets) { foreach ($testsets['testcases'] as $cases) { $input = $cases[0]; $output = $cases[1]; if ($output == false) { continue; // skipping negative tests for now } else { if (is_array($output)) { $response = $urit->parse($input, $testsets['variables']); $this->assertContains($response, $output, $input . " failed from " . $title); } else { $this->assertEquals($output, $urit->parse($input, $testsets['variables']), $input . " failed."); } } } } } }