@Test public void testProxyWithJSONResponse() 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); Map map = new Gson().fromJson(new InputStreamReader(new ByteArrayInputStream("{ \"test\":\"test\" }".getBytes())), Map.class); 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(streamProviderMock.processURL("testurl", "GET", (InputStream) null, headerParamsToForward)).andReturn(urlConnectionMock); expect(urlConnectionMock.getResponseCode()).andReturn(200); expect(urlConnectionMock.getContentType()).andReturn("application/json"); expect(urlConnectionMock.getInputStream()).andReturn(new ByteArrayInputStream("{ \"test\":\"test\" }".getBytes())); PowerMock.expectNew(URLStreamProvider.class, 20000, 15000, null, null, null).andReturn(streamProviderMock); expect(Response.status(200)).andReturn(responseBuilderMock); expect(responseBuilderMock.entity(map)).andReturn(responseBuilderMock); expect(responseBuilderMock.type("application/json")).andReturn(responseBuilderMock); expect(responseBuilderMock.build()).andReturn(responseMock); PowerMock.replay(streamProviderMock, URLStreamProvider.class, Response.class, responseBuilderMock, uriMock, URI.class); replay(getUriInfo(), urlConnectionMock, getHttpHeaders()); Response resultForGetRequest = ps.processGetRequestForwarding(getHttpHeaders(),getUriInfo()); assertSame(resultForGetRequest, responseMock); }
@GET public Response processGetRequestForwarding(@Context HttpHeaders headers, @Context UriInfo ui) { return handleRequest(REQUEST_TYPE_GET, ui, null, headers); }
ProxyService { @GET public Response processGetRequestForwarding(@Context HttpHeaders headers, @Context UriInfo ui) { return handleRequest(REQUEST_TYPE_GET, ui, null, headers); } }
ProxyService { @GET public Response processGetRequestForwarding(@Context HttpHeaders headers, @Context UriInfo ui) { return handleRequest(REQUEST_TYPE_GET, ui, null, headers); }  }
ProxyService { @GET public Response processGetRequestForwarding(@Context HttpHeaders headers, @Context UriInfo ui) { return handleRequest(REQUEST_TYPE_GET, ui, null, 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); }
ProxyService { @GET public Response processGetRequestForwarding(@Context HttpHeaders headers, @Context UriInfo ui) { return handleRequest(REQUEST_TYPE_GET, ui, null, 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; }