@Test(expected = IllegalArgumentException.class) public void should_throw_exception_for_resource_name_with_space() { server.resource("hello world", get().response("hello")); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_for_get_id_with_space() { get("1 1").response(status(200)); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_for_get_id_with_slash() throws Exception { get("1/1").response(status(200)); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_for_resource_name_with_slash() { server.resource("hello/world", get().response("hello")); }
@Test public void should_delete() throws Exception { server.resource("targets", delete("1").response(status(200)) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.deleteForResponse(remoteUrl("/targets/1")); assertThat(httpResponse.getStatusLine().getStatusCode(), is(200)); } }); }
@Test public void should_not_head_with_unknown_id() throws Exception { server.resource("targets", head("1").response(header("ETag", "Moco")) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.headForResponse(remoteUrl("/targets/2")); assertThat(httpResponse.getStatusLine().getStatusCode(), is(404)); } }); }
@Test public void should_not_delete_with_unknown_id() throws Exception { server.resource("targets", delete("1").response(status(200)) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.deleteForResponse(remoteUrl("/targets/2")); assertThat(httpResponse.getStatusLine().getStatusCode(), is(404)); } }); }
@Test public void should_delete_with_response() throws Exception { server.resource("targets", delete("1").response(status(409)) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.deleteForResponse(remoteUrl("/targets/1")); assertThat(httpResponse.getStatusLine().getStatusCode(), is(409)); } }); }
@Test public void should_head() throws Exception { server.resource("targets", head("1").response(header("ETag", "Moco")) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.headForResponse(remoteUrl("/targets/1")); assertThat(httpResponse.getStatusLine().getStatusCode(), is(200)); } }); }
@Test public void should_head_with_all() throws Exception { server.resource("targets", head().response(header("ETag", "Moco")) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.headForResponse(remoteUrl("/targets")); assertThat(httpResponse.getStatusLine().getStatusCode(), is(200)); assertThat(httpResponse.getHeaders("ETag")[0].getValue(), is("Moco")); } }); }
@Test public void should_reply_404_for_unknown_resource() throws Exception { server.resource("targets", get("2").response(with(text("hello")))); running(server, new Runnable() { @Override public void run() throws Exception { org.apache.http.HttpResponse response = helper.getResponse(remoteUrl("/targets/1")); assertThat(response.getStatusLine().getStatusCode(), is(404)); } }); }
@Test public void should_patch() throws Exception { server.resource("targets", patch("1").response(with(text("patch result"))) ); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.patchForResponse(remoteUrl("/targets/1"), "result"), is("patch result")); } }); }
@Test public void should_delete_with_any_id() throws Exception { server.resource("targets", delete(anyId()).response(status(200)) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse1 = helper.deleteForResponse(remoteUrl("/targets/1")); assertThat(httpResponse1.getStatusLine().getStatusCode(), is(200)); HttpResponse httpResponse2 = helper.deleteForResponse(remoteUrl("/targets/2")); assertThat(httpResponse2.getStatusLine().getStatusCode(), is(200)); } }); }
@Test public void should_patch_with_any_id() throws Exception { server.resource("targets", patch(anyId()).response(with(text("patch result"))) ); running(server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.patchForResponse(remoteUrl("/targets/1"), "result"), is("patch result")); } }); }
@Test public void should_head_with_any_id() throws Exception { server.resource("targets", head(anyId()).response(header("ETag", "Moco")) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse1 = helper.headForResponse(remoteUrl("/targets/1")); assertThat(httpResponse1.getStatusLine().getStatusCode(), is(200)); HttpResponse httpResponse2 = helper.headForResponse(remoteUrl("/targets/2")); assertThat(httpResponse2.getStatusLine().getStatusCode(), is(200)); } }); }
@Test public void should_put() throws Exception { RestServer server = restServer(12306); final Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; server.resource("targets", put("1").response(status(200)) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.putForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(200)); } }); }
@Test public void should_not_put_with_unknown_id() throws Exception { RestServer server = restServer(12306); final Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; server.resource("targets", put("1").response(status(200)) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.putForResponse(remoteUrl("/targets/2"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(404)); } }); }
@Test public void should_put_with_response_handler() throws Exception { RestServer server = restServer(12306); final Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; server.resource("targets", put("1").response(status(409)) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.putForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(409)); } }); }
@Test public void should_return_404_for_post_with_id_by_default() throws Exception { RestServer server = restServer(12306); final Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; server.resource("targets", post().response(status(201), header("Location", "/targets/123")) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.postForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(404)); } }); }
@Test public void should_post() throws Exception { RestServer server = restServer(12306); final Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; server.resource("targets", post().response(status(201), header("Location", "/targets/123")) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse = helper.postForResponse(remoteUrl("/targets"), Jsons.toJson(resource1)); assertThat(httpResponse.getStatusLine().getStatusCode(), is(201)); assertThat(httpResponse.getFirstHeader("Location").getValue(), is("/targets/123")); } }); }