HTTPClientImpl implements HTTPClient { Request httpRequest(HTTPRequest request) { var builder = new Request.Builder(); try { logger.debug("[request] method={}, uri={}", request.method, request.uri); builder.url(request.requestURI()); } catch (IllegalArgumentException e) { throw new HTTPClientException("uri is invalid, uri=" + request.uri, "INVALID_URL", e); } if (!request.params.isEmpty()) logger.debug("[request] params={}", new FieldMapLogParam(request.params)); request.headers.put(HTTPHeaders.USER_AGENT, userAgent); for (Map.Entry<String, String> entry : request.headers.entrySet()) { builder.addHeader(entry.getKey(), entry.getValue()); } logger.debug("[request] headers={}", new FieldMapLogParam(request.headers)); if (request.body != null) { if (request.form != null) { logger.debug("[request] form={}", new FieldMapLogParam(request.form)); } else { logger.debug("[request] body={}", BodyLogParam.of(request.body, request.contentType)); } MediaType contentType = mediaType(request.contentType); builder.method(request.method.name(), RequestBody.create(request.body, contentType)); } else { RequestBody body = request.method == HTTPMethod.GET || request.method == HTTPMethod.HEAD ? null : RequestBody.create(new byte[0], null); builder.method(request.method.name(), body); } if (request.connectTimeout != null || request.timeout != null) { logger.debug("[request] connectTimeout={}, timeout={}", request.connectTimeout, request.timeout); builder.tag(HTTPRequest.class, request); } return builder.build(); } HTTPClientImpl(OkHttpClient client, String userAgent, Duration slowOperationThreshold); @Override HTTPResponse execute(HTTPRequest request);  }
@Test void httpRequest() { var request = new HTTPRequest(HTTPMethod.POST, "http: request.params.put("query", "value"); request.accept(ContentType.APPLICATION_JSON); request.body("text", ContentType.TEXT_PLAIN); request.connectTimeout = Duration.ofSeconds(30); Request httpRequest = httpClient.httpRequest(request); assertThat(httpRequest.url().toString()).isEqualTo("http: assertThat(httpRequest.headers().get(HTTPHeaders.ACCEPT)).isEqualTo(ContentType.APPLICATION_JSON.toString()); assertThat(httpRequest.headers().get(HTTPHeaders.USER_AGENT)).isEqualTo("TestUserAgent"); assertThat(httpRequest.tag(HTTPRequest.class)).isSameAs(request); }
@Test void httpRequestWithInvalidURL() { assertThatThrownBy(() -> httpClient.httpRequest(new HTTPRequest(HTTPMethod.HEAD, " .isInstanceOf(HTTPClientException.class) .hasMessageContaining("uri is invalid"); }