From c66632a3a0caa707d99a5198096ca0698a84a514 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Mon, 25 Jan 2016 15:00:54 -0500 Subject: [PATCH] Fix `CrossOriginTests`for SF-4.3 compatibility https://build.spring.io/browse/INT-MJATS41-524 Since SF-4.3 (see https://jira.spring.io/browse/SPR-13130) the `OPTIONS` request, which isn't CORS one (without `HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD`), is handled by new internal `HttpOptionsHandler`, which just responds with `Allow` header. --- .../http/inbound/CrossOriginTests.java | 35 ++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/CrossOriginTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/CrossOriginTests.java index 2a4d6d6d6e..480cd848ab 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/CrossOriginTests.java +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/CrossOriginTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,15 @@ package org.springframework.integration.http.inbound; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.junit.Before; import org.junit.Test; @@ -33,8 +37,10 @@ import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.util.ClassUtils; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerExecutionChain; import org.springframework.web.servlet.HandlerInterceptor; @@ -47,6 +53,11 @@ import org.springframework.web.servlet.HandlerInterceptor; @DirtiesContext public class CrossOriginTests { + // SPR-13130 + private static boolean isSpring43 = + ClassUtils.isPresent("org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping$HttpOptionsHandler", + CrossOriginTests.class.getClassLoader()); + @Autowired private IntegrationRequestMappingHandlerMapping handlerMapping; @@ -161,11 +172,27 @@ public class CrossOriginTests { assertNull(config.getMaxAge()); } - @Test(expected = HttpRequestMethodNotSupportedException.class) - public void preFlightRequestWithoutRequestMethodHeader() throws Exception { + @Test + public void testOptionsHeaderHandling() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/default"); request.addHeader(HttpHeaders.ORIGIN, "http://domain2.com"); - this.handlerMapping.getHandler(request); + try { + HandlerExecutionChain handler = this.handlerMapping.getHandler(request); + if (isSpring43) { + // SPR-13130 + assertNotNull(handler); + Object handlerMethod = handler.getHandler(); + assertNotNull(handlerMethod); + assertThat(handlerMethod, instanceOf(HandlerMethod.class)); + assertThat(((HandlerMethod) handlerMethod).getBeanType().getName(), + containsString("HttpOptionsHandler")); + return; + } + fail("HttpRequestMethodNotSupportedException expected"); + } + catch (Exception e) { + assertThat(e, instanceOf(HttpRequestMethodNotSupportedException.class)); + } } private CorsConfiguration getCorsConfiguration(HandlerExecutionChain chain, boolean isPreFlightRequest) {