Skip to content

Commit 1beb6b9

Browse files
authored
Merge pull request #15 from akamai-open/maciej-fixes-for-rc2
Maciej fixes for rc2
2 parents c40724a + 03f3305 commit 1beb6b9

File tree

7 files changed

+124
-26
lines changed

7 files changed

+124
-26
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# EdgeGrid Client for Java
22

3-
Java implementation of Akamai {OPEN} EdgeGrid signing in Java.
3+
Java implementation of Akamai {OPEN} EdgeGrid signing.
44

55
## Description
66

@@ -36,8 +36,8 @@ build one with its internal builder:
3636
```java
3737
Request request = Request.builder()
3838
.method("POST")
39-
.uriWithQuery(URI.create("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/billing-usage/v1/reportSources"))
40-
.body("{ \"field\": \"field value\" }")
39+
.uri("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net/billing-usage/v1/reportSources")
40+
.body("{ \"field\": \"field value\" }".getBytes())
4141
.header("X-Some-Signed-Header", "header value")
4242
.header("X-Some-Other-Signed-Header", "header value 2")
4343
.build();
@@ -105,7 +105,6 @@ Sign your REST-assured request specification with a defined client credential:
105105

106106
```java
107107
given()
108-
.baseUri("https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net")
109108
.filter(new RestAssuredEdgeGridFilter(clientCredential))
110109
.when()
111110
.get("/billing-usage/v1/reportSources")

edgegrid-signer-core/src/test/resources/logback-test.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,8 @@
77
</encoder>
88
</appender>
99

10-
<!-- Use "org.apache.http.headers" to log only HTTP headers -->
11-
<logger name="org.apache.http.wire" level="DEBUG">
12-
<appender-ref ref="STDOUT"/>
13-
</logger>
14-
15-
16-
<logger name="org.apache.http.headers" level="NONE">
17-
18-
</logger>
19-
2010
<root level="DEBUG">
2111
<appender-ref ref="STDOUT"/>
2212
</root>
2313

24-
2514
</configuration>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+
<encoder>
6+
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<!-- Use "org.apache.http.headers" to log only HTTP headers -->
11+
<logger name="org.apache.http.wire" level="DEBUG">
12+
<appender-ref ref="STDOUT"/>
13+
</logger>
14+
15+
16+
<logger name="org.apache.http.headers" level="INFO">
17+
<appender-ref ref="STDOUT"/>
18+
</logger>
19+
20+
21+
<logger name="com.github.tomakehurst.wiremock" level="TRACE">
22+
<appender-ref ref="STDOUT"/>
23+
</logger>
24+
25+
<logger name="com.akamai" level="TRACE">
26+
<appender-ref ref="STDOUT"/>
27+
</logger>
28+
29+
30+
</configuration>

edgegrid-signer-rest-assured/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<groupId>org.testng</groupId>
3636
<artifactId>testng</artifactId>
3737
</dependency>
38+
<!-- Underlying Apache HTTP Client library uses JCL. -->
39+
<dependency>
40+
<groupId>org.slf4j</groupId>
41+
<artifactId>jcl-over-slf4j</artifactId>
42+
</dependency>
3843
</dependencies>
3944

4045
<build>

edgegrid-signer-rest-assured/src/main/java/com/akamai/edgegrid/signer/restassured/RestAssuredEdgeGridRequestSigner.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ private static String getRequestPath(FilterableRequestSpecification requestSpec)
4949
try {
5050
Field f = requestSpec.getClass().getDeclaredField("path");
5151
f.setAccessible(true);
52-
return (String) f.get(requestSpec);
52+
String requestPath = (String) f.get(requestSpec);
53+
// remove path placeholder parameter brackets
54+
requestPath = requestPath.replaceAll("[{}]*", "");
55+
return requestPath;
5356
} catch (NoSuchFieldException | IllegalAccessException e) {
5457
throw new RuntimeException(e); // should never occur
5558
}
@@ -124,6 +127,9 @@ protected void setHost(FilterableRequestSpecification requestSpec, String host)
124127
// Due to limitations of REST-assured design only requests with relative paths can be updated
125128
Validate.isTrue(isRelativeUrl(getRequestPath(requestSpec)), "path in request cannot be absolute");
126129

130+
// Avoid redundant Host headers
131+
requestSpec.removeHeader("Host");
132+
127133
requestSpec
128134
.baseUri("https://" + host)
129135
.header("Host", host);

edgegrid-signer-rest-assured/src/test/java/com/akamai/edgegrid/signer/restassured/RestAssuredEdgeGridFilterTest.java

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@
1717

1818

1919
import com.akamai.edgegrid.signer.ClientCredential;
20+
import com.akamai.edgegrid.signer.exceptions.RequestSigningException;
2021
import com.github.tomakehurst.wiremock.WireMockServer;
2122
import io.restassured.RestAssured;
23+
import io.restassured.filter.Filter;
24+
import io.restassured.filter.FilterContext;
25+
import io.restassured.response.Response;
26+
import io.restassured.specification.FilterableRequestSpecification;
27+
import io.restassured.specification.FilterableResponseSpecification;
28+
import io.restassured.specification.RequestSpecification;
29+
30+
import org.hamcrest.CoreMatchers;
31+
import org.hamcrest.MatcherAssert;
2232
import org.testng.annotations.AfterClass;
2333
import org.testng.annotations.BeforeClass;
2434
import org.testng.annotations.BeforeMethod;
@@ -33,6 +43,7 @@
3343

3444
import static com.github.tomakehurst.wiremock.client.WireMock.*;
3545
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
46+
import static org.hamcrest.MatcherAssert.assertThat;
3647

3748

