Try to fix flakiness of Jetty smoke test

This commit is contained in:
Moritz Halbritter 2023-11-30 10:57:00 +01:00
parent 203cd542c0
commit f509c90c46
3 changed files with 15 additions and 22 deletions

View File

@ -16,7 +16,7 @@
package smoketest.jetty.service;
import smoketest.jetty.util.RandomStringUtil;
import smoketest.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -28,16 +28,12 @@ public class HttpHeaderService {
private int maxHttpResponseHeaderSize;
/**
* Generates random data. The data is:
* <ol>
* <li>is longer than configured
* <code>server.jetty.max-http-response-header-size</code></li>
* <li>is url encoded by base 64 encode the random value</li>
* </ol>
* @return a base64 encoded string of random bytes
* Generates a header value, which is longer than
* 'server.jetty.max-http-response-header-size'.
* @return the header value
*/
public String getHeaderValue() {
return RandomStringUtil.getRandomBase64EncodedString(this.maxHttpResponseHeaderSize + 1);
return StringUtil.repeat('A', this.maxHttpResponseHeaderSize + 1);
}
}

View File

@ -16,18 +16,17 @@
package smoketest.jetty.util;
import java.util.Base64;
import java.util.Random;
import java.util.Arrays;
public final class RandomStringUtil {
public final class StringUtil {
private RandomStringUtil() {
private StringUtil() {
}
public static String getRandomBase64EncodedString(int length) {
byte[] responseHeader = new byte[length];
new Random().nextBytes(responseHeader);
return Base64.getEncoder().encodeToString(responseHeader);
public static String repeat(char c, int length) {
char[] chars = new char[length];
Arrays.fill(chars, c);
return new String(chars);
}
}

View File

@ -21,14 +21,12 @@ import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import smoketest.jetty.util.RandomStringUtil;
import smoketest.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
@ -46,9 +44,9 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Florian Storz
* @author Michael Weidmann
* @author Moritz Halbritter
*/
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ExtendWith(OutputCaptureExtension.class)
class SampleJettyApplicationTests {
@Autowired
@ -85,7 +83,7 @@ class SampleJettyApplicationTests {
@Test
void testMaxHttpRequestHeaderSize() {
String headerValue = RandomStringUtil.getRandomBase64EncodedString(this.maxHttpRequestHeaderSize + 1);
String headerValue = StringUtil.repeat('A', this.maxHttpRequestHeaderSize + 1);
HttpHeaders headers = new HttpHeaders();
headers.add("x-max-request-header", headerValue);
HttpEntity<?> httpEntity = new HttpEntity<>(headers);