public function testMultipleAnnotations() { $var = "value"; $hello = "Hello World!"; $urit = new Google_Utils_URITemplate(); $this->assertEquals("http://www.google.com/Hello%20World!?var=value", $urit->parse("http://www.google.com/{+hello}{?var}", array("var" => $var, "hello" => $hello))); $params = array("playerId" => "me", "leaderboardId" => "CgkIhcG1jYEbEAIQAw", "timeSpan" => "ALL_TIME", "other" => "irrelevant"); $this->assertEquals("players/me/leaderboards/CgkIhcG1jYEbEAIQAw/scores/ALL_TIME", $urit->parse("players/{playerId}/leaderboards/{leaderboardId}/scores/{timeSpan}", $params)); }
/** * Parse/expand request parameters and create a fully qualified * request uri. * * @static * * @param string $servicePath * @param string $restPath * @param array $params * @return string $requestUrl */ public static function createRequestUri($servicePath, $restPath, $params) { $requestUrl = $servicePath . $restPath; $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($value); } } else { $queryVars[] = $paramName . '=' . rawurlencode($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."); } } } } } }