private <T> RequestMatcher existMatcher(final RequestExtractor<T> extractor, final TextContainer container) { String text = container.getText(); if ("true".equalsIgnoreCase(text)) { return exist(extractor); } if ("false".equalsIgnoreCase(text)) { return not(exist(extractor)); } throw new IllegalArgumentException(String.format("Unknown exist parameter: [%s]", text)); }
@Test public void should_match_request_based_on_not_matcher() throws Exception { server.request(not(by(uri("/foo")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.get(remoteUrl("/bar")), is("bar")); } }); }
@Test public void should_match_request_based_on_not_matcher() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.request(not(by(uri("/foo")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { assertThat(helper.getWithHeader(remoteUrl("/bar"), of("foo", "bar")), is("bar")); } }); }
@Test(expected = HttpResponseException.class) public void should_throw_exception_without_match_not() throws Exception { server = httpServer(port(), request(eq(header("foo"), "bar"))); server.request(not(by(uri("/foo")))).response("bar"); running(server, new Runnable() { @Override public void run() throws IOException { helper.get(remoteUrl("/bar")); } }); }
private <T> RequestMatcher existMatcher(final RequestExtractor<T> extractor, final TextContainer container) { String text = container.getText(); if ("true".equalsIgnoreCase(text)) { return exist(extractor); } if ("false".equalsIgnoreCase(text)) { return not(exist(extractor)); } throw new IllegalArgumentException(String.format("Unknown exist parameter: [%s]", text)); }
@Before("@faker") public void faker() { server.server() .post(and( by(uri("/faker/users")), exist(jsonPath("$.name")), not(startsWith(jsonPath("$.name"), "#{")), exist(jsonPath("$.city")), not(startsWith(jsonPath("$.city"), "#{")) )) .response("success"); server.server() .post(and( by(uri("/faker/users/escape")), exist(jsonPath("$.name")), not(startsWith(jsonPath("$.name"), "#{")), exist(jsonPath("$.city")), eq(jsonPath("$.city"), "#{Address.city}") )) .response("success"); server.start(); }
@Test public void can_handle_authorization() throws Exception { // We'll respond 401 if no Authorization header is received server.request(and( by(uri("/")), by(method(GET)), not(exist(header("Authorization"))) )).response( status(401), // We include a Digest challenge header on the response header("WWW-Authenticate", "Digest realm=\"Aggregate ODK Aggregate\", qop=\"auth\", nonce=\"MTUzNjA1NzY1MjI2NDoxNjIwYTZmMmJkYTNiNGRkMTA2MjI4ZjM4YjE0ZDIyMA==\"") ); // We'll respond 200 if we get a valid Authorization header server.request(and( by(uri("/")), by(method(GET)), // The Authorization header should include the expected username contain(header("Authorization"), "username=\"username\""), // The Authorization header should include the expected realm (from the challenge) contain(header("Authorization"), "realm=\"Aggregate ODK Aggregate\""), // The Authorization header should include the expected nonce (from the challenge) contain(header("Authorization"), "nonce=\"MTUzNjA1NzY1MjI2NDoxNjIwYTZmMmJkYTNiNGRkMTA2MjI4ZjM4YjE0ZDIyMA==\""), // The Authorization header should include the challenge response match(header("Authorization"), ".+response=\"[a-z0-9]+\".+") )).response("foo"); running(server, () -> assertThat( http.execute(Request.get(BASE_URL, Credentials.from("username", "password"))), instanceOf(Response.Success.class) )); }