WebActionRequestHandler extends SimpleRequestHandler<WebActionRequest, JsonObject> { @Override protected JRestlessContainerRequest createContainerRequest(WebActionRequest request) { requireNonNull(request); final String httpMethod = requireNonNull(request.getMethod(), "httpMethod must be given").toUpperCase(); final String body = request.getBody(); final Map<String, String> requestHeaders = request.getHeaders(); final Map<String, List<String>> containerRequestHeaders; if (requestHeaders == null) { containerRequestHeaders = Collections.emptyMap(); } else { containerRequestHeaders = HeaderUtils.expandHeaders(request.getHeaders()); } InputStream entityStream; if (body != null) { entityStream = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)); } else { entityStream = new ByteArrayInputStream(new byte[0]); } final RequestAndBaseUri requestAndBaseUri = getRequestAndBaseUri(request); return new DefaultJRestlessContainerRequest(requestAndBaseUri, httpMethod, entityStream, containerRequestHeaders); }  JsonObject delegateJsonRequest(@Nonnull JsonObject request);  }
@Test public void createContainerRequest_NullRequestGiven_ShouldFailWithNpe() { assertThrows(NullPointerException.class, () -> handler.createContainerRequest(null)); }
@Test public void createContainerRequest_NullMethodGiven_ShouldFailWithNpe() { assertThrows(NullPointerException.class, () -> { WebActionRequest request = minimalRequestBuilder() .setHttpMethod(null) .build(); handler.createContainerRequest(request); }); }
@Test public void createContainerRequest_NullHeadersGiven_ShouldMapToEmptyHeaders() { WebActionRequest request = minimalRequestBuilder() .setHeaders(null) .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertEquals(Collections.emptyMap(), containerRequest.getHeaders()); }
@Test public void createContainerRequest_NullBodyGiven_ShouldMapToEmptyInputStream() { WebActionRequest request = minimalRequestBuilder() .setBody(null) .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertArrayEquals(new byte[0], IOUtils.toBytes(containerRequest.getEntityStream())); }
@Test public void createContainerRequest_BodyGiven_ShouldMapToInputStream() { WebActionRequest request = minimalRequestBuilder() .setBody("some body") .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertArrayEquals("some body".getBytes(), IOUtils.toBytes(containerRequest.getEntityStream())); }
@Test public void createContainerRequest_HeadersGiven_ShouldExpandHeaders() { Map<String, String> requestHeaders = ImmutableMap.of("hk0", "hv0", "hk1", "hv1"); WebActionRequest request = minimalRequestBuilder() .setHeaders(requestHeaders) .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertEquals(ImmutableMap.of("hk0", Collections.singletonList("hv0"), "hk1", Collections.singletonList("hv1")), containerRequest.getHeaders()); }
@Test public void createContainerRequest_NullPathGiven_ShouldMapToRootRequestPath() { WebActionRequest request = minimalRequestBuilder() .setPath(null) .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertEquals(URI.create("/"), containerRequest.getRequestUri()); assertEquals(URI.create("/"), containerRequest.getBaseUri()); }
@Test public void createContainerRequest_EmptyPathGiven_ShouldMapToRootRequestPath() { WebActionRequest request = minimalRequestBuilder() .setPath("") .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertEquals(URI.create("/"), containerRequest.getRequestUri()); assertEquals(URI.create("/"), containerRequest.getBaseUri()); }
@Test public void createContainerRequest_NonAbsolutePathGiven_ShouldPrependSlashToRequestPath() { WebActionRequest request = minimalRequestBuilder() .setPath("path") .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertEquals(URI.create("/path"), containerRequest.getRequestUri()); assertEquals(URI.create("/"), containerRequest.getBaseUri()); }
@Test public void createContainerRequest_AbsolutePathGiven_ShouldMapRequestPathAsIs() { WebActionRequest request = minimalRequestBuilder() .setPath("/path") .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertEquals(URI.create("/path"), containerRequest.getRequestUri()); assertEquals(URI.create("/"), containerRequest.getBaseUri()); }
@Test public void createContainerRequest_PathAndEmpyQueryGiven_ShouldNotAppendQuery() { WebActionRequest request = minimalRequestBuilder() .setPath("/path") .setQuery("") .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertEquals(URI.create("/path"), containerRequest.getRequestUri()); assertEquals(URI.create("/"), containerRequest.getBaseUri()); }
@Test public void createContainerRequest_PathAndQueryGiven_ShouldNotAppendQuery() { WebActionRequest request = minimalRequestBuilder() .setPath("/path") .setQuery("a=b") .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertEquals(URI.create("/path?a=b"), containerRequest.getRequestUri()); assertEquals(URI.create("/"), containerRequest.getBaseUri()); }