protected WsResponse call(WsRequest request) { return wsConnector.call(request).failIfNotSuccessful(); }
public void test() { GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC"); when(wsConnector.call(get)).thenReturn(newProtobufFakeResponse()); Testing.Fake message = call(get, Testing.Fake.parser()); assertThat(message.getLabel()).isEqualTo("ok"); assertThat(get.getPath()).isEqualTo("api/issues/issue"); // media type automatically set to protobuf assertThat(get.getMediaType()).isEqualTo(MediaTypes.PROTOBUF); }
/** * 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; }
public void test() throws IOException { GetRequest get = new GetRequest(path("issue")).setMediaType(MediaTypes.JSON); when(wsConnector.call(get)).thenReturn(new MockWsResponse().setContent("ok")); WsResponse response = call(get); assertThat(response.content()).isEqualTo("ok"); }
public void test() { GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC"); when(wsConnector.call(get)).thenReturn(MockWsResponse.createJson("{}").setRequestUrl("http://local/api/issues/issue?key=ABC")); try { call(get, Testing.Fake.parser()); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Fail to parse protobuf response of http://local/api/issues/issue?key=ABC"); } } }.test();
@Test public void log_and_profile_request_if_debug_level() { WsRequest request = newRequest(); WsResponse response = newResponse().setRequestUrl("https://local/api/issues/search"); when(wsClient.wsConnector().call(request)).thenReturn(response); logTester.setLevel(LoggerLevel.DEBUG); ScannerWsClient underTest = new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))); WsResponse result = underTest.call(request); // do not fail the execution -> interceptor returns the response assertThat(result).isSameAs(response); // check logs List<String> debugLogs = logTester.logs(LoggerLevel.DEBUG); assertThat(debugLogs).hasSize(1); assertThat(debugLogs.get(0)).contains("GET 200 https://local/api/issues/search | time="); }
public void test() { GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC"); when(wsConnector.call(get)).thenReturn(new MockWsResponse().setCode(403).setRequestUrl("https://local/foo").setContent("error")); try { call(get, Testing.Fake.parser()); fail(); } catch (HttpException e) { assertThat(e.code()).isEqualTo(403); } }
@Test public void fail_if_requires_credentials() { expectedException.expect(MessageException.class); expectedException .expectMessage("Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties sonar.login and sonar.password."); WsRequest request = newRequest(); WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, false, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_credentials_are_not_valid() { expectedException.expect(MessageException.class); expectedException.expectMessage("Not authorized. Please check the properties sonar.login and sonar.password."); WsRequest request = newRequest(); WsResponse response = newResponse().setCode(401); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, /* credentials are configured */true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_requires_permission() { expectedException.expect(MessageException.class); expectedException.expectMessage("You're not authorized to run analysis. Please contact the project administrator."); WsRequest request = newRequest(); WsResponse response = newResponse() .setCode(403); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
@Test public void fail_if_bad_request() { expectedException.expect(MessageException.class); expectedException.expectMessage("Boo! bad request! bad!"); WsRequest request = newRequest(); WsResponse response = newResponse() .setCode(400) .setContent("{\"errors\":[{\"msg\":\"Boo! bad request! bad!\"}]}"); when(wsClient.wsConnector().call(request)).thenReturn(response); new ScannerWsClient(wsClient, true, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()))).call(request); }
protected WsResponse call(WsRequest request) { return wsConnector.call(request).failIfNotSuccessful(); }
/** * @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; }
/** * 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; }
private Rules.ShowResponse showRule(String ruleKey) { GetRequest getRequest = new GetRequest("api/rules/show").setParam("key", ruleKey).setMediaType(MediaTypes.PROTOBUF); WsResponse wsResponse = wsClient.wsConnector().call(getRequest); if (wsResponse.code() != 200) { throw new HttpException(wsClient.wsConnector().baseUrl() + toString(getRequest), wsResponse.code(), wsResponse.content()); } try { return Rules.ShowResponse.parseFrom(wsResponse.contentStream()); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } }
WsResponse wsResponse = wsClient.wsConnector().call(ceTaskRequest);