}
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());
}
}
- private void prepare(HttpRequest request) {
+ private HttpRequest prepare(HttpRequest request) {
if (proxyHost != null) {
request.useProxy(proxyHost, proxyPort);
if (proxyLogin != null) {
if (login != null) {
request.basic(login, password);
}
+ return request;
}
}
*/
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;
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;
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
@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());
return this;
}
- public String requestBody() {
- return requestBody;
- }
-
public String requestPath() {
return requestPath;
}
return requestHeaders;
}
+ public Map requestParams() {
+ return requestParams;
+ }
+
public int getPort() {
return server.getConnectors()[0].getLocalPort();
}
return server.requestHeaders();
}
+ public Map requestParams() {
+ return server.requestParams();
+ }
+
public int port() {
return server.getPort();
}
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
+import static org.fest.assertions.MapAssert.entry;
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();
}
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();
}
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
} 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")
+ );
}
}
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();
}
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();
}
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
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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();
}
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");
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();
}
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();
}
}
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();
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
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
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
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")
+ );
}
}
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();
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");
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");
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");
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);