public static RestIdMatcher asIdMatcher(final String id) { if ("*".equals(id)) { return MocoRest.anyId(); } return RestIdMatchers.eq(id); }
@Test(expected = IllegalArgumentException.class) public void should_throw_exception_for_invalid_sub_resource_name() { id(anyId()).name("hello world"); }
@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_get_resource_by_any_id() throws Exception { Plain resource = new Plain(); resource.code = 1; resource.message = "hello"; server.resource("targets", get(anyId()).response(json(resource)) ); running(server, new Runnable() { @Override public void run() throws Exception { Plain response1 = getResource("/targets/1"); assertThat(response1.code, is(1)); assertThat(response1.message, is("hello")); Plain response2 = getResource("/targets/2"); assertThat(response2.code, is(1)); assertThat(response2.message, is("hello")); } }); }
@Test public void should_put_with_any_id() throws Exception { RestServer server = restServer(12306); final Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; server.resource("targets", put(anyId()).response(status(200)) ); running(server, new Runnable() { @Override public void run() throws Exception { HttpResponse httpResponse1 = helper.putForResponse(remoteUrl("/targets/1"), Jsons.toJson(resource1)); assertThat(httpResponse1.getStatusLine().getStatusCode(), is(200)); HttpResponse httpResponse2 = helper.putForResponse(remoteUrl("/targets/2"), Jsons.toJson(resource1)); assertThat(httpResponse2.getStatusLine().getStatusCode(), is(200)); } }); }
@Test public void should_get_sub_resource_with_any_id() throws Exception { Plain resource1 = new Plain(); resource1.code = 1; resource1.message = "hello"; Plain resource2 = new Plain(); resource2.code = 2; resource2.message = "world"; server.resource("targets", id(anyId()).name("sub").settings( get("1").response(json(resource1)), get("2").response(json(resource2)) ) ); running(server, new Runnable() { @Override public void run() throws Exception { Plain response1 = getResource("/targets/1/sub/1"); assertThat(response1.code, is(1)); assertThat(response1.message, is("hello")); Plain response2 = getResource("/targets/2/sub/2"); assertThat(response2.code, is(2)); assertThat(response2.message, is("world")); } }); }
public static RestIdMatcher asIdMatcher(final String id) { if ("*".equals(id)) { return MocoRest.anyId(); } return RestIdMatchers.eq(id); }