DefaultWebClient extends UserClient<HttpRequest, HttpResponse> implements WebClient { @Override public HttpResponse execute(HttpRequest req) { if (Clients.isUndefinedUri(uri())) { final URI uri; if (isAbsoluteUri(req.path())) { try { uri = URI.create(req.path()); } catch (Exception ex) { return abortRequestAndReturnFailureResponse(req, new IllegalArgumentException( "Failed to create a URI: " + req.path(), ex)); } } else if (req.scheme() != null && req.authority() != null) { uri = req.uri(); } else { return abortRequestAndReturnFailureResponse(req, new IllegalArgumentException( "Scheme and authority must be specified in \":path\" or " + "in \":scheme\" and \":authority\". :path=" + req.path() + ", :scheme=" + req.scheme() + ", :authority=" + req.authority())); } final SessionProtocol protocol; try { protocol = Scheme.parse(uri.getScheme()).sessionProtocol(); } catch (Exception e) { return abortRequestAndReturnFailureResponse(req, new IllegalArgumentException( "Failed to parse a scheme: " + uri.getScheme(), e)); } final Endpoint endpoint = Endpoint.parse(uri.getAuthority()); final String query = uri.getRawQuery(); final String path = uri.getRawPath(); final HttpRequest newReq = req.withHeaders(req.headers().toBuilder() .path(query == null ? path : path + '?' + query)); return execute(endpoint, newReq, protocol); } if (isAbsoluteUri(req.path())) { return abortRequestAndReturnFailureResponse(req, new IllegalArgumentException( "Cannot send a request with a \":path\" header that contains a URI with the authority, " + "because the client was created with a base URI. path: " + req.path())); } final String originalPath = req.path(); final String newPath = concatPaths(uri().getRawPath(), originalPath); final HttpRequest newReq; if (newPath != originalPath) { newReq = req.withHeaders(req.headers().toBuilder().path(newPath)); } else { newReq = req; } return execute(endpointGroup(), newReq, scheme().sessionProtocol()); } DefaultWebClient(ClientBuilderParams params, HttpClient delegate, MeterRegistry meterRegistry); @Override HttpResponse execute(HttpRequest req); @Override HttpResponse execute(AggregatedHttpRequest aggregatedReq); @Override HttpClient unwrap();  }
@Test void testConcatenateRequestPath() throws Exception { final String clientUriPath = "http: final String requestPath = "world/test?q1=foo"; final HttpClient mockClientDelegate = mock(HttpClient.class); final DefaultWebClient defaultWebClient = createDefaultWebClient(clientUriPath, mockClientDelegate ); defaultWebClient.execute(HttpRequest.of(RequestHeaders.of(HttpMethod.GET, requestPath))); final ArgumentCaptor<HttpRequest> argCaptor = ArgumentCaptor.forClass(HttpRequest.class); verify(mockClientDelegate).execute(any(ClientRequestContext.class), argCaptor.capture()); final String concatPath = argCaptor.getValue().path(); assertThat(concatPath).isEqualTo("/hello/world/test?q1=foo"); }
@Test void testRequestParamsUndefinedEndPoint() throws Exception { final String path = "http: final HttpClient mockClientDelegate = mock(HttpClient.class); final DefaultWebClient defaultWebClient = createDefaultWebClient(AbstractWebClientBuilder.UNDEFINED_URI, mockClientDelegate); defaultWebClient.execute(HttpRequest.of(RequestHeaders.of(HttpMethod.GET, path))); final ArgumentCaptor<HttpRequest> argCaptor = ArgumentCaptor.forClass(HttpRequest.class); verify(mockClientDelegate).execute(any(ClientRequestContext.class), argCaptor.capture()); final String concatPath = argCaptor.getValue().path(); assertThat(concatPath).isEqualTo("/helloWorld/test?q1=foo"); }
@Test void testWithoutRequestParamsUndefinedEndPoint() throws Exception { final String path = "http: final HttpClient mockClientDelegate = mock(HttpClient.class); final DefaultWebClient defaultWebClient = createDefaultWebClient(AbstractWebClientBuilder.UNDEFINED_URI, mockClientDelegate); defaultWebClient.execute(HttpRequest.of(RequestHeaders.of(HttpMethod.GET, path))); final ArgumentCaptor<HttpRequest> argCaptor = ArgumentCaptor.forClass(HttpRequest.class); verify(mockClientDelegate).execute(any(ClientRequestContext.class), argCaptor.capture()); final String concatPath = argCaptor.getValue().path(); assertThat(concatPath).isEqualTo("/helloWorld/test"); }