]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4506 Java WS Client - do not use URL query parameters for POST requests
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 16 Jul 2013 14:08:55 +0000 (16:08 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 16 Jul 2013 14:09:03 +0000 (16:09 +0200)
sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java
sonar-ws-client/src/test/java/org/sonar/wsclient/MockHttpServer.java
sonar-ws-client/src/test/java/org/sonar/wsclient/MockHttpServerInterceptor.java
sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultActionPlanClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/permissions/DefaultPermissionClientTest.java
sonar-ws-client/src/test/java/org/sonar/wsclient/user/DefaultUserClientTest.java

index 19b604884c06f4be5c57d92564ec7f7c519c3151..184559fcde4af81c454d8ef7f47ab6d3ac04e6e7 100644 (file)
@@ -117,20 +117,19 @@ public class HttpRequestFactory {
   }
 
   public String get(String wsUrl, Map<String, Object> queryParams) {
-    HttpRequest request = HttpRequest.get(baseUrl + wsUrl, queryParams, true);
+    HttpRequest request = prepare(HttpRequest.get(baseUrl + wsUrl, queryParams, true));
     return execute(request);
   }
 
   public String post(String wsUrl, Map<String, Object> queryParams) {
-    HttpRequest request = HttpRequest.post(baseUrl + wsUrl, queryParams, true);
+    HttpRequest request = prepare(HttpRequest.post(baseUrl + wsUrl, true)).form(queryParams, HttpRequest.CHARSET_UTF8);
     return execute(request);
   }
 
   private String execute(HttpRequest request) {
     try {
-      prepare(request);
       if (request.ok()) {
-        return request.body("UTF-8");
+        return request.body(HttpRequest.CHARSET_UTF8);
       }
       // TODO handle error messages
       throw new HttpException(request.url().toString(), request.code());
@@ -140,7 +139,7 @@ public class HttpRequestFactory {
     }
   }
 
-  private void prepare(HttpRequest request) {
+  private HttpRequest prepare(HttpRequest request) {
     if (proxyHost != null) {
       request.useProxy(proxyHost, proxyPort);
       if (proxyLogin != null) {
@@ -159,5 +158,6 @@ public class HttpRequestFactory {
     if (login != null) {
       request.basic(login, password);
     }
+    return request;
   }
 }
index 0c2ab225b7830875b5861741f305d97623e3e6d8..b66b3e0014c5bf9d6f5405c8b5d3d62e6cc3dd91 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.wsclient;
 
-import org.apache.commons.io.IOUtils;
 import org.eclipse.jetty.server.Handler;
 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.Server;
@@ -28,7 +27,6 @@ import org.eclipse.jetty.server.handler.AbstractHandler;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-
 import java.io.IOException;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -41,8 +39,8 @@ public class MockHttpServer {
   private Server server;
   private String responseBody;
   private int responseStatus = SC_OK;
-  private String requestBody, requestPath;
-  private Map requestHeaders = new HashMap();
+  private String requestPath;
+  private Map requestHeaders = new HashMap(), requestParams = new HashMap();
 
   public void start() throws Exception {
     // 0 is random available port
@@ -56,13 +54,18 @@ public class MockHttpServer {
       @Override
       public void handle(String target, Request baseRequest, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException {
         requestPath = baseRequest.getUri().toString();
-        requestBody = IOUtils.toString(baseRequest.getInputStream());
         requestHeaders.clear();
-        Enumeration headerNames = baseRequest.getHeaderNames();
-        while (headerNames.hasMoreElements()) {
-          String headerName = (String)headerNames.nextElement();
+        Enumeration names = baseRequest.getHeaderNames();
+        while (names.hasMoreElements()) {
+          String headerName = (String)names.nextElement();
           requestHeaders.put(headerName, baseRequest.getHeader(headerName));
         }
+        requestParams.clear();
+        names = baseRequest.getParameterNames();
+        while (names.hasMoreElements()) {
+          String headerName = (String)names.nextElement();
+          requestParams.put(headerName, baseRequest.getParameter(headerName));
+        }
         response.setStatus(responseStatus);
         response.setContentType("application/json;charset=utf-8");
         write(responseBody, response.getOutputStream());
@@ -92,10 +95,6 @@ public class MockHttpServer {
     return this;
   }
 
-  public String requestBody() {
-    return requestBody;
-  }
-
   public String requestPath() {
     return requestPath;
   }
@@ -104,6 +103,10 @@ public class MockHttpServer {
     return requestHeaders;
   }
 
+  public Map requestParams() {
+    return requestParams;
+  }
+
   public int getPort() {
     return server.getConnectors()[0].getLocalPort();
   }
index 01d700d8c70212f6c073a3bbb1ebf3e6c09f25a1..5e870c599e16c21fdc91f6e74e89ce0c0e8f9fe9 100644 (file)
@@ -57,6 +57,10 @@ public final class MockHttpServerInterceptor extends ExternalResource {
     return server.requestHeaders();
   }
 
+  public Map requestParams() {
+    return server.requestParams();
+  }
+
   public int port() {
     return server.getPort();
   }
index 3e51e4245d5745b4e6103be9dd23b030cecff171..78a5959f328e7356e0c9bed20f23a54a7a32ad73 100644 (file)
@@ -37,6 +37,7 @@ import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.fest.assertions.Fail.fail;
+import static org.fest.assertions.MapAssert.entry;
 
 public class DefaultActionPlanClientTest {
 
@@ -86,7 +87,13 @@ public class DefaultActionPlanClientTest {
     ActionPlan result = client.create(
       NewActionPlan.create().name("Short term").project("org.sonar.Sample").description("Short term issues").deadLine(stringToDate("2014-01-01")));
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/create?project=org.sonar.Sample&description=Short%20term%20issues&name=Short%20term&deadLine=2014-01-01");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/create");
+    assertThat(httpServer.requestParams()).includes(
+      entry("project", "org.sonar.Sample"),
+      entry("description", "Short term issues"),
+      entry("name", "Short term"),
+      entry("deadLine", "2014-01-01")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -99,7 +106,13 @@ public class DefaultActionPlanClientTest {
     ActionPlan result = client.update(
       UpdateActionPlan.create().key("382f6f2e-ad9d-424a-b973-9b065e04348a").name("Short term").description("Short term issues").deadLine(stringToDate("2014-01-01")));
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/update?description=Short%20term%20issues&name=Short%20term&deadLine=2014-01-01&key=382f6f2e-ad9d-424a-b973-9b065e04348a");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/update");
+    assertThat(httpServer.requestParams()).includes(
+      entry("key", "382f6f2e-ad9d-424a-b973-9b065e04348a"),
+      entry("description", "Short term issues"),
+      entry("name", "Short term"),
+      entry("deadLine", "2014-01-01")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -110,7 +123,10 @@ public class DefaultActionPlanClientTest {
     ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
     client.delete("382f6f2e-ad9d-424a-b973-9b065e04348a");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/delete?key=382f6f2e-ad9d-424a-b973-9b065e04348a");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/delete");
+    assertThat(httpServer.requestParams()).includes(
+      entry("key", "382f6f2e-ad9d-424a-b973-9b065e04348a")
+    );
   }
 
   @Test
@@ -125,7 +141,10 @@ public class DefaultActionPlanClientTest {
     } catch (HttpException e) {
       assertThat(e.status()).isEqualTo(500);
       assertThat(e.url()).startsWith("http://localhost");
-      assertThat(e.url()).endsWith("/api/action_plans/delete?key=382f6f2e-ad9d-424a-b973-9b065e04348a");
+      assertThat(e.url()).endsWith("/api/action_plans/delete");
+      assertThat(httpServer.requestParams()).includes(
+        entry("key", "382f6f2e-ad9d-424a-b973-9b065e04348a")
+      );
     }
   }
 
@@ -137,7 +156,10 @@ public class DefaultActionPlanClientTest {
     ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
     ActionPlan result = client.open("382f6f2e-ad9d-424a-b973-9b065e04348a");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/open?key=382f6f2e-ad9d-424a-b973-9b065e04348a");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/open");
+    assertThat(httpServer.requestParams()).includes(
+      entry("key", "382f6f2e-ad9d-424a-b973-9b065e04348a")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -149,7 +171,10 @@ public class DefaultActionPlanClientTest {
     ActionPlanClient client = new DefaultActionPlanClient(requestFactory);
     ActionPlan result = client.close("382f6f2e-ad9d-424a-b973-9b065e04348a");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/close?key=382f6f2e-ad9d-424a-b973-9b065e04348a");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/action_plans/close");
+    assertThat(httpServer.requestParams()).includes(
+      entry("key", "382f6f2e-ad9d-424a-b973-9b065e04348a")
+    );
     assertThat(result).isNotNull();
   }
 
index 9befff8294fda5038f1d5db80b98d9d5d99e225f..96140bd593aad93d1f0b91a878e69c070cbca9a3 100644 (file)
@@ -31,6 +31,7 @@ import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.fest.assertions.Fail.fail;
+import static org.fest.assertions.MapAssert.entry;
 
 public class DefaultIssueClientTest {
   @Rule
@@ -74,7 +75,11 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     Issue result = client.setSeverity("ABCDE", "BLOCKER");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/set_severity?issue=ABCDE&severity=BLOCKER");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/set_severity");
+    assertThat(httpServer.requestParams()).includes(
+      entry("issue", "ABCDE"),
+      entry("severity", "BLOCKER")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -86,7 +91,11 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     Issue result = client.assign("ABCDE", "emmerik");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/assign?issue=ABCDE&assignee=emmerik");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/assign");
+    assertThat(httpServer.requestParams()).includes(
+      entry("issue", "ABCDE"),
+      entry("assignee", "emmerik")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -98,7 +107,10 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     Issue result = client.assign("ABCDE", null);
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/assign?issue=ABCDE");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/assign");
+    assertThat(httpServer.requestParams()).includes(
+      entry("issue", "ABCDE")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -110,7 +122,11 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     Issue result = client.plan("ABCDE", "DEFGH");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/plan?issue=ABCDE&plan=DEFGH");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/plan");
+    assertThat(httpServer.requestParams()).includes(
+      entry("issue", "ABCDE"),
+      entry("plan", "DEFGH")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -122,7 +138,10 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     Issue result = client.plan("ABCDE", null);
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/plan?issue=ABCDE");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/plan");
+    assertThat(httpServer.requestParams()).includes(
+      entry("issue", "ABCDE")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -134,7 +153,11 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     Issue result = client.create(NewIssue.create().component("Action.java").rule("squid:AvoidCycle"));
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/create?component=Action.java&rule=squid:AvoidCycle");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/create");
+    assertThat(httpServer.requestParams()).includes(
+      entry("component", "Action.java"),
+      entry("rule", "squid:AvoidCycle")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -164,7 +187,11 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     Issue result = client.doTransition("ABCDE", "resolve");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/do_transition?issue=ABCDE&transition=resolve");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/do_transition");
+    assertThat(httpServer.requestParams()).includes(
+      entry("issue", "ABCDE"),
+      entry("transition", "resolve")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -176,7 +203,11 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     IssueComment comment = client.addComment("ISSUE-1", "this is my comment");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/add_comment?issue=ISSUE-1&text=this%20is%20my%20comment");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/add_comment");
+    assertThat(httpServer.requestParams()).includes(
+      entry("issue", "ISSUE-1"),
+      entry("text", "this is my comment")
+    );
     assertThat(comment).isNotNull();
     assertThat(comment.key()).isEqualTo("COMMENT-123");
     assertThat(comment.htmlText()).isEqualTo("this is my comment");
@@ -210,7 +241,11 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     Issue result = client.doAction("ABCDE", "tweet");
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/do_action?issue=ABCDE&actionKey=tweet");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/do_action");
+    assertThat(httpServer.requestParams()).includes(
+      entry("issue", "ABCDE"),
+      entry("actionKey", "tweet")
+    );
     assertThat(result).isNotNull();
   }
 
@@ -227,7 +262,12 @@ public class DefaultIssueClientTest {
     IssueClient client = new DefaultIssueClient(requestFactory);
     BulkChange result = client.bulkChange(query);
 
-    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/bulk_change?assign.assignee=geoffrey&issues=ABCD,EFGH&actions=assign");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/bulk_change");
+    assertThat(httpServer.requestParams()).includes(
+      entry("assign.assignee", "geoffrey"),
+      entry("issues", "ABCD,EFGH"),
+      entry("actions", "assign")
+    );
     assertThat(result).isNotNull();
   }
 }
index c850665b14575fe60dc9d9627eeeb1b18aff9b58..b3a6a572f72c26ea34088d8e847909eb55ef592a 100644 (file)
@@ -26,15 +26,13 @@ import org.junit.Test;
 import org.sonar.wsclient.MockHttpServerInterceptor;
 import org.sonar.wsclient.internal.HttpRequestFactory;
 
-import java.util.Arrays;
-import java.util.List;
-
 import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.MapAssert.entry;
 
 public class DefaultPermissionClientTest {
 
-  private HttpRequestFactory requestFactory;
-  private DefaultPermissionClient client;
+  HttpRequestFactory requestFactory;
+  DefaultPermissionClient client;
 
   @Rule
   public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
@@ -52,7 +50,11 @@ public class DefaultPermissionClientTest {
     PermissionParameters params = PermissionParameters.create().user("daveloper").permission("admin");
     client.addPermission(params);
 
-    assertThatRequestUrlContains("/api/permissions/add?", "user=daveloper", "permission=admin");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/permissions/add");
+    assertThat(httpServer.requestParams()).includes(
+      entry("user", "daveloper"),
+      entry("permission", "admin")
+    );
   }
 
   @Test
@@ -62,7 +64,11 @@ public class DefaultPermissionClientTest {
     PermissionParameters params = PermissionParameters.create().group("my_group").permission("admin");
     client.addPermission(params);
 
-    assertThatRequestUrlContains("/api/permissions/add?", "group=my_group", "permission=admin");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/permissions/add");
+    assertThat(httpServer.requestParams()).includes(
+      entry("group", "my_group"),
+      entry("permission", "admin")
+    );
   }
 
   @Test
@@ -72,7 +78,11 @@ public class DefaultPermissionClientTest {
     PermissionParameters params = PermissionParameters.create().user("daveloper").permission("admin");
     client.removePermission(params);
 
-    assertThatRequestUrlContains("/api/permissions/remove?", "user=daveloper", "permission=admin");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/permissions/remove");
+    assertThat(httpServer.requestParams()).includes(
+      entry("user", "daveloper"),
+      entry("permission", "admin")
+    );
   }
 
   @Test
@@ -82,12 +92,10 @@ public class DefaultPermissionClientTest {
     PermissionParameters params = PermissionParameters.create().group("my_group").permission("admin");
     client.removePermission(params);
 
-    assertThatRequestUrlContains("/api/permissions/remove?", "group=my_group", "permission=admin");
-  }
-
-  private void assertThatRequestUrlContains(String baseUrl, String... parameters) {
-    assertThat(httpServer.requestedPath()).startsWith(baseUrl);
-    List<String> requestParameters = Arrays.asList(httpServer.requestedPath().substring(baseUrl.length()).split("&"));
-    assertThat(requestParameters).containsOnly(parameters);
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/permissions/remove");
+    assertThat(httpServer.requestParams()).includes(
+      entry("group", "my_group"),
+      entry("permission", "admin")
+    );
   }
 }
index 90e6865f28bbe52550afbfb209214774184d8056..7056a2d32e7091976f142eb1081672c04851218b 100644 (file)
@@ -29,11 +29,12 @@ import java.util.Arrays;
 import java.util.List;
 
 import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.MapAssert.entry;
 
 public class DefaultUserClientTest {
 
-  private HttpRequestFactory requestFactory;
-  private DefaultUserClient client;
+  HttpRequestFactory requestFactory;
+  DefaultUserClient client;
 
   @Rule
   public MockHttpServerInterceptor httpServer = new MockHttpServerInterceptor();
@@ -51,7 +52,7 @@ public class DefaultUserClientTest {
     UserQuery query = UserQuery.create().logins("simon", "loic");
     List<User> users = client.find(query);
 
-    assertThatRequestUrlContains("/api/users/search?", "logins=simon,loic");
+    assertThatGetRequestUrlContains("/api/users/search?", "logins=simon,loic");
     assertThat(users).hasSize(1);
     User simon = users.get(0);
     assertThat(simon.login()).isEqualTo("simon");
@@ -67,7 +68,12 @@ public class DefaultUserClientTest {
     UserParameters params = UserParameters.create().login("daveloper").password("pass1").passwordConfirmation("pass1");
     User createdUser = client.create(params);
 
-    assertThatRequestUrlContains("/api/users/create?", "login=daveloper", "password=pass1", "password_confirmation=pass1");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/users/create");
+    assertThat(httpServer.requestParams()).includes(
+      entry("login", "daveloper"),
+      entry("password", "pass1"),
+      entry("password_confirmation", "pass1")
+    );
     assertThat(createdUser).isNotNull();
     assertThat(createdUser.login()).isEqualTo("daveloper");
     assertThat(createdUser.name()).isEqualTo("daveloper");
@@ -81,7 +87,11 @@ public class DefaultUserClientTest {
     UserParameters params = UserParameters.create().login("daveloper").email("new_email");
     User updatedUser = client.update(params);
 
-    assertThatRequestUrlContains("/api/users/update?", "login=daveloper", "email=new_email");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/users/update");
+    assertThat(httpServer.requestParams()).includes(
+      entry("login", "daveloper"),
+      entry("email", "new_email")
+    );
     assertThat(updatedUser).isNotNull();
     assertThat(updatedUser.login()).isEqualTo("daveloper");
     assertThat(updatedUser.name()).isEqualTo("daveloper");
@@ -94,10 +104,13 @@ public class DefaultUserClientTest {
 
     client.deactivate("daveloper");
 
-    assertThatRequestUrlContains("/api/users/deactivate?", "login=daveloper");
+    assertThat(httpServer.requestedPath()).isEqualTo("/api/users/deactivate");
+    assertThat(httpServer.requestParams()).includes(
+      entry("login", "daveloper")
+    );
   }
 
-  private void assertThatRequestUrlContains(String baseUrl, String... parameters) {
+  private void assertThatGetRequestUrlContains(String baseUrl, String... parameters) {
     assertThat(httpServer.requestedPath()).startsWith(baseUrl);
     List<String> requestParameters = Arrays.asList(httpServer.requestedPath().substring(baseUrl.length()).split("&"));
     assertThat(requestParameters).containsOnly(parameters);