ProxyService { @POST @Consumes({MediaType.WILDCARD, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON}) public Response processPostRequestForwarding(InputStream body, @Context HttpHeaders headers, @Context UriInfo ui) { return handleRequest(REQUEST_TYPE_POST, ui, body, headers); }  @GET Response processGetRequestForwarding(@Context HttpHeaders headers, @Context UriInfo ui); @POST @Consumes({MediaType.WILDCARD, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON}) Response processPostRequestForwarding(InputStream body, @Context HttpHeaders headers, @Context UriInfo ui); @PUT @Consumes({MediaType.WILDCARD, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON}) Response processPutRequestForwarding(InputStream body, @Context HttpHeaders headers, @Context UriInfo ui); @DELETE Response processDeleteRequestForwarding(@Context HttpHeaders headers, @Context UriInfo ui); static final int URL_CONNECT_TIMEOUT; static final int URL_READ_TIMEOUT; static final int HTTP_ERROR_RANGE_START; }
@Test public void testProxyPostRequest() throws Exception { ProxyService ps = new ProxyService(); URLStreamProvider streamProviderMock = PowerMock.createNiceMock(URLStreamProvider.class); HttpURLConnection urlConnectionMock = createMock(HttpURLConnection.class); URI uriMock = PowerMock.createMock(URI.class); MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); MultivaluedMap<String, String> headerParams = new MultivaluedMapImpl(); Map<String, List<String>> headerParamsToForward = new HashMap<String, List<String>>(); Response.ResponseBuilder responseBuilderMock = PowerMock.createMock(ResponseBuilderImpl.class); Response responseMock = createMock(ResponseImpl.class); headerParams.add("AmbariProxy-User-Remote","testuser"); headerParams.add("Content-Type","testtype"); List<String> userRemoteParams = new LinkedList<String>(); userRemoteParams.add("testuser"); headerParamsToForward.put("User-Remote", userRemoteParams); InputStream is = new ByteArrayInputStream("test".getBytes()); PowerMock.mockStatic(Response.class); expect(getHttpHeaders().getRequestHeaders()).andReturn(headerParams); expect(getHttpHeaders().getRequestHeader("AmbariProxy-User-Remote")).andReturn(userRemoteParams); expect(getUriInfo().getRequestUri()).andReturn(uriMock); expect(getUriInfo().getQueryParameters()).andReturn(queryParams); expect(uriMock.getQuery()).andReturn("url=testurl"); expect(getHttpHeaders().getMediaType()).andReturn(APPLICATION_FORM_URLENCODED_TYPE); expect(streamProviderMock.processURL("testurl", "POST", is, headerParamsToForward)).andReturn(urlConnectionMock); expect(urlConnectionMock.getResponseCode()).andReturn(200); expect(urlConnectionMock.getContentType()).andReturn("text/plain"); expect(urlConnectionMock.getInputStream()).andReturn(is); PowerMock.expectNew(URLStreamProvider.class, 20000, 15000, null, null, null).andReturn(streamProviderMock); expect(Response.status(200)).andReturn(responseBuilderMock); expect(responseBuilderMock.entity(is)).andReturn(responseBuilderMock); expect(responseBuilderMock.type("text/plain")).andReturn(responseBuilderMock); expect(responseBuilderMock.build()).andReturn(responseMock); PowerMock.replay(streamProviderMock, URLStreamProvider.class, Response.class, responseBuilderMock, uriMock, URI.class); replay(getUriInfo(), urlConnectionMock, getHttpHeaders()); Response resultForPostRequest = ps.processPostRequestForwarding(is, getHttpHeaders(), getUriInfo()); assertSame(resultForPostRequest, responseMock); }