@ -18,13 +18,13 @@ package org.springframework.web.socket.sockjs.support;
import java.io.IOException ;
import java.io.IOException ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.List ;
import java.util.Collections ;
import javax.servlet.ServletOutputStream ;
import javax.servlet.ServletOutputStream ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpServletResponse ;
import org.junit.Before ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.Test ;
import org.springframework.http.HttpHeaders ;
import org.springframework.http.HttpHeaders ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.server.ServerHttpRequest ;
import org.springframework.http.server.ServerHttpRequest ;
@ -55,15 +55,14 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
@Override
@Override
@Before
@Before
public void setU p ( ) {
public void setu p ( ) {
super . setU p ( ) ;
super . setu p ( ) ;
this . service = new TestSockJsService ( new ThreadPoolTaskScheduler ( ) ) ;
this . service = new TestSockJsService ( new ThreadPoolTaskScheduler ( ) ) ;
}
}
@Test
@Test
public void validateRequest ( ) throws Exception {
public void validateRequest ( ) {
this . service . setWebSocketEnabled ( false ) ;
this . service . setWebSocketEnabled ( false ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/server/session/websocket" , HttpStatus . NOT_FOUND ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/server/session/websocket" , HttpStatus . NOT_FOUND ) ;
@ -82,7 +81,7 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
}
}
@Test
@Test
public void handleInfoGet ( ) throws Exception {
public void handleInfoGet ( ) throws IO Exception {
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
assertEquals ( "application/json;charset=UTF-8" , this . servletResponse . getContentType ( ) ) ;
assertEquals ( "application/json;charset=UTF-8" , this . servletResponse . getContentType ( ) ) ;
@ -105,7 +104,7 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
assertEquals ( ",\"origins\":[\"*:*\"],\"cookie_needed\":false,\"websocket\":false}" ,
assertEquals ( ",\"origins\":[\"*:*\"],\"cookie_needed\":false,\"websocket\":false}" ,
body . substring ( body . indexOf ( ',' ) ) ) ;
body . substring ( body . indexOf ( ',' ) ) ) ;
this . service . setAllowedOrigins ( Arrays . as List( "http://mydomain1.com" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "http://mydomain1.com" ) ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
assertNull ( this . servletResponse . getHeader ( HttpHeaders . ACCESS_CONTROL_ALLOW_ORIGIN ) ) ;
assertNull ( this . servletResponse . getHeader ( HttpHeaders . ACCESS_CONTROL_ALLOW_ORIGIN ) ) ;
assertNull ( this . servletResponse . getHeader ( HttpHeaders . ACCESS_CONTROL_ALLOW_CREDENTIALS ) ) ;
assertNull ( this . servletResponse . getHeader ( HttpHeaders . ACCESS_CONTROL_ALLOW_CREDENTIALS ) ) ;
@ -113,7 +112,7 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
}
}
@Test // SPR-12226 and SPR-12660
@Test // SPR-12226 and SPR-12660
public void handleInfoGetWithOrigin ( ) throws Exception {
public void handleInfoGetWithOrigin ( ) throws IO Exception {
this . servletRequest . setServerName ( "mydomain2.com" ) ;
this . servletRequest . setServerName ( "mydomain2.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ORIGIN , "http://mydomain2.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ORIGIN , "http://mydomain2.com" ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
@ -126,24 +125,22 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
assertEquals ( ",\"origins\":[\"*:*\"],\"cookie_needed\":true,\"websocket\":true}" ,
assertEquals ( ",\"origins\":[\"*:*\"],\"cookie_needed\":true,\"websocket\":true}" ,
body . substring ( body . indexOf ( ',' ) ) ) ;
body . substring ( body . indexOf ( ',' ) ) ) ;
this . service . setAllowedOrigins ( Arrays . as List( "http://mydomain1.com" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "http://mydomain1.com" ) ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
List < String > origins = Arrays . asList ( "http://mydomain1.com" , "http://mydomain2.com" , "http://mydomain3.com" ) ;
this . service . setAllowedOrigins ( Arrays . asList ( "http://mydomain1.com" , "http://mydomain2.com" , "http://mydomain3.com" ) ) ;
this . service . setAllowedOrigins ( origins ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
this . service . setAllowedOrigins ( Arrays . as List( "*" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "*" ) ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
this . servletRequest . setServerName ( "mydomain3.com" ) ;
this . servletRequest . setServerName ( "mydomain3.com" ) ;
this . service . setAllowedOrigins ( Arrays . as List( "http://mydomain1.com" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "http://mydomain1.com" ) ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . FORBIDDEN ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . FORBIDDEN ) ;
}
}
@Test // SPR-11443
@Test // SPR-11443
public void handleInfoGetCorsFilter ( ) throws Exception {
public void handleInfoGetCorsFilter ( ) {
// Simulate scenario where Filter would have already set CORS headers
// Simulate scenario where Filter would have already set CORS headers
this . servletResponse . setHeader ( HttpHeaders . ACCESS_CONTROL_ALLOW_ORIGIN , "foobar:123" ) ;
this . servletResponse . setHeader ( HttpHeaders . ACCESS_CONTROL_ALLOW_ORIGIN , "foobar:123" ) ;
@ -153,7 +150,7 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
}
}
@Test // SPR-11919
@Test // SPR-11919
public void handleInfoGetWildflyNPE ( ) throws Exception {
public void handleInfoGetWildflyNPE ( ) throws IO Exception {
HttpServletResponse mockResponse = mock ( HttpServletResponse . class ) ;
HttpServletResponse mockResponse = mock ( HttpServletResponse . class ) ;
ServletOutputStream ous = mock ( ServletOutputStream . class ) ;
ServletOutputStream ous = mock ( ServletOutputStream . class ) ;
given ( mockResponse . getHeaders ( HttpHeaders . ACCESS_CONTROL_ALLOW_ORIGIN ) ) . willThrow ( NullPointerException . class ) ;
given ( mockResponse . getHeaders ( HttpHeaders . ACCESS_CONTROL_ALLOW_ORIGIN ) ) . willThrow ( NullPointerException . class ) ;
@ -166,18 +163,18 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
}
}
@Test // SPR-12660
@Test // SPR-12660
public void handleInfoOptions ( ) throws Exception {
public void handleInfoOptions ( ) {
this . servletRequest . addHeader ( HttpHeaders . ACCESS_CONTROL_REQUEST_HEADERS , "Last-Modified" ) ;
this . servletRequest . addHeader ( HttpHeaders . ACCESS_CONTROL_REQUEST_HEADERS , "Last-Modified" ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
this . service . setAllowedOrigins ( Arrays . as List( "http://mydomain1.com" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "http://mydomain1.com" ) ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
}
}
@Test // SPR-12226 and SPR-12660
@Test // SPR-12226 and SPR-12660
public void handleInfoOptionsWithAllowedOrigin ( ) throws Exception {
public void handleInfoOptionsWithAllowedOrigin ( ) {
this . servletRequest . setServerName ( "mydomain2.com" ) ;
this . servletRequest . setServerName ( "mydomain2.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ORIGIN , "http://mydomain2.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ORIGIN , "http://mydomain2.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ACCESS_CONTROL_REQUEST_METHOD , "GET" ) ;
this . servletRequest . addHeader ( HttpHeaders . ACCESS_CONTROL_REQUEST_METHOD , "GET" ) ;
@ -185,22 +182,21 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
assertNotNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNotNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
this . service . setAllowedOrigins ( Arrays . as List( "http://mydomain1.com" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "http://mydomain1.com" ) ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
assertNotNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNotNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
List < String > origins = Arrays . asList ( "http://mydomain1.com" , "http://mydomain2.com" , "http://mydomain3.com" ) ;
this . service . setAllowedOrigins ( Arrays . asList ( "http://mydomain1.com" , "http://mydomain2.com" , "http://mydomain3.com" ) ) ;
this . service . setAllowedOrigins ( origins ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
assertNotNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNotNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
this . service . setAllowedOrigins ( Arrays . as List( "*" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "*" ) ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
assertNotNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNotNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
}
}
@Test // SPR-16304
@Test // SPR-16304
public void handleInfoOptionsWithForbiddenOrigin ( ) throws Exception {
public void handleInfoOptionsWithForbiddenOrigin ( ) {
this . servletRequest . setServerName ( "mydomain3.com" ) ;
this . servletRequest . setServerName ( "mydomain3.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ORIGIN , "http://mydomain2.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ORIGIN , "http://mydomain2.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ACCESS_CONTROL_REQUEST_METHOD , "GET" ) ;
this . servletRequest . addHeader ( HttpHeaders . ACCESS_CONTROL_REQUEST_METHOD , "GET" ) ;
@ -209,34 +205,33 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
CorsConfiguration corsConfiguration = this . service . getCorsConfiguration ( this . servletRequest ) ;
CorsConfiguration corsConfiguration = this . service . getCorsConfiguration ( this . servletRequest ) ;
assertTrue ( corsConfiguration . getAllowedOrigins ( ) . isEmpty ( ) ) ;
assertTrue ( corsConfiguration . getAllowedOrigins ( ) . isEmpty ( ) ) ;
this . service . setAllowedOrigins ( Arrays . as List( "http://mydomain1.com" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "http://mydomain1.com" ) ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . FORBIDDEN ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . FORBIDDEN ) ;
corsConfiguration = this . service . getCorsConfiguration ( this . servletRequest ) ;
corsConfiguration = this . service . getCorsConfiguration ( this . servletRequest ) ;
assertEquals ( Arrays . as List( "http://mydomain1.com" ) , corsConfiguration . getAllowedOrigins ( ) ) ;
assertEquals ( Collections . singleton List( "http://mydomain1.com" ) , corsConfiguration . getAllowedOrigins ( ) ) ;
}
}
@Test // SPR-12283
@Test // SPR-12283
public void handleInfoOptionsWithOriginAndCorsHeadersDisabled ( ) throws Exception {
public void handleInfoOptionsWithOriginAndCorsHeadersDisabled ( ) {
this . servletRequest . addHeader ( HttpHeaders . ORIGIN , "http://mydomain2.com" ) ;
this . servletRequest . addHeader ( HttpHeaders . ORIGIN , "http://mydomain2.com" ) ;
this . service . setAllowedOrigins ( Arrays . as List( "*" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "*" ) ) ;
this . service . setSuppressCors ( true ) ;
this . service . setSuppressCors ( true ) ;
this . servletRequest . addHeader ( HttpHeaders . ACCESS_CONTROL_REQUEST_HEADERS , "Last-Modified" ) ;
this . servletRequest . addHeader ( HttpHeaders . ACCESS_CONTROL_REQUEST_HEADERS , "Last-Modified" ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
this . service . setAllowedOrigins ( Arrays . as List( "http://mydomain1.com" ) ) ;
this . service . setAllowedOrigins ( Collections . singleton List( "http://mydomain1.com" ) ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . FORBIDDEN ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . FORBIDDEN ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
List < String > origins = Arrays . asList ( "http://mydomain1.com" , "http://mydomain2.com" , "http://mydomain3.com" ) ;
this . service . setAllowedOrigins ( Arrays . asList ( "http://mydomain1.com" , "http://mydomain2.com" , "http://mydomain3.com" ) ) ;
this . service . setAllowedOrigins ( origins ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
resetResponseAndHandleRequest ( "OPTIONS" , "/echo/info" , HttpStatus . NO_CONTENT ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
assertNull ( this . service . getCorsConfiguration ( this . servletRequest ) ) ;
}
}
@Test
@Test
public void handleIframeRequest ( ) throws Exception {
public void handleIframeRequest ( ) throws IO Exception {
resetResponseAndHandleRequest ( "GET" , "/echo/iframe.html" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/iframe.html" , HttpStatus . OK ) ;
assertEquals ( "text/html;charset=UTF-8" , this . servletResponse . getContentType ( ) ) ;
assertEquals ( "text/html;charset=UTF-8" , this . servletResponse . getContentType ( ) ) ;
@ -247,13 +242,13 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
}
}
@Test
@Test
public void handleIframeRequestNotModified ( ) throws Exception {
public void handleIframeRequestNotModified ( ) {
this . servletRequest . addHeader ( "If-None-Match" , "\"0096cbd37f2a5218c33bb0826a7c74cbf\"" ) ;
this . servletRequest . addHeader ( "If-None-Match" , "\"0096cbd37f2a5218c33bb0826a7c74cbf\"" ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/iframe.html" , HttpStatus . NOT_MODIFIED ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/iframe.html" , HttpStatus . NOT_MODIFIED ) ;
}
}
@Test
@Test
public void handleRawWebSocketRequest ( ) throws Exception {
public void handleRawWebSocketRequest ( ) throws IO Exception {
resetResponseAndHandleRequest ( "GET" , "/echo" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo" , HttpStatus . OK ) ;
assertEquals ( "Welcome to SockJS!\n" , this . servletResponse . getContentAsString ( ) ) ;
assertEquals ( "Welcome to SockJS!\n" , this . servletResponse . getContentAsString ( ) ) ;
@ -263,7 +258,7 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
}
}
@Test
@Test
public void handleEmptyContentType ( ) throws Exception {
public void handleEmptyContentType ( ) {
this . servletRequest . setContentType ( "" ) ;
this . servletRequest . setContentType ( "" ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
resetResponseAndHandleRequest ( "GET" , "/echo/info" , HttpStatus . OK ) ;
@ -271,12 +266,12 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
}
}
private void resetResponseAndHandleRequest ( String httpMethod , String uri , HttpStatus httpStatus ) throws IOException {
private void resetResponseAndHandleRequest ( String httpMethod , String uri , HttpStatus httpStatus ) {
resetResponse ( ) ;
resetResponse ( ) ;
handleRequest ( httpMethod , uri , httpStatus ) ;
handleRequest ( httpMethod , uri , httpStatus ) ;
}
}
private void handleRequest ( String httpMethod , String uri , HttpStatus httpStatus ) throws IOException {
private void handleRequest ( String httpMethod , String uri , HttpStatus httpStatus ) {
setRequest ( httpMethod , uri ) ;
setRequest ( httpMethod , uri ) ;
String sockJsPath = uri . substring ( "/echo" . length ( ) ) ;
String sockJsPath = uri . substring ( "/echo" . length ( ) ) ;
this . service . handleRequest ( this . request , this . response , sockJsPath , this . handler ) ;
this . service . handleRequest ( this . request , this . response , sockJsPath , this . handler ) ;