3849
/**
@@ -82,11 +93,49 @@ public void signEachRequest() throws URISyntaxException, IOException {
8293
RestAssured.given()
8394
.relaxedHTTPSValidation()
8495
.filter(new RestAssuredEdgeGridFilter(credential))
85-
.baseUri("https://ignored-hostname.com")
8696
.get("/billing-usage/v1/reportSources")
8797
.then().statusCode(200);
8898
}
8999

100+
@Test
101+
public void signEachRequestWithPathParams() throws URISyntaxException, IOException {
102+
103+
wireMockServer.stubFor(get(urlPathMatching("/config-gtm/v1/domains/.*"))
104+
.withHeader("Authorization", matching(".*"))
105+
.withHeader("Host", equalTo(SERVICE_MOCK))
106+
.willReturn(aResponse()
107+
.withStatus(200)
108+
.withHeader("Content-Type", "text/xml")
109+
.withBody("<response>Some content</response>")));
110+
111+
RestAssured.given()
112+
.relaxedHTTPSValidation()
113+
.filter(new RestAssuredEdgeGridFilter(credential))
114+
.get("/config-gtm/v1/domains/{domain}", "storage1.akadns.net")
115+
.then().statusCode(200);
116+
}
117+
118+
@Test
119+
public void signEachRequestWithPathParamsAndQueryString() throws URISyntaxException,
120+
IOException {
121+
122+
wireMockServer.stubFor(get(urlPathMatching("/config-gtm/v1/domains/.*"))
123+
.withHeader("Authorization", matching(".*"))
124+
.withHeader("Host", equalTo(SERVICE_MOCK))
125+
.withQueryParam("param1", equalTo("value1"))
126+
.willReturn(aResponse()
127+
.withStatus(200)
128+
.withHeader("Content-Type", "text/xml")
129+
.withBody("<response>Some content</response>")));
130+
131+
RestAssured.given()
132+
.relaxedHTTPSValidation()
133+
.filter(new RestAssuredEdgeGridFilter(credential))
134+
.queryParam("param1", "value1")
135+
.get("/config-gtm/v1/domains/{domain}", "storage1.akadns.net")
136+
.then().statusCode(200);
137+
}
138+
90139
@Test
91140
public void signWithHostHeader() throws URISyntaxException, IOException {
92141

@@ -102,11 +151,35 @@ public void signWithHostHeader() throws URISyntaxException, IOException {
102151
.relaxedHTTPSValidation()
103152
.filter(new RestAssuredEdgeGridFilter(credential))
104153
.header("Host", "ignored-hostname.com")
105-
.baseUri("https://ignored-hostname.com")
106154
.get("/billing-usage/v1/reportSources")
107155
.then().statusCode(200);
108156
}
109157

158+
@Test
159+
public void replacesProvidedHostHeader() throws URISyntaxException, IOException,
160+
RequestSigningException {
161+
162+
163+
RestAssured.given()
164+
.relaxedHTTPSValidation()
165+
.header("Host", "ignored-hostname.com")
166+
.filter(new RestAssuredEdgeGridFilter(credential))
167+
.filter(new Filter() {
168+
@Override
169+
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
170+
MatcherAssert.assertThat(requestSpec.getHeaders().getList("Host").size(),
171+
CoreMatchers.equalTo(1));
172+
MatcherAssert.assertThat(requestSpec.getHeaders().get("Host").getValue(),
173+
CoreMatchers.equalTo(credential.getHost()));
174+
175+
return ctx.next(requestSpec, responseSpec);
176+
}
177+
})
178+
.get();
179+
180+
181+
}
182+
110183
@Test(expectedExceptions = IllegalArgumentException.class)
111184
public void dontSignEachRequestWithAbsolutePath() throws URISyntaxException, IOException {
112185

@@ -121,7 +194,6 @@ public void dontSignRequestWithFileContent() throws URISyntaxException, IOExcept
121194

122195
RestAssured.given()
123196
.filter(new RestAssuredEdgeGridFilter(credential))
124-
.baseUri("https://ignored-hostname.com")
125197
.body(new File("/home/johan/some_large_file.bin"))
126198
.post("/billing-usage/v1/reportSources")
127199
.then().statusCode(200);
@@ -132,7 +204,6 @@ public void dontSignRequestWithInputStreamContent() throws URISyntaxException, I
132204

133205
RestAssured.given()
134206
.filter(new RestAssuredEdgeGridFilter(credential))
135-
.baseUri("https://ignored-hostname.com")
136207
.body(new ByteArrayInputStream("exampleString".getBytes(StandardCharsets.UTF_8)))
137208
.post("/billing-usage/v1/reportSources")
138209
.then().statusCode(200);
@@ -143,7 +214,6 @@ public void dontSignRequestWithMultipartContent() throws URISyntaxException, IOE
143214

144215
RestAssured.given()
145216
.filter(new RestAssuredEdgeGridFilter(credential))
146-
.baseUri("https://ignored-hostname.com")
147217
.multiPart("file", new File("/home/johan/some_large_file.bin"))
148218
.post("/billing-usage/v1/reportSources")
149219
.then().statusCode(200);

edgegrid-signer-rest-assured/src/test/resources/logback-test.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@
1313
</logger>
1414

1515

16-
<logger name="org.apache.http.headers" level="NONE">
17-
16+
<logger name="org.apache.http.headers" level="INFO">
17+
<appender-ref ref="STDOUT"/>
1818
</logger>
1919

2020

2121
<logger name="com.github.tomakehurst.wiremock" level="TRACE">
2222
<appender-ref ref="STDOUT"/>
2323
</logger>
2424

25-
<root level="DEBUG">
25+
<logger name="com.akamai" level="TRACE">
2626
<appender-ref ref="STDOUT"/>
27-
</root>
28-
27+
</logger>
2928

3029
</configuration>

0 commit comments

Comments
 (0)