Skip to content

Commit

Permalink
Adding request headers support
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishnan83 committed Oct 2, 2013
1 parent 4b1ccda commit 31b720c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/org/rest/rapa/HttpMethodExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.commons.httpclient.methods.*;

import java.io.IOException;
import java.util.Map;

public class HttpMethodExecutor {

Expand All @@ -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<String, String> requestHeaders) throws IOException {
return executeGet(url, httpMethodProvider.getMethod(requestHeaders), HttpStatus.SC_OK);
}

String post(String content, String url, String contentType)
Expand Down
15 changes: 13 additions & 2 deletions src/org/rest/rapa/HttpMethodProvider.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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() {
Expand Down
13 changes: 11 additions & 2 deletions src/org/rest/rapa/RestClient.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -67,9 +70,15 @@ public void delete(Resource resource) throws RestClientException {

public Resource getById(int id, Class<? extends Resource> type)
throws RestClientException {
return getById(id, type, new HashMap<String, String>());
}

public Resource getById(int id, Class<? extends Resource> type,
Map<String, String> 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);
}
Expand Down
5 changes: 5 additions & 0 deletions test/org/rest/rapa/AbstractHttpMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +16,7 @@ public abstract class AbstractHttpMethodTest {
protected FormatHandler formatHandler;
protected HttpMethodExecutor httpMethodExecutor;
protected RestClient client;
protected Map<String, String> emptyRequestHeaders;

@Before
public void before() {
Expand All @@ -21,5 +25,6 @@ public void before() {
formatHandler = mock(FormatHandler.class);
httpMethodExecutor = mock(HttpMethodExecutor.class);
client = new RestClient(resourceUrl, formatHandler, httpMethodExecutor);
emptyRequestHeaders = new HashMap<String, String>();
}
}
10 changes: 5 additions & 5 deletions test/org/rest/rapa/GetResourceRestClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<test>1</test>");
when(formatHandler.deserialize("<test>1</test>", ResourceImpl.class))
.thenReturn(resource);
Expand All @@ -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(
"<test>1</test>");
client.getById(1, ResourceImpl.class);
verify(formatHandler).deserialize("<test>1</test>", ResourceImpl.class);
Expand All @@ -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(
"<test>1</test>");
doThrow(new Exception()).when(formatHandler).deserialize(
"<test>1</test>", ResourceImpl.class);
Expand All @@ -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);
}
}
19 changes: 11 additions & 8 deletions test/org/rest/rapa/HttpMethodExcecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -25,6 +27,7 @@ public class HttpMethodExcecutorTest {
private HttpMethodProvider mockHttpMethodProvider;
private Ehcache mockCache;
private CacheManager mockCacheManager;
private Map<String, String> emptyRequestHeaders = new HashMap<String, String> ();

@Before
public void setup() {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -109,7 +112,7 @@ public void shouldThrowAnExceptionWhenHTTPStatusIsNotOK()
HttpMethodExecutor httpMethodExecutor = new HttpMethodExecutor(
mockHttpClientAdaptor, mockHttpMethodProvider, mockCache,
mockCacheManager);
httpMethodExecutor.get(URL);
httpMethodExecutor.get(URL, emptyRequestHeaders);
}

@Test
Expand Down
23 changes: 22 additions & 1 deletion test/org/rest/rapa/HttpMethodProviderTest.java
Original file line number Diff line number Diff line change
@@ -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<String, String> emptyRequestHeaders = new HashMap<String, String>();

@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<String, String> requestHeaders = new HashMap<String, String>();
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
Expand Down

0 comments on commit 31b720c

Please sign in to comment.