From 31b720ccea598c9716667307911c3c51e400874b Mon Sep 17 00:00:00 2001 From: HariKrishnan Date: Wed, 2 Oct 2013 19:09:39 +0530 Subject: [PATCH] Adding request headers support --- build.gradle | 2 +- src/org/rest/rapa/HttpMethodExecutor.java | 5 ++-- src/org/rest/rapa/HttpMethodProvider.java | 15 ++++++++++-- src/org/rest/rapa/RestClient.java | 13 +++++++++-- .../org/rest/rapa/AbstractHttpMethodTest.java | 5 ++++ .../rest/rapa/GetResourceRestClientTest.java | 10 ++++---- .../rest/rapa/HttpMethodExcecutorTest.java | 19 ++++++++------- .../org/rest/rapa/HttpMethodProviderTest.java | 23 ++++++++++++++++++- 8 files changed, 71 insertions(+), 21 deletions(-) diff --git a/build.gradle b/build.gradle index e545a6e..35af69a 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ install.dependsOn ':build' defaultTasks 'clean', 'install' sourceCompatibility = 1.6 -version = '0.9.1' +version = '0.9.2' group = 'org.rest.rapa' repositories { diff --git a/src/org/rest/rapa/HttpMethodExecutor.java b/src/org/rest/rapa/HttpMethodExecutor.java index f9fce76..3f27422 100644 --- a/src/org/rest/rapa/HttpMethodExecutor.java +++ b/src/org/rest/rapa/HttpMethodExecutor.java @@ -11,6 +11,7 @@ import org.apache.commons.httpclient.methods.*; import java.io.IOException; +import java.util.Map; public class HttpMethodExecutor { @@ -31,8 +32,8 @@ public HttpMethodExecutor(HttpClientAdapter httpClientAdapter, this.cacheManager = cacheManager; } - public String get(String url) throws IOException { - return executeGet(url, httpMethodProvider.getMethod(), HttpStatus.SC_OK); + public String get(String url, Map requestHeaders) throws IOException { + return executeGet(url, httpMethodProvider.getMethod(requestHeaders), HttpStatus.SC_OK); } String post(String content, String url, String contentType) diff --git a/src/org/rest/rapa/HttpMethodProvider.java b/src/org/rest/rapa/HttpMethodProvider.java index 71071b9..555c918 100644 --- a/src/org/rest/rapa/HttpMethodProvider.java +++ b/src/org/rest/rapa/HttpMethodProvider.java @@ -1,11 +1,22 @@ package org.rest.rapa; +import java.util.Map; + +import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.methods.*; public class HttpMethodProvider { - public GetMethod getMethod() { - return new GetMethod(); + public GetMethod getMethod(Map requestHeaders) { + GetMethod getMethod = new GetMethod(); + if (!requestHeaders.isEmpty()) { + for (String headerName : requestHeaders.keySet()) { + Header header = new Header(headerName, + requestHeaders.get(headerName)); + getMethod.addRequestHeader(header); + } + } + return getMethod; } public PostMethod postMethod() { diff --git a/src/org/rest/rapa/RestClient.java b/src/org/rest/rapa/RestClient.java index 76455c6..62ec37c 100644 --- a/src/org/rest/rapa/RestClient.java +++ b/src/org/rest/rapa/RestClient.java @@ -1,5 +1,8 @@ package org.rest.rapa; +import java.util.HashMap; +import java.util.Map; + import org.apache.commons.logging.*; import org.rest.rapa.formatter.FormatHandler; import org.rest.rapa.resource.Resource; @@ -67,9 +70,15 @@ public void delete(Resource resource) throws RestClientException { public Resource getById(int id, Class type) throws RestClientException { + return getById(id, type, new HashMap()); + } + + public Resource getById(int id, Class type, + Map requestHeaders) throws RestClientException { try { - return formatHandler.deserialize(httpMethodExecutor.get(resourceUrl - .getResourceSpecificURL(id)), type); + return formatHandler.deserialize(httpMethodExecutor.get( + resourceUrl.getResourceSpecificURL(id), requestHeaders), + type); } catch (Exception e) { throw new RestClientException("Error while getting resource", e); } diff --git a/test/org/rest/rapa/AbstractHttpMethodTest.java b/test/org/rest/rapa/AbstractHttpMethodTest.java index 177d123..d7d5bae 100644 --- a/test/org/rest/rapa/AbstractHttpMethodTest.java +++ b/test/org/rest/rapa/AbstractHttpMethodTest.java @@ -2,6 +2,9 @@ import static org.mockito.Mockito.mock; +import java.util.HashMap; +import java.util.Map; + import org.junit.Before; import org.rest.rapa.formatter.FormatHandler; import org.rest.rapa.resource.Resource; @@ -13,6 +16,7 @@ public abstract class AbstractHttpMethodTest { protected FormatHandler formatHandler; protected HttpMethodExecutor httpMethodExecutor; protected RestClient client; + protected Map emptyRequestHeaders; @Before public void before() { @@ -21,5 +25,6 @@ public void before() { formatHandler = mock(FormatHandler.class); httpMethodExecutor = mock(HttpMethodExecutor.class); client = new RestClient(resourceUrl, formatHandler, httpMethodExecutor); + emptyRequestHeaders = new HashMap(); } } diff --git a/test/org/rest/rapa/GetResourceRestClientTest.java b/test/org/rest/rapa/GetResourceRestClientTest.java index 8c746ba..eec98b7 100644 --- a/test/org/rest/rapa/GetResourceRestClientTest.java +++ b/test/org/rest/rapa/GetResourceRestClientTest.java @@ -18,7 +18,7 @@ public void before() { @Test public void shouldGetRequestedResource() throws Exception { - when(httpMethodExecutor.get("http://test.com/1")).thenReturn( + when(httpMethodExecutor.get("http://test.com/1", emptyRequestHeaders)).thenReturn( "1"); when(formatHandler.deserialize("1", ResourceImpl.class)) .thenReturn(resource); @@ -28,12 +28,12 @@ public void shouldGetRequestedResource() throws Exception { @Test public void shouldHttpGetResource() throws Exception { client.getById(1, ResourceImpl.class); - verify(httpMethodExecutor).get("http://test.com/1"); + verify(httpMethodExecutor).get("http://test.com/1", emptyRequestHeaders); } @Test public void shouldDeserializeResource() throws Exception { - when(httpMethodExecutor.get("http://test.com/1")).thenReturn( + when(httpMethodExecutor.get("http://test.com/1", emptyRequestHeaders)).thenReturn( "1"); client.getById(1, ResourceImpl.class); verify(formatHandler).deserialize("1", ResourceImpl.class); @@ -42,7 +42,7 @@ public void shouldDeserializeResource() throws Exception { @Test(expected = RestClientException.class) public void shouldFailToGetResourceIfUnableToDeSerializeTheResource() throws Exception { - when(httpMethodExecutor.get("http://test.com/1")).thenReturn( + when(httpMethodExecutor.get("http://test.com/1", emptyRequestHeaders)).thenReturn( "1"); doThrow(new Exception()).when(formatHandler).deserialize( "1", ResourceImpl.class); @@ -53,7 +53,7 @@ public void shouldFailToGetResourceIfUnableToDeSerializeTheResource() public void shouldFailToGetIfUnableToHttpGetRemoteResource() throws Exception { doThrow(new IOException()).when(httpMethodExecutor).get( - "http://test.com/1"); + "http://test.com/1", emptyRequestHeaders); client.getById(1, ResourceImpl.class); } } diff --git a/test/org/rest/rapa/HttpMethodExcecutorTest.java b/test/org/rest/rapa/HttpMethodExcecutorTest.java index 13afc13..6817933 100644 --- a/test/org/rest/rapa/HttpMethodExcecutorTest.java +++ b/test/org/rest/rapa/HttpMethodExcecutorTest.java @@ -16,6 +16,8 @@ import static org.mockito.Mockito.*; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; public class HttpMethodExcecutorTest { private static final String CONTENT = "content"; @@ -25,6 +27,7 @@ public class HttpMethodExcecutorTest { private HttpMethodProvider mockHttpMethodProvider; private Ehcache mockCache; private CacheManager mockCacheManager; + private Map emptyRequestHeaders = new HashMap (); @Before public void setup() { @@ -40,7 +43,7 @@ public void shouldRetrieveElementFromCacheIfPresent() throws IOException { HttpMethodExecutor httpMethodExecutor = new HttpMethodExecutor( mockHttpClientAdaptor, mockHttpMethodProvider, mockCache, mockCacheManager); - httpMethodExecutor.get(URL); + httpMethodExecutor.get(URL, emptyRequestHeaders); } @Test @@ -50,13 +53,13 @@ public void shouldExecuteGetMethodIfResourceNotCached() throws IOException { .thenReturn(HttpStatus.SC_OK); String cacheControlHeaderValue = "max-age=0, public"; - when(mockHttpMethodProvider.getMethod()).thenReturn( + when(mockHttpMethodProvider.getMethod(emptyRequestHeaders)).thenReturn( createGetMethod(cacheControlHeaderValue)); HttpMethodExecutor httpMethodExecutor = new HttpMethodExecutor( mockHttpClientAdaptor, mockHttpMethodProvider, mockCache, mockCacheManager); - httpMethodExecutor.get(URL); + httpMethodExecutor.get(URL, emptyRequestHeaders); } @Test @@ -68,14 +71,14 @@ public void shouldCacheResourceIfMaxAgeIsGreaterThanZero() .thenReturn(HttpStatus.SC_OK); String cacheControlHeaderValue = "max-age=1, public"; - when(mockHttpMethodProvider.getMethod()).thenReturn( + when(mockHttpMethodProvider.getMethod(emptyRequestHeaders)).thenReturn( createGetMethod(cacheControlHeaderValue)); HttpMethodExecutor httpMethodExecutor = new HttpMethodExecutor( mockHttpClientAdaptor, mockHttpMethodProvider, mockCache, mockCacheManager); - httpMethodExecutor.get(URL); + httpMethodExecutor.get(URL, emptyRequestHeaders); verify(mockCache).put(any(Element.class)); } @@ -89,14 +92,14 @@ public void shouldNotCacheResourceIfMaxAgeIsLessThanOne() .thenReturn(HttpStatus.SC_OK); String cacheControlHeaderValue = "max-age=0, public"; - when(mockHttpMethodProvider.getMethod()).thenReturn( + when(mockHttpMethodProvider.getMethod(emptyRequestHeaders)).thenReturn( createGetMethod(cacheControlHeaderValue)); HttpMethodExecutor httpMethodExecutor = new HttpMethodExecutor( mockHttpClientAdaptor, mockHttpMethodProvider, mockCache, mockCacheManager); - httpMethodExecutor.get(URL); + httpMethodExecutor.get(URL, emptyRequestHeaders); verify(mockCache, never()).put(any(Element.class)); } @@ -109,7 +112,7 @@ public void shouldThrowAnExceptionWhenHTTPStatusIsNotOK() HttpMethodExecutor httpMethodExecutor = new HttpMethodExecutor( mockHttpClientAdaptor, mockHttpMethodProvider, mockCache, mockCacheManager); - httpMethodExecutor.get(URL); + httpMethodExecutor.get(URL, emptyRequestHeaders); } @Test diff --git a/test/org/rest/rapa/HttpMethodProviderTest.java b/test/org/rest/rapa/HttpMethodProviderTest.java index 6dd217a..af26862 100644 --- a/test/org/rest/rapa/HttpMethodProviderTest.java +++ b/test/org/rest/rapa/HttpMethodProviderTest.java @@ -1,14 +1,35 @@ package org.rest.rapa; import static org.junit.Assert.*; + +import java.util.HashMap; +import java.util.Map; + import org.junit.Test; +import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.methods.*; public class HttpMethodProviderTest { + private Map emptyRequestHeaders = new HashMap(); + @Test public void shouldProvideGetMethod() { HttpMethodProvider provider = new HttpMethodProvider(); - assertEquals(GetMethod.class, provider.getMethod().getClass()); + assertEquals(GetMethod.class, provider.getMethod(emptyRequestHeaders).getClass()); + } + + @Test + public void shouldAddRequestHeadersAndProvideGetMethod() { + HttpMethodProvider provider = new HttpMethodProvider(); + Map requestHeaders = new HashMap(); + requestHeaders.put("headerName", "headerValue"); + GetMethod getMethod = provider.getMethod(requestHeaders); + assertEquals(GetMethod.class, getMethod.getClass()); + Header[] requestHeaders2 = getMethod.getRequestHeaders(); + assertEquals(1, requestHeaders2.length); + Header header = requestHeaders2[0]; + assertEquals("headerName", header.getName()); + assertEquals("headerValue", header.getValue()); } @Test