11package com .blazemeter .jmeter .http2 .core ;
22
3- import static org .assertj .core .api .Assertions .assertThat ;
4- import static org .junit .Assert .assertThrows ;
5-
63import com .blazemeter .jmeter .http2 .sampler .HTTP2Sampler ;
74import com .blazemeter .jmeter .http2 .sampler .JMeterTestUtils ;
85import com .google .common .base .Stopwatch ;
96import com .google .common .io .Resources ;
107import jakarta .servlet .http .HttpServlet ;
118import jakarta .servlet .http .HttpServletRequest ;
129import jakarta .servlet .http .HttpServletResponse ;
13- import java .io .ByteArrayOutputStream ;
14- import java .io .IOException ;
15- import java .io .InputStream ;
16- import java .net .MalformedURLException ;
17- import java .net .URL ;
18- import java .nio .charset .StandardCharsets ;
19- import java .nio .file .Files ;
20- import java .nio .file .Paths ;
21- import java .util .Arrays ;
22- import java .util .Base64 ;
23- import java .util .Collections ;
24- import java .util .List ;
25- import java .util .concurrent .ExecutionException ;
26- import java .util .concurrent .TimeUnit ;
27- import java .util .concurrent .TimeoutException ;
28- import java .util .stream .Collectors ;
29- import java .util .zip .GZIPOutputStream ;
3010import jodd .net .MimeTypes ;
3111import org .apache .jmeter .config .Arguments ;
3212import org .apache .jmeter .protocol .http .control .AuthManager ;
7555import org .junit .runner .RunWith ;
7656import org .mockito .junit .MockitoJUnitRunner ;
7757
58+ import java .io .ByteArrayOutputStream ;
59+ import java .io .IOException ;
60+ import java .io .InputStream ;
61+ import java .net .MalformedURLException ;
62+ import java .net .URL ;
63+ import java .nio .charset .StandardCharsets ;
64+ import java .nio .file .Files ;
65+ import java .nio .file .Paths ;
66+ import java .util .Arrays ;
67+ import java .util .Base64 ;
68+ import java .util .Collections ;
69+ import java .util .List ;
70+ import java .util .concurrent .ExecutionException ;
71+ import java .util .concurrent .TimeUnit ;
72+ import java .util .concurrent .TimeoutException ;
73+ import java .util .stream .Collectors ;
74+ import java .util .zip .GZIPOutputStream ;
75+
76+ import static org .assertj .core .api .Assertions .assertThat ;
77+ import static org .junit .Assert .assertThrows ;
78+
7879@ RunWith (MockitoJUnitRunner .class )
7980public class HTTP2JettyClientTest {
8081
@@ -91,6 +92,7 @@ public class HTTP2JettyClientTest {
9192 private static final String SERVER_PATH_200_GZIP = "/test/gzip" ;
9293 private static final String SERVER_PATH_200_EMBEDDED = "/test/embedded" ;
9394 private static final String SERVER_PATH_200_FILE_SENT = "/test/file" ;
95+ private static final String SERVER_PATH_BIG_RESPONSE = "/test/big-response" ;
9496 private static final String SERVER_PATH_400 = "/test/400" ;
9597 private static final String SERVER_PATH_302 = "/test/302" ;
9698 private static final String SERVER_PATH_200_WITH_BODY = "/test/body" ;
@@ -104,6 +106,7 @@ public class HTTP2JettyClientTest {
104106 private static final String AUTH_PASSWORD = "password" ;
105107 private static final String AUTH_REALM = "realm" ;
106108 private static final String KEYSTORE_PASSWORD = "storepwd" ;
109+ private static final int BIG_BUFFER_SIZE = 4 * 1024 * 1024 ;
107110
108111 @ Rule
109112 public final JUnitSoftAssertions softly = new JUnitSoftAssertions ();
@@ -239,6 +242,11 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
239242 break ;
240243 case SERVER_PATH_DELETE_DATA :
241244 resp .setStatus (HttpStatus .OK_200 );
245+ break ;
246+ case SERVER_PATH_BIG_RESPONSE :
247+ resp .getOutputStream ().write (new byte [(int ) BIG_BUFFER_SIZE ]);
248+ resp .setContentType ("image/jpg" );
249+ break ;
242250 }
243251 }
244252 };
@@ -293,12 +301,12 @@ public void shouldSendBodyInformationWhenRequestWithBodyRaw() throws Exception {
293301 }
294302
295303 private HTTPSampleResult buildOkResult (String requestBody , String requestContentType ) {
296- return buildResult (true , HttpStatus . Code .OK , null ,
304+ return buildResult (true , Code .OK , null ,
297305 requestBody != null ? requestBody .getBytes (StandardCharsets .UTF_8 ) : null ,
298306 requestContentType );
299307 }
300308
301- private HTTPSampleResult buildResult (boolean successful , HttpStatus . Code statusCode ,
309+ private HTTPSampleResult buildResult (boolean successful , Code statusCode ,
302310 HttpFields headers , byte [] requestBody , String requestContentType ) {
303311
304312 Mutable httpFields = HttpFields .build ()
@@ -861,7 +869,7 @@ public void shouldNotUseMultipartWhenHasOneFileWithEmptyParamName() throws Excep
861869 buildStartedServer ();
862870 URL file = getClass ().getResource ("blazemeter-labs-logo.png" );
863871 sampler .setHTTPFiles (new HTTPFileArg []{new HTTPFileArg (file .getPath (), "" , "image/png" )});
864- HTTPSampleResult expected = buildResult (true , HttpStatus . Code .OK , null ,
872+ HTTPSampleResult expected = buildResult (true , Code .OK , null ,
865873 Resources .toByteArray (file ), "image/png" );
866874 validateResponse (sample (SERVER_PATH_200_FILE_SENT , HTTPConstants .POST ), expected );
867875 }
@@ -899,4 +907,35 @@ public void shouldGetSuccessResponseWhenServerRequiresClientCertAndOneIsConfigur
899907 }
900908 }
901909
910+ @ Test
911+ public void shouldGetResponseWhenBufferSizeIsSmallerOrTheSameAsMaxBufferSize () throws Exception {
912+ buildStartedServer ();
913+ JMeterUtils .setProperty ("httpJettyClient.maxBufferSize" , String .valueOf (BIG_BUFFER_SIZE ));
914+ HTTPSampleResult result = sampleWithGet (SERVER_PATH_BIG_RESPONSE );
915+ //Since no text response was set, we validate the size of the response body instead.
916+ assertThat (result .getBodySizeAsLong ()).isEqualTo (BIG_BUFFER_SIZE );
917+ }
918+
919+ @ Test (expected = IllegalArgumentException .class )
920+ public void shouldThrowAnExceptionWhenBufferSizeIsBiggerThanMaxBufferSize () throws Exception {
921+ buildStartedServer ();
922+ JMeterUtils .setProperty ("httpJettyClient.maxBufferSize" , String .valueOf (BIG_BUFFER_SIZE - 1 ));
923+ sampleWithGet (SERVER_PATH_BIG_RESPONSE );
924+ }
925+
926+ @ Test
927+ public void shouldNotGetAResponseWhenBufferSizeIsBiggerThanMaxBufferSize () throws Exception {
928+ buildStartedServer ();
929+ JMeterUtils .setProperty ("httpJettyClient.maxBufferSize" , String .valueOf (BIG_BUFFER_SIZE - 1 ));
930+ //There is no response, since an exception is thrown in this case
931+
932+ Exception exception = assertThrows (IllegalArgumentException .class ,
933+ () -> sampleWithGet (SERVER_PATH_BIG_RESPONSE ));
934+
935+ assertThat (exception .toString ()).contains (
936+ "java.lang.IllegalArgumentException: Buffer capacity 4194303 exceeded."
937+ + " To modify buffer size, set the property httpJettyClient.maxBufferSize"
938+ + " in the jmeter.properties file" );
939+ }
940+
902941}
0 commit comments