public static <T extends Message> T convert(WsResponse response, Parser<T> parser) { try (InputStream byteStream = response.contentStream()) { byte[] bytes = IOUtils.toByteArray(byteStream); // HTTP header "Content-Type" is not verified. It may be different than protobuf. return parser.parseFrom(bytes); } catch (Exception e) { throw new IllegalStateException("Fail to parse protobuf response of " + response.requestUrl(), e); } }
/** * If an exception is not thrown, the response needs to be closed by either calling close() directly, or closing the * body content's stream/reader. * * @throws IllegalStateException if the request could not be executed due to a connectivity problem or timeout. Because networks can * fail during an exchange, it is possible that the remote server accepted the request before the failure * @throws MessageException if there was a problem with authentication or if a error message was parsed from the response. * @throws HttpException if the response code is not in range [200..300). Consider using {@link #createErrorMessage(HttpException)} to create more relevant messages for the users. */ public WsResponse call(WsRequest request) { Preconditions.checkState(!globalMode.isMediumTest(), "No WS call should be made in medium test mode"); Profiler profiler = Profiler.createIfDebug(LOG).start(); WsResponse response = target.wsConnector().call(request); profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl())); failIfUnauthorized(response); return response; }
@Test public void call_request() throws Exception { WsRequest wsRequest = new PostRequest("api/issues/search") .setMediaType(MediaTypes.JSON) .setParam("foo", "bar"); answer(new DumbLocalResponse(400, MediaTypes.JSON, "{}".getBytes(UTF_8), Collections.<String>emptyList())); WsResponse wsResponse = underTest.call(wsRequest); verifyRequested("POST", "api/issues/search", MediaTypes.JSON, ImmutableMap.of("foo", "bar")); assertThat(wsResponse.code()).isEqualTo(400); assertThat(wsResponse.content()).isEqualTo("{}"); assertThat(IOUtils.toString(wsResponse.contentReader())).isEqualTo("{}"); assertThat(IOUtils.toString(wsResponse.contentStream())).isEqualTo("{}"); assertThat(wsResponse.contentType()).isEqualTo(MediaTypes.JSON); assertThat(wsResponse.requestUrl()).isEqualTo("api/issues/search"); }
@Test public void support_base_url_with_context() { // just to be sure assertThat(serverUrl).endsWith("/"); underTest = HttpConnector.newBuilder().url(serverUrl + "sonar").build(); GetRequest request = new GetRequest("api/issues/search"); answerHelloWorld(); assertThat(underTest.call(request).requestUrl()).isEqualTo(serverUrl + "sonar/api/issues/search"); request = new GetRequest("/api/issues/search"); answerHelloWorld(); assertThat(underTest.call(request).requestUrl()).isEqualTo(serverUrl + "sonar/api/issues/search"); }
@Test public void follow_redirects_post() throws IOException, InterruptedException { MockWebServer server2 = new MockWebServer(); server2.start(); server2.url("").url().toString(); server.enqueue(new MockResponse() .setResponseCode(302) .setHeader("Location", server2.url("").url().toString())); server2.enqueue(new MockResponse() .setResponseCode(200)); underTest = HttpConnector.newBuilder().url(serverUrl).build(); PostRequest request = new PostRequest("api/ce/submit").setParam("projectKey", "project"); WsResponse response = underTest.call(request); RecordedRequest recordedRequest = server2.takeRequest(); assertThat(recordedRequest.getMethod()).isEqualTo("POST"); assertThat(recordedRequest.getBody().readUtf8()).isEqualTo("projectKey=project"); assertThat(response.requestUrl()).isEqualTo(server2.url("").url().toString()); assertThat(response.code()).isEqualTo(200); }
public static <T extends Message> T convert(WsResponse response, Parser<T> parser) { try (InputStream byteStream = response.contentStream()) { byte[] bytes = IOUtils.toByteArray(byteStream); // HTTP header "Content-Type" is not verified. It may be different than protobuf. return parser.parseFrom(bytes); } catch (Exception e) { throw new IllegalStateException("Fail to parse protobuf response of " + response.requestUrl(), e); } }
/** * If an exception is not thrown, the response needs to be closed by either calling close() directly, or closing the * body content's stream/reader. * * @throws IllegalStateException if the request could not be executed due to a connectivity problem or timeout. Because networks can * fail during an exchange, it is possible that the remote server accepted the request before the failure * @throws MessageException if there was a problem with authentication or if a error message was parsed from the response. * @throws HttpException if the response code is not in range [200..300). Consider using {@link #createErrorMessage(HttpException)} to create more relevant messages for the users. */ public WsResponse call(WsRequest request) { Preconditions.checkState(!globalMode.isMediumTest(), "No WS call should be made in medium test mode"); Profiler profiler = Profiler.createIfDebug(LOG).start(); WsResponse response = target.wsConnector().call(request); profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl())); failIfUnauthorized(response); return response; }
/** * @throws IllegalStateException if the request could not be executed due to * a connectivity problem or timeout. Because networks can * fail during an exchange, it is possible that the remote server * accepted the request before the failure * @throws HttpException if the response code is not in range [200..300) */ public WsResponse call(WsRequest request) { Profiler profiler = Profiler.createIfDebug(LOG).start(); WsResponse response = target.wsConnector().call(request); profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl())); failIfUnauthorized(response); return response; }