HttpRequests { public static String toUrl(Request request) throws Exception { MethodConfig mc = request.getMethodConfig(); Charset charset = mc.getCharset(); PathBuilder pathBuilder = mc.getPathTemplate().getBuilder(charset); Iterator<EncodedPair> pathParamsIterator = request.getEncodedParamsIterator(PATH); while(pathParamsIterator.hasNext()){ EncodedPair encoded = pathParamsIterator.next(); pathBuilder.merge(encoded.getName(), encoded.getValue(), true); } String query = join(request.getEncodedParamsIterator(QUERY), '&'); if(isNotBlank(query)) { query = "?" + query; } String matrix = join(request.getEncodedParamsIterator(MATRIX), ';'); if(isNotBlank(matrix)) { matrix = ";" + matrix; } return pathBuilder.build() + matrix + query; } private  HttpRequests(); static String toUrl(Request request);  }
@Test public void toUrlShouldTransformGivenRequestToUrl() throws Exception { Request request = mock(Request.class); MethodConfig methodConfig = mock(MethodConfig.class); PathTemplate pathTemplate = mock(PathTemplate.class); PathBuilder pathBuilder = mock(PathBuilder.class); Iterator<EncodedPair> pathPairs = asList( Pairs.toPreEncodedPair("p1","v1"), Pairs.toPreEncodedPair("p2","v2") ).iterator(); Iterator<EncodedPair> queryPairs = mock(Iterator.class); Iterator<EncodedPair> matrixPairs = mock(Iterator.class); when(request.getEncodedParamsIterator(PATH)).thenReturn(pathPairs); when(request.getEncodedParamsIterator(QUERY)).thenReturn(queryPairs); when(request.getEncodedParamsIterator(MATRIX)).thenReturn(matrixPairs); when(pathTemplate.getBuilder(UTF8)).thenReturn(pathBuilder); when(methodConfig.getCharset()).thenReturn(UTF8); when(methodConfig.getPathTemplate()).thenReturn(pathTemplate); when(request.getMethodConfig()).thenReturn(methodConfig); when(pathBuilder.build()).thenReturn("path"); mockStatic(Pairs.class); when(Pairs.join(queryPairs, '&')).thenReturn("query"); when(Pairs.join(matrixPairs, ';')).thenReturn("matrix"); assertEquals("path;matrix?query", HttpRequests.toUrl(request)); verify(pathBuilder).merge("p1", "v1", true); verify(pathBuilder).merge("p2", "v2", true); }
@Test public void toUrlShouldTransformGivenRequestToUrlWhenQueryAndMatrixAreEmpty() throws Exception { Request request = mock(Request.class); MethodConfig methodConfig = mock(MethodConfig.class); PathTemplate pathTemplate = mock(PathTemplate.class); PathBuilder pathBuilder = mock(PathBuilder.class); Iterator<EncodedPair> pathPairs = asList( Pairs.toPreEncodedPair("p1","v1"), Pairs.toPreEncodedPair("p2","v2") ).iterator(); Iterator<EncodedPair> queryPairs = mock(Iterator.class); Iterator<EncodedPair> matrixPairs = mock(Iterator.class); when(request.getEncodedParamsIterator(PATH)).thenReturn(pathPairs); when(request.getEncodedParamsIterator(QUERY)).thenReturn(queryPairs); when(request.getEncodedParamsIterator(MATRIX)).thenReturn(matrixPairs); when(pathTemplate.getBuilder(UTF8)).thenReturn(pathBuilder); when(methodConfig.getCharset()).thenReturn(UTF8); when(methodConfig.getPathTemplate()).thenReturn(pathTemplate); when(request.getMethodConfig()).thenReturn(methodConfig); when(pathBuilder.build()).thenReturn("path"); mockStatic(Pairs.class); when(Pairs.join(queryPairs, '&')).thenReturn(""); when(Pairs.join(matrixPairs, ';')).thenReturn(""); assertEquals("path", HttpRequests.toUrl(request)); verify(pathBuilder).merge("p1", "v1", true); verify(pathBuilder).merge("p2", "v2", true); }