aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws-client
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-01-31 12:05:58 +0100
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-01-31 12:06:05 +0100
commit47de0ecd410920fc000151f5c528b97b28505ec9 (patch)
treefcb7f4b6dd93cc36fd6fd044281dbc5ffa4ec23a /sonar-ws-client
parentaf595f0fda1855b09033a0ed98c955db0c5432c6 (diff)
downloadsonarqube-47de0ecd410920fc000151f5c528b97b28505ec9.tar.gz
sonarqube-47de0ecd410920fc000151f5c528b97b28505ec9.zip
WS client should consider codes other than 200 for success
Diffstat (limited to 'sonar-ws-client')
-rw-r--r--sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java14
-rw-r--r--sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java22
2 files changed, 35 insertions, 1 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java
index 184559fcde4..e20513ee7f2 100644
--- a/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java
+++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java
@@ -23,13 +23,21 @@ import com.github.kevinsawicki.http.HttpRequest;
import org.sonar.wsclient.base.HttpException;
import javax.annotation.Nullable;
+
+import java.util.Arrays;
import java.util.Map;
+import static java.net.HttpURLConnection.HTTP_CREATED;
+import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
+import static java.net.HttpURLConnection.HTTP_OK;
+
/**
* Not an API. Please do not use this class, except maybe for unit tests.
*/
public class HttpRequestFactory {
+ private final static int[] RESPONSE_SUCCESS = {HTTP_OK, HTTP_CREATED, HTTP_NO_CONTENT};
+
private final String baseUrl;
private String login, password, proxyHost, proxyLogin, proxyPassword;
private int proxyPort;
@@ -128,7 +136,7 @@ public class HttpRequestFactory {
private String execute(HttpRequest request) {
try {
- if (request.ok()) {
+ if (isSuccess(request)) {
return request.body(HttpRequest.CHARSET_UTF8);
}
// TODO handle error messages
@@ -139,6 +147,10 @@ public class HttpRequestFactory {
}
}
+ private boolean isSuccess(HttpRequest request) {
+ return Arrays.binarySearch(RESPONSE_SUCCESS, request.code()) >= 0;
+ }
+
private HttpRequest prepare(HttpRequest request) {
if (proxyHost != null) {
request.useProxy(proxyHost, proxyPort);
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java
index ff870ec5938..c0f8c726cc3 100644
--- a/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java
+++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java
@@ -76,6 +76,28 @@ public class HttpRequestFactoryTest {
}
@Test
+ public void post_successful_if_created() {
+ httpServer.stubStatusCode(201).stubResponseBody("{}");
+
+ HttpRequestFactory factory = new HttpRequestFactory(httpServer.url());
+ String json = factory.post("/api/issues/change", Collections.<String, Object>emptyMap());
+
+ assertThat(json).isEqualTo("{}");
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/change");
+ }
+
+ @Test
+ public void post_successful_if_no_content() {
+ httpServer.stubStatusCode(204).stubResponseBody("");
+
+ HttpRequestFactory factory = new HttpRequestFactory(httpServer.url());
+ String json = factory.post("/api/issues/change", Collections.<String, Object>emptyMap());
+
+ assertThat(json).isEqualTo("");
+ assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/change");
+ }
+
+ @Test
public void test_authentication() {
httpServer.stubStatusCode(200).stubResponseBody("{}");