30
30
import java .util .Locale ;
31
31
import java .util .Map ;
32
32
33
+ import jakarta .servlet .AsyncContext ;
34
+ import jakarta .servlet .AsyncEvent ;
35
+ import jakarta .servlet .AsyncListener ;
33
36
import jakarta .servlet .http .Cookie ;
34
37
import org .junit .jupiter .api .Test ;
35
38
@@ -663,6 +666,44 @@ void httpHeaderFormattedDateError() {
663
666
request .getDateHeader (HttpHeaders .IF_MODIFIED_SINCE ));
664
667
}
665
668
669
+ @ Test
670
+ void shouldRejectAsyncStartsIfUnsupported () {
671
+ assertThat (request .isAsyncStarted ()).isFalse ();
672
+ assertThatIllegalStateException ().isThrownBy (request ::startAsync );
673
+ }
674
+
675
+ @ Test
676
+ void startAsyncShouldUpdateRequestState () {
677
+ assertThat (request .isAsyncStarted ()).isFalse ();
678
+ request .setAsyncSupported (true );
679
+ AsyncContext asyncContext = request .startAsync ();
680
+ assertThat (request .isAsyncStarted ()).isTrue ();
681
+ }
682
+
683
+ @ Test
684
+ void shouldNotifyAsyncListeners () {
685
+ request .setAsyncSupported (true );
686
+ AsyncContext asyncContext = request .startAsync ();
687
+ TestAsyncListener testAsyncListener = new TestAsyncListener ();
688
+ asyncContext .addListener (testAsyncListener );
689
+ asyncContext .complete ();
690
+ assertThat (testAsyncListener .events ).hasSize (1 );
691
+ assertThat (testAsyncListener .events .get (0 )).extracting ("name" ).isEqualTo ("onComplete" );
692
+ }
693
+
694
+ @ Test
695
+ void shouldNotifyAsyncListenersWhenNewAsyncStarted () {
696
+ request .setAsyncSupported (true );
697
+ AsyncContext asyncContext = request .startAsync ();
698
+ TestAsyncListener testAsyncListener = new TestAsyncListener ();
699
+ asyncContext .addListener (testAsyncListener );
700
+ AsyncContext newAsyncContext = request .startAsync ();
701
+ assertThat (testAsyncListener .events ).hasSize (1 );
702
+ ListenerEvent listenerEvent = testAsyncListener .events .get (0 );
703
+ assertThat (listenerEvent ).extracting ("name" ).isEqualTo ("onStartAsync" );
704
+ assertThat (listenerEvent .event .getAsyncContext ()).isEqualTo (newAsyncContext );
705
+ }
706
+
666
707
private void assertEqualEnumerations (Enumeration <?> enum1 , Enumeration <?> enum2 ) {
667
708
int count = 0 ;
668
709
while (enum1 .hasMoreElements ()) {
@@ -672,4 +713,31 @@ private void assertEqualEnumerations(Enumeration<?> enum1, Enumeration<?> enum2)
672
713
}
673
714
}
674
715
716
+ static class TestAsyncListener implements AsyncListener {
717
+
718
+ List <ListenerEvent > events = new ArrayList <>();
719
+
720
+ @ Override
721
+ public void onComplete (AsyncEvent asyncEvent ) throws IOException {
722
+ this .events .add (new ListenerEvent ("onComplete" , asyncEvent ));
723
+ }
724
+
725
+ @ Override
726
+ public void onTimeout (AsyncEvent asyncEvent ) throws IOException {
727
+ this .events .add (new ListenerEvent ("onTimeout" , asyncEvent ));
728
+ }
729
+
730
+ @ Override
731
+ public void onError (AsyncEvent asyncEvent ) throws IOException {
732
+ this .events .add (new ListenerEvent ("onError" , asyncEvent ));
733
+ }
734
+
735
+ @ Override
736
+ public void onStartAsync (AsyncEvent asyncEvent ) throws IOException {
737
+ this .events .add (new ListenerEvent ("onStartAsync" , asyncEvent ));
738
+ }
739
+ }
740
+
741
+ record ListenerEvent (String name , AsyncEvent event ) {}
742
+
675
743
}
0 commit comments