From c55bd2c961faef610704fba6782b646acecfbbca Mon Sep 17 00:00:00 2001 From: Ryan Bergman Date: Mon, 30 Dec 2024 09:10:30 -0600 Subject: [PATCH] fix this flakey test --- .../test/java/BehaviorTests/HeaderAsserts.java | 9 +++++++-- .../src/test/java/BehaviorTests/Http2Test.java | 4 ++-- .../src/test/java/BehaviorTests/TestUtil.java | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/unirest-bdd-tests/src/test/java/BehaviorTests/HeaderAsserts.java b/unirest-bdd-tests/src/test/java/BehaviorTests/HeaderAsserts.java index a3aec7b6..55701ab9 100644 --- a/unirest-bdd-tests/src/test/java/BehaviorTests/HeaderAsserts.java +++ b/unirest-bdd-tests/src/test/java/BehaviorTests/HeaderAsserts.java @@ -36,6 +36,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.function.Consumer; import static org.assertj.core.api.Assertions.assertThat; @@ -60,13 +61,17 @@ public void assertNoHeader(String s) { assertFalse(headers.containsKey(s), "Should Have No Header " + s); } - public HeaderAsserts assertHeader(String key, String... value) { + public HeaderAsserts assertContainsHeader(String key){ TestUtil.assertMultiMap(headers).containsKeys(key); + return this; + } + + public HeaderAsserts assertHeader(String key, String... value) { + assertContainsHeader(key); assertThat(headers.get(key)) .extracting(HeaderValue::getValue) .contains(value); return this; - } public HeaderAsserts assertBasicAuth(String username, String password) { diff --git a/unirest-bdd-tests/src/test/java/BehaviorTests/Http2Test.java b/unirest-bdd-tests/src/test/java/BehaviorTests/Http2Test.java index 5576f2fc..f11a14fa 100644 --- a/unirest-bdd-tests/src/test/java/BehaviorTests/Http2Test.java +++ b/unirest-bdd-tests/src/test/java/BehaviorTests/Http2Test.java @@ -56,7 +56,7 @@ void sendsHeadersForHttp2() { .getBody() .assertHeader("Connection", "Upgrade, HTTP2-Settings") .assertHeader("Upgrade", "h2c") - .assertHeader("HTTP2-Settings", "AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA"); + .assertHeader("HTTP2-Settings", TestUtil.Matchers.isBase64Encoded()); } @Test @@ -81,7 +81,7 @@ void overrideConfigPerRequest() { .getBody() .assertHeader("Connection", "Upgrade, HTTP2-Settings") .assertHeader("Upgrade", "h2c") - .assertHeader("HTTP2-Settings", "AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA"); + .assertHeader("HTTP2-Settings", TestUtil.Matchers.isBase64Encoded()); } @Test diff --git a/unirest-bdd-tests/src/test/java/BehaviorTests/TestUtil.java b/unirest-bdd-tests/src/test/java/BehaviorTests/TestUtil.java index ad3f3612..6d0197bc 100644 --- a/unirest-bdd-tests/src/test/java/BehaviorTests/TestUtil.java +++ b/unirest-bdd-tests/src/test/java/BehaviorTests/TestUtil.java @@ -43,9 +43,12 @@ import java.net.http.HttpClient; import java.net.http.HttpResponse; import java.security.KeyStore; +import java.util.Base64; import java.util.HashMap; import java.util.Map; +import java.util.function.Consumer; +import static org.assertj.core.api.Fail.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -157,4 +160,17 @@ public PasswordAuthentication requestPasswordAuthenticationInstance(String host, public static MultimapAssert assertMultiMap(final Multimap actual) { return org.assertj.guava.api.Assertions.assertThat(actual); } + + public static final class Matchers { + public static Consumer isBase64Encoded() { + return h -> { + var value = h.getValue(); + try { + Base64.getDecoder().decode(value); + }catch (IllegalArgumentException e){ + fail("Header was not base64 encoded: " + value); + } + }; + } + } }