58
58
import javax .security .auth .x500 .X500Principal ;
59
59
60
60
import jakarta .servlet .http .HttpServletResponse ;
61
+
61
62
import java .io .ByteArrayOutputStream ;
62
63
import java .io .File ;
63
64
import java .io .IOException ;
@@ -345,14 +346,14 @@ public void testSecureServerSupportsCurrentTlsProtocolVersion() throws Exception
345
346
public void testSecureServerTrustStoreConfiguredClientAuthenticationRequired () throws Exception {
346
347
configureProcessorSslContextService (ListenHTTP .ClientAuthentication .REQUIRED );
347
348
final int port = startSecureServer ();
348
- assertThrows (IOException .class , () -> sendMessage (null , true , port , false , HTTP_POST ));
349
+ assertThrows (IOException .class , () -> sendMessage (null , true , port , HTTP_BASE_PATH , false , HTTP_POST ));
349
350
}
350
351
351
352
@ Test
352
353
public void testSecureServerTrustStoreNotConfiguredClientAuthenticationNotRequired () throws Exception {
353
354
configureProcessorSslContextService (ListenHTTP .ClientAuthentication .AUTO );
354
355
final int port = startSecureServer ();
355
- final int responseCode = sendMessage (null , true , port , true , HTTP_POST );
356
+ final int responseCode = sendMessage (null , true , port , HTTP_BASE_PATH , true , HTTP_POST );
356
357
assertEquals (HttpServletResponse .SC_NO_CONTENT , responseCode );
357
358
}
358
359
@@ -464,7 +465,7 @@ public void testPostContentEncodingGzipAccepted() throws IOException {
464
465
465
466
final OkHttpClient okHttpClient = getOkHttpClient (false , false );
466
467
final Request .Builder requestBuilder = new Request .Builder ();
467
- final String url = buildUrl (false , port );
468
+ final String url = buildUrl (false , port , HTTP_BASE_PATH );
468
469
requestBuilder .url (url );
469
470
470
471
final String message = String .class .getSimpleName ();
@@ -496,6 +497,14 @@ public void testOptionsNotAllowed() throws Exception {
496
497
startWebServerAndSendMessages (messages , HttpServletResponse .SC_METHOD_NOT_ALLOWED , false , false , HTTP_OPTIONS );
497
498
}
498
499
500
+ @ Test
501
+ public void testOptionsNotAllowedOnNonBasePath () throws Exception {
502
+ final int port = startWebServer ();
503
+ final int statusCode = sendMessage ("payload 1" , false , port , "randomPath" , false , HTTP_OPTIONS );
504
+
505
+ assertEquals (HttpServletResponse .SC_METHOD_NOT_ALLOWED , statusCode , "HTTP Status Code not matched" );
506
+ }
507
+
499
508
@ Test
500
509
public void testTraceNotAllowed () throws Exception {
501
510
final List <String > messages = new ArrayList <>();
@@ -504,6 +513,14 @@ public void testTraceNotAllowed() throws Exception {
504
513
startWebServerAndSendMessages (messages , HttpServletResponse .SC_METHOD_NOT_ALLOWED , false , false , HTTP_TRACE );
505
514
}
506
515
516
+ @ Test
517
+ public void testTraceNotAllowedOnNonBasePath () throws Exception {
518
+ final int port = startWebServer ();
519
+ final int statusCode = sendMessage ("payload 1" , false , port , "randomPath" , false , HTTP_TRACE );
520
+
521
+ assertEquals (HttpServletResponse .SC_METHOD_NOT_ALLOWED , statusCode , "HTTP Status Code not matched" );
522
+ }
523
+
507
524
private MockRecordParser setupRecordReaderTest () throws InitializationException {
508
525
final MockRecordParser parser = new MockRecordParser ();
509
526
final MockRecordWriter writer = new MockRecordWriter ();
@@ -527,10 +544,10 @@ private int startSecureServer() {
527
544
return startWebServer ();
528
545
}
529
546
530
- private int sendMessage (final String message , final boolean secure , final int port , boolean clientAuthRequired , final String httpMethod ) throws IOException {
547
+ private int sendMessage (final String message , final boolean secure , final int port , final String basePath , boolean clientAuthRequired , final String httpMethod ) throws IOException {
531
548
final byte [] bytes = message == null ? new byte []{} : message .getBytes (StandardCharsets .UTF_8 );
532
549
final RequestBody requestBody = RequestBody .create (bytes , APPLICATION_OCTET_STREAM );
533
- final String url = buildUrl (secure , port );
550
+ final String url = buildUrl (secure , port , basePath );
534
551
final Request .Builder requestBuilder = new Request .Builder ();
535
552
final Request request = requestBuilder .method (httpMethod , requestBody )
536
553
.url (url )
@@ -557,8 +574,8 @@ private OkHttpClient getOkHttpClient(final boolean secure, final boolean clientA
557
574
return builder .build ();
558
575
}
559
576
560
- private String buildUrl (final boolean secure , final int port ) {
561
- return String .format ("%s://localhost:%s/%s" , secure ? "https" : "http" , port , HTTP_BASE_PATH );
577
+ private String buildUrl (final boolean secure , final int port , String basePath ) {
578
+ return String .format ("%s://localhost:%s/%s" , secure ? "https" : "http" , port , basePath );
562
579
}
563
580
564
581
private void testPOSTRequestsReceived (int returnCode , boolean secure , boolean twoWaySsl ) throws Exception {
@@ -623,7 +640,7 @@ private void startWebServerAndSendMessages(final List<String> messages, final in
623
640
final int port = startWebServer ();
624
641
625
642
for (final String message : messages ) {
626
- final int statusCode = sendMessage (message , secure , port , clientAuthRequired , httpMethod );
643
+ final int statusCode = sendMessage (message , secure , port , HTTP_BASE_PATH , clientAuthRequired , httpMethod );
627
644
assertEquals (expectedStatusCode , statusCode , "HTTP Status Code not matched" );
628
645
}
629
646
}
@@ -669,7 +686,7 @@ public void testMultipartFormDataRequest() throws IOException {
669
686
.build ();
670
687
671
688
final Request request = new Request .Builder ()
672
- .url (buildUrl (isSecure , port ))
689
+ .url (buildUrl (isSecure , port , HTTP_BASE_PATH ))
673
690
.post (multipartBody )
674
691
.build ();
675
692
@@ -741,7 +758,7 @@ public void testLargeHTTPRequestHeader() throws Exception {
741
758
742
759
final int port = startWebServer ();
743
760
OkHttpClient client = getOkHttpClient (false , false );
744
- final String url = buildUrl (false , port );
761
+ final String url = buildUrl (false , port , HTTP_BASE_PATH );
745
762
Request request = new Request .Builder ()
746
763
.url (url )
747
764
.addHeader ("Large-Header" , largeHeaderValue )
0 commit comments