HttpUtils { public static Map<String, String> parseUriPattern(String pattern, String url) { pattern = normaliseUriPath(pattern); url = normaliseUriPath(url); if (!pattern.contains("{") || !pattern.contains("}")) { return url.equals(pattern) ? Collections.emptyMap() : null; } UriTemplateParser templateParser = getUriParserForPattern(pattern); Matcher urlMatcher = templateParser.getPattern().matcher(url); if (urlMatcher.matches()) { if (templateParser.getGroupIndexes().length == 0) { return Collections.emptyMap(); } Map<String, String> pathParams = new LinkedHashMap<>(); int index = 0; for (String name : templateParser.getNames()) { String value = urlMatcher.group(templateParser.getGroupIndexes()[index]); if (name == null || pathParams.containsKey(name)) { pathParams.put(name + "@" + (index + 1), value); } else { pathParams.put(name, value); } index++; } return pathParams; } return null; } private  HttpUtils(); static KeyStore getKeyStore(ScenarioContext context, String trustStoreFile, String password, String type); static boolean isPrintable(String mediaType); static Charset parseContentTypeCharset(String mimeType); static Map<String, String> parseContentTypeParams(String mimeType); static String getContentType(ScriptValue sv); static Map<String, Cookie> parseCookieHeaderString(String header); static String createCookieHeaderValue(Collection<Cookie> cookies); static Map<String, String> parseUriPattern(String pattern, String url); static final String normaliseUriPath(String uri); static List<Integer> calculatePathMatchScore(String path); static String generateMimeBoundaryMarker(); static String multiPartToString(List<MultiPartItem> items, String boundary); static final String HEADER_CONTENT_TYPE; static final String HEADER_CONTENT_LENGTH; static final String HEADER_ACCEPT; static final String HEADER_ALLOW; static final String HEADER_AC_ALLOW_ORIGIN; static final String HEADER_AC_ALLOW_METHODS; static final String HEADER_AC_REQUEST_HEADERS; static final String HEADER_AC_ALLOW_HEADERS; static final String CHARSET; static final Set<String> HTTP_METHODS; static final List<Integer> DEFAULT_PATH_SCORE; }
@Test public void testParseUriPathPatterns() { Map<String, String> map = HttpUtils.parseUriPattern("/cats/{id}", "/cats/1"); Match.equals(map, "{ id: '1' }"); map = HttpUtils.parseUriPattern("/cats/{id}/", "/cats/1"); Match.equals(map, "{ id: '1' }"); map = HttpUtils.parseUriPattern("/cats/{id}", "/cats/1/"); Match.equals(map, "{ id: '1' }"); map = HttpUtils.parseUriPattern("/cats/{id}", "/foo/bar"); Match.equals(map, null); map = HttpUtils.parseUriPattern("/cats", "/cats/1"); Match.equals(map, null); map = HttpUtils.parseUriPattern("/{path}/{id}", "/cats/1"); Match.equals(map, "{ path: 'cats', id: '1' }"); map = HttpUtils.parseUriPattern("/cats/{id}/foo", "/cats/1/foo"); Match.equals(map, "{ id: '1' }"); map = HttpUtils.parseUriPattern("/api/{img}", "/api/billie.jpg"); Match.equals(map, "{ img: 'billie.jpg' }"); map = HttpUtils.parseUriPattern("/{greedyPath:.+}", "/cats/1"); Match.equals(map, "{ greedyPath: 'cats/1' }"); map = HttpUtils.parseUriPattern("/cats/v{partialPath}x", "/cats/v1x"); Match.equals(map, "{ partialPath: '1' }"); map = HttpUtils.parseUriPattern("/cats/{duplicate}/{duplicate}", "/cats/v1/1043"); Match.equals(map, "{ duplicate: 'v1', 'duplicate@2': '1043' }"); map = HttpUtils.parseUriPattern("/cats/{}/{}", "/cats/v1/1043"); Match.equals(map, "{ ignored: 'v1', 'ignored@2': '1043' }"); }