@Test public void testResponseWithError() throws Exception { ProxyService ps = new ProxyService(); URLStreamProvider streamProviderMock = PowerMock.createNiceMock(URLStreamProvider.class); HttpURLConnection urlConnectionMock = createMock(HttpURLConnection.class); Response.ResponseBuilder responseBuilderMock = PowerMock.createMock(ResponseBuilderImpl.class); URI uriMock = PowerMock.createMock(URI.class); Response responseMock = createMock(ResponseImpl.class); InputStream es = new ByteArrayInputStream("error".getBytes()); MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl(); MultivaluedMap<String, String> headerParams = new MultivaluedMapImpl(); Map<String, List<String>> headerParamsToForward = new HashMap<String, List<String>>(); 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); 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(400).times(2); expect(urlConnectionMock.getContentType()).andReturn("text/plain"); expect(urlConnectionMock.getErrorStream()).andReturn(es); expect(Response.status(400)).andReturn(responseBuilderMock); expect(responseBuilderMock.entity(es)).andReturn(responseBuilderMock); expect(responseBuilderMock.type("text/plain")).andReturn(responseBuilderMock); expect(responseBuilderMock.build()).andReturn(responseMock); PowerMock.expectNew(URLStreamProvider.class, 20000, 15000, null, null, null).andReturn(streamProviderMock); PowerMock.replay(streamProviderMock, URLStreamProvider.class, uriMock, URI.class, Response.class, responseBuilderMock); replay(getUriInfo(), urlConnectionMock, getHttpHeaders()); Response resultForErrorRequest = ps.processGetRequestForwarding(getHttpHeaders(),getUriInfo()); assertSame(resultForErrorRequest, 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; }