Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hasContentType matcher, HTTP status consts
This matcher removes a lot of boilerplate from checking the Content-Type of a HttpResponse. It's based on the Hamcrest API with the help of these tutorials: - https://hamcrest.org/JavaHamcrest/tutorial - https://www.baeldung.com/hamcrest-custom-matchers This could be generalized to handle any header. However, I want to keep it straightforward for now, as a good basic example of how to write a custom matcher. Also took the opportunity to replace bare HTTP status codes (200, et. al.) with constants defined on jakarta.servlet.http.HttpServletResponse.
- Loading branch information
Showing
3 changed files
with
69 additions
and
10 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
strcalc/src/test/java/com/mike_bland/training/testing/matchers/HasContentType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.mike_bland.training.testing.matchers; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.TypeSafeMatcher; | ||
|
||
import java.net.http.HttpResponse; | ||
|
||
// Custom Hamcrest matcher validating the Content-Type header of a HttpResponse. | ||
// | ||
// This could be generalized to check any HTTP header, or collection thereof. | ||
// However, since this is a teaching example, we'll keep it straightforward. | ||
class HasContentType<T> extends TypeSafeMatcher<HttpResponse<T>> { | ||
private final String expected; | ||
|
||
// Constructor to register the expected Content-Type value. | ||
HasContentType(String contentType) { | ||
this.expected = contentType; | ||
} | ||
|
||
// Helper method to extract the Content-Type value from an HttpResponse. | ||
private String getContentType(HttpResponse<T> resp) { | ||
return resp.headers().firstValue("Content-Type").orElse(""); | ||
} | ||
|
||
// Performs the actual assertion. | ||
@Override public boolean matchesSafely(HttpResponse<T> resp) { | ||
return getContentType(resp).equals(expected); | ||
} | ||
|
||
// Describes the "Expected:" value in assertion failure messages. | ||
@Override public void describeTo(Description description) { | ||
description.appendText(expected); | ||
} | ||
|
||
// Describes the actual ("but:") value in assertion failure messages. | ||
@Override public void describeMismatchSafely( | ||
HttpResponse<T> resp, Description description) { | ||
description.appendText(getContentType(resp)); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
strcalc/src/test/java/com/mike_bland/training/testing/matchers/Matchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.mike_bland.training.testing.matchers; | ||
|
||
import org.hamcrest.Matcher; | ||
|
||
import java.net.http.HttpResponse; | ||
|
||
// Collection of custom Hamcrest Matcher<T> classes for assertThat() statements. | ||
// | ||
// These tutorials describe how to write Matchers: | ||
// | ||
// - https://hamcrest.org/JavaHamcrest/tutorial | ||
// - https://www.baeldung.com/hamcrest-custom-matchers | ||
// | ||
// Note that these tutorials show the static factory functions defined on the | ||
// same class as the Matcher. However, Hamcrest itself collects these factories | ||
// into its own org.hamcrest.Matchers class for convenience, instead of | ||
// importing one class per Matcher. | ||
public class Matchers { | ||
public static <T> Matcher<HttpResponse<T>> hasContentType( | ||
String contentType) { | ||
return new HasContentType<>(contentType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters