SockJsServiceTests.java 6.7 KB
Newer Older
R
Rossen Stoyanchev 已提交
1
/*
2
 * Copyright 2002-2014 the original author or authors.
R
Rossen Stoyanchev 已提交
3 4 5 6 7
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
S
Sam Brannen 已提交
8
 *      http://www.apache.org/licenses/LICENSE-2.0
R
Rossen Stoyanchev 已提交
9 10 11 12 13 14 15 16
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.web.socket.sockjs.support;
R
Rossen Stoyanchev 已提交
18 19 20 21 22 23 24 25 26 27 28 29

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.AbstractHttpRequestTests;
import org.springframework.web.socket.WebSocketHandler;
30
import org.springframework.web.socket.sockjs.SockJsException;
R
Rossen Stoyanchev 已提交
31 32 33 34

import static org.junit.Assert.*;

/**
35 36
 * Test fixture for {@link AbstractSockJsService}.
 *
R
Rossen Stoyanchev 已提交
37 38
 * @author Rossen Stoyanchev
 */
S
Sam Brannen 已提交
39
public class SockJsServiceTests extends AbstractHttpRequestTests {
R
Rossen Stoyanchev 已提交
40 41 42 43 44 45

	private TestSockJsService service;

	private WebSocketHandler handler;


46
	@Override
R
Rossen Stoyanchev 已提交
47 48 49 50 51 52
	@Before
	public void setUp() {
		super.setUp();
		this.service = new TestSockJsService(new ThreadPoolTaskScheduler());
	}

53 54
	@Test
	public void validateRequest() throws Exception {
R
Rossen Stoyanchev 已提交
55

56
		this.service.setWebSocketEnabled(false);
57 58
		handleRequest("GET", "/echo/server/session/websocket", HttpStatus.NOT_FOUND);

59
		this.service.setWebSocketEnabled(true);
60 61 62 63 64 65 66 67 68 69
		handleRequest("GET", "/echo/server/session/websocket", HttpStatus.OK);

		handleRequest("GET", "/echo//", HttpStatus.NOT_FOUND);
		handleRequest("GET", "/echo///", HttpStatus.NOT_FOUND);
		handleRequest("GET", "/echo/other", HttpStatus.NOT_FOUND);
		handleRequest("GET", "/echo//service/websocket", HttpStatus.NOT_FOUND);
		handleRequest("GET", "/echo/server//websocket", HttpStatus.NOT_FOUND);
		handleRequest("GET", "/echo/server/session/", HttpStatus.NOT_FOUND);
		handleRequest("GET", "/echo/s.erver/session/websocket", HttpStatus.NOT_FOUND);
		handleRequest("GET", "/echo/server/s.ession/websocket", HttpStatus.NOT_FOUND);
R
Rossen Stoyanchev 已提交
70 71
	}

72 73 74
	@Test
	public void handleInfoGet() throws Exception {

75
		handleRequest("GET", "/echo/info", HttpStatus.OK);
76 77 78 79 80 81 82 83

		assertEquals("application/json;charset=UTF-8", this.servletResponse.getContentType());
		assertEquals("*", this.servletResponse.getHeader("Access-Control-Allow-Origin"));
		assertEquals("true", this.servletResponse.getHeader("Access-Control-Allow-Credentials"));
		assertEquals("no-store, no-cache, must-revalidate, max-age=0", this.servletResponse.getHeader("Cache-Control"));

		String body = this.servletResponse.getContentAsString();
		assertEquals("{\"entropy\"", body.substring(0, body.indexOf(':')));
84
		assertEquals(",\"origins\":[\"*:*\"],\"cookie_needed\":true,\"websocket\":true}",
85 86
				body.substring(body.indexOf(',')));

87
		this.service.setSessionCookieNeeded(false);
88
		this.service.setWebSocketEnabled(false);
89
		handleRequest("GET", "/echo/info", HttpStatus.OK);
90 91 92 93 94

		body = this.servletResponse.getContentAsString();
		assertEquals(",\"origins\":[\"*:*\"],\"cookie_needed\":false,\"websocket\":false}",
				body.substring(body.indexOf(',')));
	}
R
Rossen Stoyanchev 已提交
95 96

