View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket.sockjs.transport.handler;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  
22  import org.springframework.http.MediaType;
23  import org.springframework.web.socket.AbstractHttpRequestTests;
24  import org.springframework.web.socket.TextMessage;
25  import org.springframework.web.socket.WebSocketHandler;
26  import org.springframework.web.socket.sockjs.SockJsMessageDeliveryException;
27  import org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession;
28  import org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig;
29  import org.springframework.web.socket.sockjs.transport.session.TestHttpSockJsSession;
30  
31  import static org.junit.Assert.*;
32  import static org.mockito.BDDMockito.*;
33  
34  /**
35   * Test fixture for {@link AbstractHttpReceivingTransportHandler} and sub-classes
36   * {@link XhrReceivingTransportHandler} and {@link JsonpReceivingTransportHandler}.
37   *
38   * @author Rossen Stoyanchev
39   */
40  public class HttpReceivingTransportHandlerTests  extends AbstractHttpRequestTests {
41  
42  
43  	@Override
44  	@Before
45  	public void setUp() {
46  		super.setUp();
47  	}
48  
49  	@Test
50  	public void readMessagesXhr() throws Exception {
51  		this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));
52  		handleRequest(new XhrReceivingTransportHandler());
53  
54  		assertEquals(204, this.servletResponse.getStatus());
55  	}
56  
57  	@Test
58  	public void readMessagesJsonp() throws Exception {
59  		this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));
60  		handleRequest(new JsonpReceivingTransportHandler());
61  
62  		assertEquals(200, this.servletResponse.getStatus());
63  		assertEquals("ok", this.servletResponse.getContentAsString());
64  	}
65  
66  	@Test
67  	public void readMessagesJsonpFormEncoded() throws Exception {
68  		this.servletRequest.setContent("d=[\"x\"]".getBytes("UTF-8"));
69  		this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
70  		handleRequest(new JsonpReceivingTransportHandler());
71  
72  		assertEquals(200, this.servletResponse.getStatus());
73  		assertEquals("ok", this.servletResponse.getContentAsString());
74  	}
75  
76  	// SPR-10621
77  
78  	@Test
79  	public void readMessagesJsonpFormEncodedWithEncoding() throws Exception {
80  		this.servletRequest.setContent("d=[\"x\"]".getBytes("UTF-8"));
81  		this.servletRequest.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
82  		handleRequest(new JsonpReceivingTransportHandler());
83  
84  		assertEquals(200, this.servletResponse.getStatus());
85  		assertEquals("ok", this.servletResponse.getContentAsString());
86  	}
87  
88  	@Test
89  	public void readMessagesBadContent() throws Exception {
90  		this.servletRequest.setContent("".getBytes("UTF-8"));
91  		handleRequestAndExpectFailure();
92  
93  		this.servletRequest.setContent("[\"x]".getBytes("UTF-8"));
94  		handleRequestAndExpectFailure();
95  	}
96  
97  	@Test(expected=IllegalArgumentException.class)
98  	public void readMessagesNoSession() throws Exception {
99  		WebSocketHandler webSocketHandler = mock(WebSocketHandler.class);
100 		new XhrReceivingTransportHandler().handleRequest(this.request, this.response, webSocketHandler, null);
101 	}
102 
103 	@Test
104 	public void delegateMessageException() throws Exception {
105 
106 		StubSockJsServiceConfig sockJsConfig = new StubSockJsServiceConfig();
107 
108 		this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));
109 
110 		WebSocketHandler wsHandler = mock(WebSocketHandler.class);
111 		TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null);
112 		session.delegateConnectionEstablished();
113 
114 		willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x"));
115 
116 		try {
117 			XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler();
118 			transportHandler.initialize(sockJsConfig);
119 			transportHandler.handleRequest(this.request, this.response, wsHandler, session);
120 			fail("Expected exception");
121 		}
122 		catch (SockJsMessageDeliveryException ex) {
123 			assertNull(session.getCloseStatus());
124 		}
125 	}
126 
127 
128 	private void handleRequest(AbstractHttpReceivingTransportHandler transportHandler) throws Exception {
129 
130 		WebSocketHandler wsHandler = mock(WebSocketHandler.class);
131 		AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);
132 
133 		transportHandler.initialize(new StubSockJsServiceConfig());
134 		transportHandler.handleRequest(this.request, this.response, wsHandler, session);
135 
136 		assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getContentType().toString());
137 		verify(wsHandler).handleMessage(session, new TextMessage("x"));
138 	}
139 
140 	private void handleRequestAndExpectFailure() throws Exception {
141 
142 		resetResponse();
143 
144 		WebSocketHandler wsHandler = mock(WebSocketHandler.class);
145 		AbstractSockJsSession session = new TestHttpSockJsSession("1", new StubSockJsServiceConfig(), wsHandler, null);
146 
147 		new XhrReceivingTransportHandler().handleRequest(this.request, this.response, wsHandler, session);
148 
149 		assertEquals(500, this.servletResponse.getStatus());
150 		verifyNoMoreInteractions(wsHandler);
151 	}
152 
153 }