*/
GetResponse get(String appUrl, AccessToken token, String endPoint) throws IOException;
+ /**
+ * Content of the response is populated if response's HTTP code is {@link java.net.HttpURLConnection#HTTP_OK OK}.
+ * No log if there is an issue during the call.
+ */
+ GetResponse getSilent(String appUrl, AccessToken token, String endPoint) throws IOException;
+
/**
* Content of the response is populated if response's HTTP code is {@link java.net.HttpURLConnection#HTTP_OK OK} or
* {@link java.net.HttpURLConnection#HTTP_CREATED CREATED}.
@Override
public GetResponse get(String appUrl, AccessToken token, String endPoint) throws IOException {
+ return get(appUrl, token, endPoint, true);
+ }
+
+ @Override
+ public GetResponse getSilent(String appUrl, AccessToken token, String endPoint) throws IOException {
+ return get(appUrl, token, endPoint, false);
+ }
+
+ private GetResponse get(String appUrl, AccessToken token, String endPoint, boolean withLog) throws IOException {
validateEndPoint(endPoint);
try (okhttp3.Response response = client.newCall(newGetRequest(appUrl, token, endPoint)).execute()) {
int responseCode = response.code();
if (responseCode != HTTP_OK) {
- LOG.warn("GET response did not have expected HTTP code (was {}): {}", responseCode, attemptReadContent(response));
+ if (withLog) {
+ LOG.warn("GET response did not have expected HTTP code (was {}): {}", responseCode, attemptReadContent(response));
+ }
return new GetResponseImpl(responseCode, null, null);
}
return new GetResponseImpl(responseCode, readContent(response.body()).orElse(null), readNextEndPoint(response));
}
}
+
private static void validateEndPoint(String endPoint) {
checkArgument(endPoint.startsWith("/") || endPoint.startsWith("http"), "endpoint must start with '/' or 'http'");
}
import okhttp3.mockwebserver.RecordedRequest;
import okhttp3.mockwebserver.SocketPolicy;
import org.junit.Before;
+import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sonar.alm.client.github.GithubApplicationHttpClient.Response;
import org.sonar.alm.client.github.security.AccessToken;
import org.sonar.alm.client.github.security.UserAccessToken;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
import static java.lang.String.format;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
@Rule
public MockWebServer server = new MockWebServer();
+ @ClassRule
+ public static LogTester logTester = new LogTester().setLevel(LoggerLevel.WARN);
+
private GithubApplicationHttpClientImpl underTest;
private final AccessToken accessToken = new UserAccessToken(randomAlphabetic(10));
public void setUp() {
this.appUrl = format("http://%s:%s", server.getHostName(), server.getPort());
this.underTest = new GithubApplicationHttpClientImpl(new ConstantTimeoutConfiguration(500));
+ logTester.clear();
}
@Test
.hasMessage("invalidUrl/endpoint is not a valid url");
}
+ @Test
+ public void getSilent_no_log_if_code_is_not_200() throws IOException {
+ server.enqueue(new MockResponse().setResponseCode(403));
+
+ GetResponse response = underTest.getSilent(appUrl, accessToken, randomEndPoint);
+
+ assertThat(logTester.logs()).isEmpty();
+ assertThat(response.getContent()).isEmpty();
+
+ }
+
+ @Test
+ public void get_log_if_code_is_not_200() throws IOException {
+ server.enqueue(new MockResponse().setResponseCode(403));
+
+ GetResponse response = underTest.get(appUrl, accessToken, randomEndPoint);
+
+ assertThat(logTester.logs(LoggerLevel.WARN)).isNotEmpty();
+ assertThat(response.getContent()).isEmpty();
+
+ }
+
@Test
public void get_adds_authentication_header_with_Bearer_type_and_Accept_header() throws IOException, InterruptedException {
server.enqueue(new MockResponse());