adding setAuthorizationHeader method

Signed-off-by: Stéphane Tournié <stephane.tournie@serli.com>
6.19.x
Stéphane Tournié 2016-02-24 08:49:51 +01:00
parent 1082978419
commit 7a1df9b677
3 changed files with 36 additions and 8 deletions

View File

@ -48,6 +48,7 @@ import java.util.List;
import java.util.Map;
import static java.util.Objects.requireNonNull;
import static com.google.common.base.Strings.isNullOrEmpty;
/**
* Simple implementation of {@link HttpJsonRequest} based on {@link HttpURLConnection}.
@ -71,6 +72,7 @@ public class DefaultHttpJsonRequest implements HttpJsonRequest {
private String method;
private Object body;
private List<Pair<String, ?>> queryParams;
private String authorizationHeaderValue;
DefaultHttpJsonRequest(String url) {
this.url = requireNonNull(url, "Required non-null url");
@ -115,7 +117,14 @@ public class DefaultHttpJsonRequest implements HttpJsonRequest {
queryParams.add(Pair.of(name, value));
return this;
}
@Override
public HttpJsonRequest setAuthorizationHeader(@NotNull String value) {
requireNonNull(value, "Required non-null header value");
authorizationHeaderValue = value;
return this;
}
@Override
public HttpJsonRequest setTimeout(int timeout) {
this.timeout = timeout;
@ -133,7 +142,7 @@ public class DefaultHttpJsonRequest implements HttpJsonRequest {
if (method == null) {
throw new IllegalStateException("Could not perform request, request method wasn't set");
}
return doRequest(timeout, url, method, body, queryParams);
return doRequest(timeout, url, method, body, queryParams, authorizationHeaderValue);
}
/**
@ -153,6 +162,8 @@ public class DefaultHttpJsonRequest implements HttpJsonRequest {
* request body, must be instance of {@link JsonSerializable}
* @param parameters
* query parameters, may be null
* @param authorizationHeaderValue
* value of authorization header, may be null
* @return response to this request
* @throws IOException
* when connection content type is not "application/json"
@ -173,7 +184,8 @@ public class DefaultHttpJsonRequest implements HttpJsonRequest {
String url,
String method,
Object body,
List<Pair<String, ?>> parameters) throws IOException,
List<Pair<String, ?>> parameters,
String authorizationHeaderValue) throws IOException,
ServerException,
ForbiddenException,
NotFoundException,
@ -205,7 +217,9 @@ public class DefaultHttpJsonRequest implements HttpJsonRequest {
conn.setRequestMethod(method);
//drop a hint for server side that we want to receive application/json
conn.addRequestProperty(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
if (authToken != null) {
if (!isNullOrEmpty(authorizationHeaderValue)) {
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, authorizationHeaderValue);
} else if (authToken != null) {
conn.setRequestProperty(HttpHeaders.AUTHORIZATION, authToken);
}
if (body != null) {

View File

@ -121,6 +121,17 @@ public interface HttpJsonRequest {
*/
HttpJsonRequest addQueryParam(@NotNull String name, @NotNull Object value);
/**
* Adds authorization header to the request.
*
* @param value
* authorization header value
* @return this request instance
* @throws NullPointerException
* when value is null
*/
HttpJsonRequest setAuthorizationHeader(@NotNull String value);
/**
* Sets request timeout in milliseconds.
*

View File

@ -97,11 +97,11 @@ public class DefaultHttpJsonRequestTest {
public void shouldUseUrlAndMethodFromTheLinks() throws Exception {
final Link link = createLink("POST", DEFAULT_URL, "rel");
final DefaultHttpJsonRequest request = spy(new DefaultHttpJsonRequest(link));
doReturn(new DefaultHttpJsonResponse("", 200)).when(request).doRequest(anyInt(), anyString(), anyString(), anyObject(), any());
doReturn(new DefaultHttpJsonResponse("", 200)).when(request).doRequest(anyInt(), anyString(), anyString(), anyObject(), any(), anyString());
request.request();
verify(request).doRequest(0, DEFAULT_URL, "POST", null, null);
verify(request).doRequest(0, DEFAULT_URL, "POST", null, null, null);
}
@Test
@ -119,7 +119,8 @@ public class DefaultHttpJsonRequestTest {
"http://localhost:8080",
"PUT",
body,
asList(Pair.of("name", "value"), Pair.of("name2", "value2")));
asList(Pair.of("name", "value"), Pair.of("name2", "value2")),
null);
}
@Test
@ -136,6 +137,7 @@ public class DefaultHttpJsonRequestTest {
eq("http://localhost:8080"),
eq("POST"),
mapCaptor.capture(),
eq(null),
eq(null));
assertTrue(mapCaptor.getValue() instanceof JsonStringMap);
assertEquals(mapCaptor.getValue(), body);
@ -153,6 +155,7 @@ public class DefaultHttpJsonRequestTest {
eq("http://localhost:8080"),
eq("POST"),
listCaptor.capture(),
eq(null),
eq(null));
assertTrue(listCaptor.getValue() instanceof JsonArray);
assertEquals(listCaptor.getValue(), body);
@ -320,6 +323,6 @@ public class DefaultHttpJsonRequestTest {
}
private void prepareResponse(String response) throws Exception {
doReturn(new DefaultHttpJsonResponse(response, 200)).when(request).doRequest(anyInt(), anyString(), anyString(), anyObject(), any());
doReturn(new DefaultHttpJsonResponse(response, 200)).when(request).doRequest(anyInt(), anyString(), anyString(), anyObject(), any(), anyString());
}
}