1717
1818
1919import com .akamai .edgegrid .signer .ClientCredential ;
20+ import com .akamai .edgegrid .signer .exceptions .RequestSigningException ;
2021import com .github .tomakehurst .wiremock .WireMockServer ;
2122import 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 ;
2232import org .testng .annotations .AfterClass ;
2333import org .testng .annotations .BeforeClass ;
2434import org .testng .annotations .BeforeMethod ;
3343
3444import static com .github .tomakehurst .wiremock .client .WireMock .*;
3545import 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 );
0 commit comments