@Before public void setUp() throws Exception { fetcher = new BasicHttpFetcher(BASE_URL.getAuthority()); mockInputStream = EasyMock.createMock(InputStream.class); EasyMock.expect(mockInputStream.available()).andReturn(0); mockInputStream.close(); mockEntity = EasyMock.createMock(HttpEntity.class); EasyMock.expect(mockEntity.getContent()).andReturn(mockInputStream); EasyMock.expect(mockEntity.getContentLength()).andReturn(16384L).anyTimes(); }
/** * @param response The response to parse * @return A HttpResponse object made by consuming the response of the * given HttpMethod. * @throws IOException when problems occur processing the body content */ private HttpResponse makeResponse(org.apache.http.HttpResponse response) throws IOException { HttpResponseBuilder builder = new HttpResponseBuilder(); if (response.getAllHeaders() != null) { for (Header h : response.getAllHeaders()) { if (h.getName() != null) builder.addHeader(h.getName(), h.getValue()); } } HttpEntity entity = response.getEntity(); if (maxObjSize > 0 && entity != null && entity.getContentLength() > maxObjSize) { return HttpResponse.badrequest("Exceeded maximum number of bytes - " + maxObjSize); } byte[] responseBytes = (entity == null) ? null : toByteArraySafe(entity); return builder .setHttpStatusCode(response.getStatusLine().getStatusCode()) .setResponse(responseBytes) .create(); }
slowResponseWarning(request, started, now); return makeResponse(response);
String basicHttpFetcherProxy) { setMaxObjectSizeBytes(maxObjSize); setSlowResponseWarning(DEFAULT_SLOW_RESPONSE_WARNING);
@Test public void testHeadWithMaxObjectSizeBytes() throws Exception { fetcher.setMaxObjectSizeBytes(1024 * 1024); Uri uri = new UriBuilder(Uri.parse("http://www.google.com/search")) .addQueryParameter("body", "") .addQueryParameter("status", "200") .toUri(); HttpRequest request = new HttpRequest(uri); request.setMethod("HEAD"); HttpResponse response = fetcher.fetch(request); assertEquals(200, response.getHttpStatusCode()); assertEquals("", response.getResponseAsString()); } }
@Test public void testWithProxy() throws Exception { String content = "Hello, Gagan!"; Uri uri = new UriBuilder(Uri.parse("http://www.google.com/search")) .addQueryParameter("body", content) .addQueryParameter("status", "201") .toUri(); HttpRequest request = new HttpRequest(uri); HttpResponse response = fetcher.fetch(request); assertEquals(201, response.getHttpStatusCode()); assertEquals(content, response.getResponseAsString()); }
String basicHttpFetcherProxy) { setMaxObjectSizeBytes(maxObjSize); setSlowResponseWarning(DEFAULT_SLOW_RESPONSE_WARNING);
@Test public void testHeadWithMaxObjectSizeBytes() throws Exception { fetcher.setMaxObjectSizeBytes(1024 * 1024); Uri uri = new UriBuilder(Uri.parse("http://www.google.com/search")) .addQueryParameter("body", "") .addQueryParameter("status", "200") .toUri(); HttpRequest request = new HttpRequest(uri); request.setMethod("HEAD"); HttpResponse response = fetcher.fetch(request); assertEquals(200, response.getHttpStatusCode()); assertEquals("", response.getResponseAsString()); } }
@Test public void testWithProxy() throws Exception { String content = "Hello, Gagan!"; Uri uri = new UriBuilder(Uri.parse("http://www.google.com/search")) .addQueryParameter("body", content) .addQueryParameter("status", "201") .toUri(); HttpRequest request = new HttpRequest(uri); HttpResponse response = fetcher.fetch(request); assertEquals(201, response.getHttpStatusCode()); assertEquals(content, response.getResponseAsString()); }
/** * @param response The response to parse * @return A HttpResponse object made by consuming the response of the * given HttpMethod. * @throws IOException when problems occur processing the body content */ private HttpResponse makeResponse(org.apache.http.HttpResponse response) throws IOException { HttpResponseBuilder builder = new HttpResponseBuilder(); if (response.getAllHeaders() != null) { for (Header h : response.getAllHeaders()) { if (h.getName() != null) builder.addHeader(h.getName(), h.getValue()); } } HttpEntity entity = response.getEntity(); if (maxObjSize > 0 && entity.getContentLength() > maxObjSize) { return HttpResponse.badrequest("Exceeded maximum number of bytes - " + maxObjSize); } byte[] responseBytes = (entity == null) ? null : toByteArraySafe(entity); return builder .setHttpStatusCode(response.getStatusLine().getStatusCode()) .setResponse(responseBytes) .create(); }
@Before public void setUp() throws Exception { fetcher = new BasicHttpFetcher(BASE_URL.getAuthority()); mockInputStream = EasyMock.createMock(InputStream.class); EasyMock.expect(mockInputStream.available()).andReturn(0); mockInputStream.close(); mockEntity = EasyMock.createMock(HttpEntity.class); EasyMock.expect(mockEntity.getContent()).andReturn(mockInputStream); EasyMock.expect(mockEntity.getContentLength()).andReturn(16384L).anyTimes(); }
String basicHttpFetcherProxy) { setMaxObjectSizeBytes(maxObjSize); setSlowResponseWarning(DEFAULT_SLOW_RESPONSE_WARNING);
slowResponseWarning(request, started, now); return makeResponse(response);
@Test public void testWithProxy() throws Exception { String content = "Hello, Gagan!"; Uri uri = new UriBuilder(Uri.parse("http://www.google.com/search")) .addQueryParameter("body", content) .addQueryParameter("status", "201") .toUri(); HttpRequest request = new HttpRequest(uri); HttpResponse response = fetcher.fetch(request); assertEquals(201, response.getHttpStatusCode()); assertEquals(content, response.getResponseAsString()); }
/** * @param response The response to parse * @return A HttpResponse object made by consuming the response of the * given HttpMethod. * @throws IOException when problems occur processing the body content */ private HttpResponse makeResponse(org.apache.http.HttpResponse response) throws IOException { HttpResponseBuilder builder = new HttpResponseBuilder(); if (response.getAllHeaders() != null) { for (Header h : response.getAllHeaders()) { if (h.getName() != null) builder.addHeader(h.getName(), h.getValue()); } } HttpEntity entity = response.getEntity(); if (maxObjSize > 0 && entity != null && entity.getContentLength() > maxObjSize) { return HttpResponse.badrequest("Exceeded maximum number of bytes - " + maxObjSize); } byte[] responseBytes = (entity == null) ? null : toByteArraySafe(entity); return builder .setHttpStatusCode(response.getStatusLine().getStatusCode()) .setResponse(responseBytes) .create(); }
@Before public void setUp() throws Exception { fetcher = new BasicHttpFetcher(BASE_URL.getAuthority()); mockInputStream = EasyMock.createMock(InputStream.class); EasyMock.expect(mockInputStream.available()).andReturn(0); mockInputStream.close(); mockEntity = EasyMock.createMock(HttpEntity.class); EasyMock.expect(mockEntity.getContent()).andReturn(mockInputStream); EasyMock.expect(mockEntity.getContentLength()).andReturn(16384L).anyTimes(); }
slowResponseWarning(request, started, now); return makeResponse(response);
@Test public void testToByteArraySafeHandlesExceptionWithNoMessage() throws Exception { EOFException e = new EOFException(); EasyMock.expect(mockInputStream.read(EasyMock.isA(byte[].class))).andThrow(e).anyTimes(); EasyMock.replay(mockEntity, mockInputStream); try { fetcher.toByteArraySafe(mockEntity); } catch (EOFException eofe) { fail("Exception Should have been caught"); } EasyMock.verify(mockEntity, mockInputStream); }
@Before public void setUp() throws Exception { svcLookup = new DefaultRpcServiceLookup(new DefaultServiceFetcher(null, new BasicHttpFetcher(null)), 60l); socialEndpoint = "http://localhost:8080/social/rpc"; host = "localhost:8080"; }
@Test public void testToByteArraySafeHandlesExceptionWithNoMessage() throws Exception { EOFException e = new EOFException(); EasyMock.expect(mockInputStream.read(EasyMock.isA(byte[].class))).andThrow(e).anyTimes(); EasyMock.replay(mockEntity, mockInputStream); try { fetcher.toByteArraySafe(mockEntity); } catch (EOFException eofe) { fail("Exception Should have been caught"); } EasyMock.verify(mockEntity, mockInputStream); }