Skip to content

Commit

Permalink
fix this flakey test
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Dec 30, 2024
1 parent 200d5b7 commit c55bd2c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions unirest-bdd-tests/src/test/java/BehaviorTests/Http2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions unirest-bdd-tests/src/test/java/BehaviorTests/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -157,4 +160,17 @@ public PasswordAuthentication requestPasswordAuthenticationInstance(String host,
public static <K, V> MultimapAssert<K, V> assertMultiMap(final Multimap<K, V> actual) {
return org.assertj.guava.api.Assertions.assertThat(actual);
}

public static final class Matchers {
public static Consumer<HeaderAsserts.HeaderValue> isBase64Encoded() {
return h -> {
var value = h.getValue();
try {
Base64.getDecoder().decode(value);
}catch (IllegalArgumentException e){
fail("Header was not base64 encoded: " + value);
}
};
}
}
}

0 comments on commit c55bd2c

Please sign in to comment.