Skip to content

Commit

Permalink
Fix setBasicAuth method to actually set header instead of parameter (#27
Browse files Browse the repository at this point in the history
)

- Bugfix auth set as parameter now set in headers
  • Loading branch information
Christoffer-Cortes authored and Anders Breid committed Oct 15, 2019
1 parent 128ee7c commit b8eccb8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
26 changes: 23 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.github.ericsson</groupId>
Expand All @@ -14,40 +16,58 @@
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-reflect</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public HttpRequest setBasicAuth(String username, String password)
throws UnsupportedEncodingException {
String auth = String.format("%s:%s", username, password);
String encodedAuth = new String(Base64.encodeBase64(auth.getBytes()), "UTF-8");
params.put(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth);
addHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,46 @@

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import com.ericsson.eiffelcommons.utils.HttpRequest;
import com.ericsson.eiffelcommons.utils.HttpRequest.HttpMethod;

public class HttpRequestTest {
private static final String EXPECTED_URI = "http://something.com/testing/test/";
private static final String EXPECTED_HEADER = "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=";

@Test
public void testBuildingOfURI()
throws NoSuchFieldException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
URISyntaxException, ClientProtocolException, IOException {
String expectedURI = "http://something.com/testing/test/";
String expectedAuthParam = "Authorization=Basic dXNlcm5hbWU6cGFzc3dvcmQ=";
public void testBuildingOfURI() throws Exception {

HttpRequest request = new HttpRequest(HttpMethod.POST);
Method createURIBuilder = HttpRequest.class.getDeclaredMethod("createURIBuilder");
Method addParametersToURIBuilder = HttpRequest.class.getDeclaredMethod(
"addParametersToURIBuilder", URIBuilder.class);
createURIBuilder.setAccessible(true);
addParametersToURIBuilder.setAccessible(true);

request.setBaseUrl("http://something.com");
request.setEndpoint("/testing/test/");
URIBuilder builder = (URIBuilder) createURIBuilder.invoke(request);
assertEquals(expectedURI, builder.toString());
URIBuilder builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());

request.setBaseUrl("http://something.com/");
request.setEndpoint("/testing/test/");
builder = (URIBuilder) createURIBuilder.invoke(request);
assertEquals(expectedURI, builder.toString());
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());

request.setBaseUrl("http://something.com/");
request.setEndpoint("testing/test/");
builder = (URIBuilder) createURIBuilder.invoke(request);
assertEquals(expectedURI, builder.toString());
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());

request.setBaseUrl("http://something.com");
request.setEndpoint("testing/test/");
builder = (URIBuilder) createURIBuilder.invoke(request);
assertEquals(expectedURI, builder.toString());
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());

request.setBasicAuth("username", "password");
builder = (URIBuilder) createURIBuilder.invoke(request);
builder = (URIBuilder) addParametersToURIBuilder.invoke(request, builder);
String actualAuthParam = builder.getQueryParams().get(0).toString();
assertEquals(expectedAuthParam, actualAuthParam);
HttpRequestBase client = Whitebox.getInternalState(request, "request");
String actualAuthHeader = client.getAllHeaders()[0].toString();
assertEquals(EXPECTED_HEADER, actualAuthHeader);
}
}

0 comments on commit b8eccb8

Please sign in to comment.