/** * Create a JAX-RS web target whose URI refers to the {@link #getBaseUri() base URI} the tested * JAX-RS / Jersey application is deployed at, plus the path specified in the {@code path} argument. * <p> * This method is an equivalent of calling {@code target().path(path)}. * </p> * * @param path relative path (from tested application base URI) this web target should point to. * @return the created JAX-RS web target. */ public final WebTarget target(final String path) { return target().path(path); }
/** * Creates a web target to be sent to the resource under testing. * * @param path relative path (from tested application base URI) this web target should point to. * @return the created JAX-RS web target. */ public WebTarget target(String path) { return getJerseyTest().target(path); }
return _jerseyTest.target(path);
/** * Create a JAX-RS web target whose URI refers to the {@link #getBaseUri() base URI} the tested * JAX-RS / Jersey application is deployed at, plus the path specified in the {@code path} argument. * <p> * This method is an equivalent of calling {@code target().path(path)}. * </p> * * @param path relative path (from tested application base URI) this web target should point to. * @return the created JAX-RS web target. */ public final WebTarget target(final String path) { return target().path(path); }
/** * Creates a web target to be sent to the resource under testing. * * @param path relative path (from tested application base URI) this web target should point to. * @return the created JAX-RS web target. */ public WebTarget target(String path) { return getJerseyTest().target(path); }
/** * Constructs and sends a request to a specified URL. * <p> * If the request has query parameters, please use {@link #makeRequest(String, Map)} instead. * * @param target The specified URL * * @return a request builder which user can use to send different types of requests, such as HTTP HEAD and HTTP GET * methods. */ public Builder makeRequest(String target) { return getHarness().target(target).request(); } }
/** * Constructs and sends a request to a specified URL. * <p> * If the request has query parameters, please use {@link #makeRequest(String, Map)} instead. * * @param target The specified URL * * @return a request builder which user can use to send different types of requests, such as HTTP HEAD and HTTP GET * methods. */ public Builder makeRequest(String target) { return getHarness().target(target).request(); } }
/** * Constructs and sends a request to a specified URL with specified query parameters. * <p> * If the request does not have any query parameters, please use {@link #makeRequest(String)} instead. * * @param target The specified URL * @param queryParams The specified query parameters * * @return a request builder which user can use to send different types of requests, such as HTTP HEAD and HTTP GET * methods. */ public Builder makeRequest(String target, Map<String, Object> queryParams) { // Set target of call WebTarget httpCall = getHarness().target(target); // Add query params to call for (Map.Entry<String, Object> entry : queryParams.entrySet()) { httpCall = httpCall.queryParam(entry.getKey(), entry.getValue()); } return httpCall.request(); }
/** * Constructs and sends a request to a specified URL with specified query parameters. * <p> * If the request does not have any query parameters, please use {@link #makeRequest(String)} instead. * * @param target The specified URL * @param queryParams The specified query parameters * * @return a request builder which user can use to send different types of requests, such as HTTP HEAD and HTTP GET * methods. */ public Builder makeRequest(String target, Map<String, Object> queryParams) { // Set target of call WebTarget httpCall = getHarness().target(target); // Add query params to call for (Map.Entry<String, Object> entry : queryParams.entrySet()) { httpCall = httpCall.queryParam(entry.getKey(), entry.getValue()); } return httpCall.request(); }
@Test @DisplayName("should not limit if the backing rate limiter throws exception") public void shouldNotLimitIfBackingRateLimiterFails() { when(requestRateLimiterFactory.getInstance(anySet())).thenReturn(requestRateLimiter); when(requestRateLimiter.overLimitWhenIncremented(anyString())).thenThrow(new RuntimeException()); Response response = rule.getJerseyTest().target("/test/{id}").resolveTemplate("id", 1) .request(MediaType.APPLICATION_JSON_TYPE) .get(); assertThat(response.getStatus()).isEqualTo(200); }
@Test @DisplayName("should configure rate limiter") public void shouldReportOnly() { when(requestRateLimiterFactory.getInstance(anySet())).thenReturn(requestRateLimiter); when(requestRateLimiter.overLimitWhenIncremented(anyString())).thenReturn(true); Response response = rule.getJerseyTest().target("/test/reportOnly/{id}").resolveTemplate("id", 1) .request(MediaType.APPLICATION_JSON_TYPE) .get(); assertThat(response.getStatus()).isEqualTo(200); }
@Test @DisplayName("should limit request returning a 429") public void shouldLimit() { when(requestRateLimiterFactory.getInstance(anySet())).thenReturn(requestRateLimiter); when(requestRateLimiter.overLimitWhenIncremented(anyString())).thenReturn(true); Response response = rule.getJerseyTest().target("/test/{id}").resolveTemplate("id", 1) .request(MediaType.APPLICATION_JSON_TYPE) .get(); assertThat(response.getStatus()).isEqualTo(429); }
@Test @DisplayName("should not limit request") public void shouldNotLimit() { when(requestRateLimiterFactory.getInstance(anySet())).thenReturn(requestRateLimiter); when(requestRateLimiter.overLimitWhenIncremented(anyString())).thenReturn(false); Response response = rule.getJerseyTest().target("/test/{id}").resolveTemplate("id", 1) .request(MediaType.APPLICATION_JSON_TYPE) .get(); assertThat(response.getStatus()).isEqualTo(200); }
@Test public void testNonBinaryNotZippedWhenNoZippingRequestedAndNotBinaryCompressionOnly() throws Exception { runJerseyTest(createJerseyTest(false), (jersey) -> { Response response = jersey.target() .path("/non-binary") .request() .get(); assertEquals(200, response.getStatus()); assertNull(response.getHeaderString(HttpHeaders.CONTENT_ENCODING)); assertNull(response.getHeaderString(GatewayBinaryResponseFilter.HEADER_BINARY_RESPONSE)); assertEquals("non-binary", response.readEntity(String.class)); }); }
@Test public void testBinaryNotZippedWhenZippingNotRequested() throws Exception { runJerseyTest(createJerseyTest(true), (jersey) -> { Response response = jersey.target() .path("/binary") .request() .get(); assertEquals(200, response.getStatus()); assertNull(response.getHeaderString(HttpHeaders.CONTENT_ENCODING)); assertNotNull(response.getHeaderString(GatewayBinaryResponseFilter.HEADER_BINARY_RESPONSE)); assertEquals("binary", response.readEntity(String.class)); }); }
@Test public void testNonBinaryNotZippedWhenNoZippingRequestedAndBinaryCompressionOnly() throws Exception { runJerseyTest(createJerseyTest(true), (jersey) -> { Response response = jersey.target() .path("/non-binary") .request() .get(); assertEquals(200, response.getStatus()); assertNull(response.getHeaderString(HttpHeaders.CONTENT_ENCODING)); assertNull(response.getHeaderString(GatewayBinaryResponseFilter.HEADER_BINARY_RESPONSE)); assertEquals("non-binary", response.readEntity(String.class)); }); }
@Test public void testNonBinaryZippedWhenZippingRequestedAndNotBinaryCompressionOnly() throws Exception { runJerseyTest(createJerseyTest(false), (jersey) -> { Response response = jersey.target() .path("/non-binary") .request() .header(HttpHeaders.ACCEPT_ENCODING, "gzip") .get(); assertEquals(200, response.getStatus()); assertNotNull(response.getHeaderString(HttpHeaders.CONTENT_ENCODING)); assertNotNull(response.getHeaderString(GatewayBinaryResponseFilter.HEADER_BINARY_RESPONSE)); InputStream unzipStream = new GZIPInputStream(response.readEntity(InputStream.class)); assertEquals("non-binary", new String(IOUtils.toBytes(unzipStream))); }); }
@Test public void testNonBinaryNotZippedWhenZippingRequestedAndBinaryCompressionOnly() throws Exception { runJerseyTest(createJerseyTest(true), (jersey) -> { Response response = jersey.target() .path("/non-binary") .request() .header(HttpHeaders.ACCEPT_ENCODING, "gzip") .get(); assertEquals(200, response.getStatus()); assertNull(response.getHeaderString(HttpHeaders.CONTENT_ENCODING)); assertNull(response.getHeaderString(GatewayBinaryResponseFilter.HEADER_BINARY_RESPONSE)); assertEquals("non-binary", response.readEntity(String.class)); }); }
@Test public void testBinaryZippedWhenZippingRequested() throws Exception { runJerseyTest(createJerseyTest(true), (jersey) -> { Response response = jersey.target() .path("/binary") .request() .header(HttpHeaders.ACCEPT_ENCODING, "gzip") .get(); assertEquals(200, response.getStatus()); assertNotNull(response.getHeaderString(HttpHeaders.CONTENT_ENCODING)); assertNotNull(response.getHeaderString(GatewayBinaryResponseFilter.HEADER_BINARY_RESPONSE)); InputStream unzipStream = new GZIPInputStream(response.readEntity(InputStream.class)); assertEquals("binary", IOUtils.toString(unzipStream)); }); }