Skip to content
Snippets Groups Projects

feat: Add header validation support to testing package

4 files
+ 139
24
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -55,6 +55,7 @@ public final class EndpointTestBuilder {
@@ -55,6 +55,7 @@ public final class EndpointTestBuilder {
private boolean checkSchema;
private boolean checkSchema;
private boolean checkFormat;
private boolean checkFormat;
private boolean checkBodyParams;
private boolean checkBodyParams;
 
private boolean checkHeaderParams;
/**
/**
* Generate the test base from the definition with generic test defaults.
* Generate the test base from the definition with generic test defaults.
@@ -68,6 +69,7 @@ public final class EndpointTestBuilder {
@@ -68,6 +69,7 @@ public final class EndpointTestBuilder {
this.checkSchema = false;
this.checkSchema = false;
this.checkFormat = false;
this.checkFormat = false;
this.checkBodyParams = false;
this.checkBodyParams = false;
 
this.checkHeaderParams = false;
}
}
/**
/**
@@ -194,6 +196,16 @@ public final class EndpointTestBuilder {
@@ -194,6 +196,16 @@ public final class EndpointTestBuilder {
return this;
return this;
}
}
 
/**
 
* Set the test to perform the header field/value validation on run.
 
*
 
* @return the current test builder instance for chaining.
 
*/
 
public EndpointTestBuilder andCheckHeaderParams() {
 
this.checkHeaderParams = true;
 
return this;
 
}
 
// Handle appending a body to the request
// Handle appending a body to the request
private EndpointTestBuilder withBody(Object body) {
private EndpointTestBuilder withBody(Object body) {
this.body = body;
this.body = body;
@@ -213,6 +225,27 @@ public final class EndpointTestBuilder {
@@ -213,6 +225,27 @@ public final class EndpointTestBuilder {
for (Entry<String, Object> entry : params.entrySet()) {
for (Entry<String, Object> entry : params.entrySet()) {
response = response.body(entry.getKey(), is(entry.getValue()));
response = response.body(entry.getKey(), is(entry.getValue()));
}
}
 
} else {
 
throw new AssertionError("checkBodyParams was set, but no validation params were provided");
 
}
 
return response;
 
}
 
 
/**
 
* Asserts that the given ValidatableResponse headers contains the desired key/value pairs. Throwing a {@link AssertionError} on a
 
* failed assertion. If all is matching, returns the processed ValidatableResponse for further processing.
 
*
 
* @param response The response to process
 
* @return The processed ValidatableResponse object
 
*/
 
private ValidatableResponse assertHeaderParams(ValidatableResponse response) {
 
Map<String, Object> params = testDef.getHeaderValidationParams();
 
if (params != null) {
 
for (Entry<String, Object> entry : params.entrySet()) {
 
response = response.header(entry.getKey(), is(entry.getValue()));
 
}
 
} else {
 
throw new AssertionError("checkHeaderParams was set, but no validation params were provided");
}
}
return response;
return response;
}
}
@@ -240,6 +273,11 @@ public final class EndpointTestBuilder {
@@ -240,6 +273,11 @@ public final class EndpointTestBuilder {
response = assertResponseBody(response);
response = assertResponseBody(response);
}
}
 
// check header values when set
 
if (checkHeaderParams) {
 
response = assertHeaderParams(response);
 
}
 
// check schema when set
// check schema when set
if (checkSchema) {
if (checkSchema) {
response = response.assertThat().body(matchesJsonSchemaInClasspath(testDef.getSchemaLocation()));
response = response.assertThat().body(matchesJsonSchemaInClasspath(testDef.getSchemaLocation()));
@@ -295,9 +333,16 @@ public final class EndpointTestBuilder {
@@ -295,9 +333,16 @@ public final class EndpointTestBuilder {
return checkBodyParams;
return checkBodyParams;
}
}
 
/**
 
* @return the checkHeaderParams
 
*/
 
boolean isCheckHeaderParams() {
 
return checkHeaderParams;
 
}
 
@Override
@Override
public int hashCode() {
public int hashCode() {
return Objects.hash(body, checkFormat, checkSchema, methodName, testDef);
return Objects.hash(body, checkFormat, checkSchema, checkBodyParams, checkHeaderParams, methodName, testDef);
}
}
@Override
@Override
@@ -313,6 +358,7 @@ public final class EndpointTestBuilder {
@@ -313,6 +358,7 @@ public final class EndpointTestBuilder {
}
}
EndpointTestBuilder other = (EndpointTestBuilder) obj;
EndpointTestBuilder other = (EndpointTestBuilder) obj;
return Objects.equals(body, other.body) && checkFormat == other.checkFormat && checkSchema == other.checkSchema
return Objects.equals(body, other.body) && checkFormat == other.checkFormat && checkSchema == other.checkSchema
 
&& checkBodyParams == other.checkBodyParams && checkHeaderParams == other.checkHeaderParams
&& Objects.equals(methodName, other.methodName) && Objects.equals(testDef, other.testDef);
&& Objects.equals(methodName, other.methodName) && Objects.equals(testDef, other.testDef);
}
}
}
}
Loading