From adaa8213a04512ebf21689c3704ca98820688399 Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Sat, 3 Sep 2022 22:26:01 +0400 Subject: [PATCH] feat: Add Predicates to match header value and presence. --- .../io/github/nstdio/http/ext/Predicates.java | 29 ++++++++++++++++ .../github/nstdio/http/ext/PredicatesTest.kt | 34 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/main/java/io/github/nstdio/http/ext/Predicates.java b/src/main/java/io/github/nstdio/http/ext/Predicates.java index 7820063..4c3c92b 100644 --- a/src/main/java/io/github/nstdio/http/ext/Predicates.java +++ b/src/main/java/io/github/nstdio/http/ext/Predicates.java @@ -18,6 +18,7 @@ import java.net.URI; import java.net.http.HttpRequest; +import java.net.http.HttpResponse; import java.util.Objects; import java.util.function.Predicate; @@ -45,4 +46,32 @@ public static Predicate uri(URI uri) { Objects.requireNonNull(uri); return r -> r.uri().equals(uri); } + + /** + * The {@code Predicate} that matches only {@code HttpResponse} with given header. + * + * @param name The header name. + * @param value The header value. + * + * @return The {@code Predicate} that matches {@code HttpResponse} with given header. + */ + public static Predicate> hasHeader(String name, String value) { + Objects.requireNonNull(name); + Objects.requireNonNull(value); + + return r -> r.headers().firstValue(name).filter(value::equals).isPresent(); + } + + /** + * The {@code Predicate} that matches only {@code HttpRequest} with given header. + * + * @param name The header name. + * + * @return The {@code Predicate} that matches {@code HttpRequest} with given header. + */ + public static Predicate> hasHeader(String name) { + Objects.requireNonNull(name); + + return r -> r.headers().firstValue(name).isPresent(); + } } diff --git a/src/test/kotlin/io/github/nstdio/http/ext/PredicatesTest.kt b/src/test/kotlin/io/github/nstdio/http/ext/PredicatesTest.kt index e79d46c..32ea47f 100644 --- a/src/test/kotlin/io/github/nstdio/http/ext/PredicatesTest.kt +++ b/src/test/kotlin/io/github/nstdio/http/ext/PredicatesTest.kt @@ -34,4 +34,38 @@ class PredicatesTest { .accepts(r1) .rejects(r2) } + + @Test + fun `Should accept or reject with header name and value`() { + //given + val r = StaticHttpResponse.builder() + .statusCode(200) + .headers(HttpHeadersBuilder().add("Content-Type", "text/plain").build()) + .build() + + //when + then + assertThat(Predicates.hasHeader("Content-Type", "text/plain")) + .accepts(r) + + assertThat(Predicates.hasHeader("Content-Type", "text/plain;charset=UTF-8")) + .rejects(r) + assertThat(Predicates.hasHeader("Content-Length", "12")) + .rejects(r) + } + + @Test + fun `Should accept or reject with header name`() { + //given + val r = StaticHttpResponse.builder() + .statusCode(200) + .headers(HttpHeadersBuilder().add("Content-Type", "*/*").build()) + .build() + + //when + then + assertThat(Predicates.hasHeader("Content-Type")) + .accepts(r) + + assertThat(Predicates.hasHeader("Content-Length")) + .rejects(r) + } } \ No newline at end of file