1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.context.request.async;
18
19
20 import javax.servlet.AsyncEvent;
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import org.springframework.mock.web.test.MockAsyncContext;
26 import org.springframework.mock.web.test.MockHttpServletRequest;
27 import org.springframework.mock.web.test.MockHttpServletResponse;
28
29 import static org.hamcrest.Matchers.*;
30 import static org.junit.Assert.*;
31 import static org.mockito.BDDMockito.*;
32
33
34
35
36
37
38 public class StandardServletAsyncWebRequestTests {
39
40 private StandardServletAsyncWebRequest asyncRequest;
41
42 private MockHttpServletRequest request;
43
44 private MockHttpServletResponse response;
45
46 @Before
47 public void setup() {
48 this.request = new MockHttpServletRequest();
49 this.request.setAsyncSupported(true);
50 this.response = new MockHttpServletResponse();
51 this.asyncRequest = new StandardServletAsyncWebRequest(this.request, this.response);
52 this.asyncRequest.setTimeout(44*1000L);
53 }
54
55 @Test
56 public void isAsyncStarted() throws Exception {
57 assertFalse(this.asyncRequest.isAsyncStarted());
58
59 this.asyncRequest.startAsync();
60 assertTrue(this.asyncRequest.isAsyncStarted());
61 }
62
63 @Test
64 public void startAsync() throws Exception {
65 this.asyncRequest.startAsync();
66
67 MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
68
69 assertNotNull(asyncContext);
70 assertEquals("Timeout value not set", 44 * 1000, asyncContext.getTimeout());
71 assertEquals(1, asyncContext.getListeners().size());
72 assertSame(this.asyncRequest, asyncContext.getListeners().get(0));
73 }
74
75 @Test
76 public void startAsyncMultipleTimes() throws Exception {
77 this.asyncRequest.startAsync();
78 this.asyncRequest.startAsync();
79 this.asyncRequest.startAsync();
80 this.asyncRequest.startAsync();
81
82 MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
83
84 assertNotNull(asyncContext);
85 assertEquals(1, asyncContext.getListeners().size());
86 }
87
88 @Test
89 public void startAsyncNotSupported() throws Exception {
90 this.request.setAsyncSupported(false);
91 try {
92 this.asyncRequest.startAsync();
93 fail("expected exception");
94 }
95 catch (IllegalStateException ex) {
96 assertThat(ex.getMessage(), containsString("Async support must be enabled"));
97 }
98 }
99
100 @Test
101 public void startAsyncAfterCompleted() throws Exception {
102 this.asyncRequest.onComplete(new AsyncEvent(null));
103 try {
104 this.asyncRequest.startAsync();
105 fail("expected exception");
106 }
107 catch (IllegalStateException ex) {
108 assertEquals("Async processing has already completed", ex.getMessage());
109 }
110 }
111
112 @Test
113 public void onTimeoutDefaultBehavior() throws Exception {
114 this.asyncRequest.onTimeout(new AsyncEvent(null));
115 assertEquals(200, this.response.getStatus());
116 }
117
118 @Test
119 public void onTimeoutTimeoutHandler() throws Exception {
120 Runnable timeoutHandler = mock(Runnable.class);
121
122 this.asyncRequest.addTimeoutHandler(timeoutHandler);
123 this.asyncRequest.onTimeout(new AsyncEvent(null));
124
125 verify(timeoutHandler).run();
126 }
127
128 @Test(expected=IllegalStateException.class)
129 public void setTimeoutDuringConcurrentHandling() {
130 this.asyncRequest.startAsync();
131 this.asyncRequest.setTimeout(25L);
132 }
133
134 }