	@Test
97 98 99 100
	public void handleInfoOptions() throws Exception {

		this.servletRequest.addHeader("Access-Control-Request-Headers", "Last-Modified");

101
		handleRequest("OPTIONS", "/echo/info", HttpStatus.NO_CONTENT);
102
		this.response.flush();
103 104 105 106 107 108

		assertEquals("*", this.servletResponse.getHeader("Access-Control-Allow-Origin"));
		assertEquals("true", this.servletResponse.getHeader("Access-Control-Allow-Credentials"));
		assertEquals("Last-Modified", this.servletResponse.getHeader("Access-Control-Allow-Headers"));
		assertEquals("OPTIONS, GET", this.servletResponse.getHeader("Access-Control-Allow-Methods"));
		assertEquals("31536000", this.servletResponse.getHeader("Access-Control-Max-Age"));
R
Rossen Stoyanchev 已提交
109 110 111
	}

	@Test
112 113
	public void handleIframeRequest() throws Exception {

114
		handleRequest("GET", "/echo/iframe.html", HttpStatus.OK);
115 116 117 118 119

		assertEquals("text/html;charset=UTF-8", this.servletResponse.getContentType());
		assertTrue(this.servletResponse.getContentAsString().startsWith("<!DOCTYPE html>\n"));
		assertEquals(496, this.servletResponse.getContentLength());
		assertEquals("public, max-age=31536000", this.response.getHeaders().getCacheControl());
P
Phillip Webb 已提交
120
		assertEquals("\"0da1ed070012f304e47b83c81c48ad620\"", this.response.getHeaders().getETag());
R
Rossen Stoyanchev 已提交
121 122 123
	}

	@Test
124 125 126 127
	public void handleIframeRequestNotModified() throws Exception {

		this.servletRequest.addHeader("If-None-Match", "\"0da1ed070012f304e47b83c81c48ad620\"");

128
		handleRequest("GET", "/echo/iframe.html", HttpStatus.NOT_MODIFIED);
R
Rossen Stoyanchev 已提交
129 130 131
	}

	@Test
132
	public void handleRawWebSocketRequest() throws Exception {
R
Rossen Stoyanchev 已提交
133

134
		handleRequest("GET", "/echo", HttpStatus.OK);
135 136
		assertEquals("Welcome to SockJS!\n", this.servletResponse.getContentAsString());

137
		handleRequest("GET", "/echo/websocket", HttpStatus.OK);
138 139
		assertNull("Raw WebSocket should not open a SockJS session", this.service.sessionId);
		assertSame(this.handler, this.service.handler);
R
Rossen Stoyanchev 已提交
140 141 142
	}


143
	private void handleRequest(String httpMethod, String uri, HttpStatus httpStatus) throws IOException {
R
Rossen Stoyanchev 已提交
144
		resetResponse();
145
		setRequest(httpMethod, uri);
146 147
		String sockJsPath = uri.substring("/echo".length());
		this.service.handleRequest(this.request, this.response, sockJsPath, this.handler);
R
Rossen Stoyanchev 已提交
148 149 150 151

		assertEquals(httpStatus.value(), this.servletResponse.getStatus());
	}

152 153 154 155
	@Test
	public void handleEmptyContentType() throws Exception {

		servletRequest.setContentType("");
156
		handleRequest("GET", "/echo/info", HttpStatus.OK);
157 158 159 160

		assertEquals("Invalid/empty content should have been ignored", 200, this.servletResponse.getStatus());
	}

161

R
Rossen Stoyanchev 已提交
162 163 164 165
	private static class TestSockJsService extends AbstractSockJsService {

		private String sessionId;

166
		@SuppressWarnings("unused")
167
		private String transport;
R
Rossen Stoyanchev 已提交
168 169 170 171 172 173 174 175

		private WebSocketHandler handler;

		public TestSockJsService(TaskScheduler scheduler) {
			super(scheduler);
		}

		@Override
176 177
		protected void handleRawWebSocketRequest(ServerHttpRequest req, ServerHttpResponse res,
				WebSocketHandler handler) throws IOException {
R
Rossen Stoyanchev 已提交
178 179 180 181 182

			this.handler = handler;
		}

		@Override
183
		protected void handleTransportRequest(ServerHttpRequest req, ServerHttpResponse res, WebSocketHandler handler,
184
				String sessionId, String transport) throws SockJsException {
R
Rossen Stoyanchev 已提交
185 186

			this.sessionId = sessionId;
187
			this.transport = transport;
R
Rossen Stoyanchev 已提交
188 189 190 191 192
			this.handler = handler;
		}
	}

}