/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/WebsocketRoutingFilterTests.java

https://github.com/spring-cloud/spring-cloud-gateway · Java · 56 lines · 32 code · 9 blank · 15 comment · 0 complexity · 92dc14e2bc09563c8bc30ca3c2261716 MD5 · raw file

  1. /*
  2. * Copyright 2013-2020 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. * https://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. package org.springframework.cloud.gateway.filter;
  17. import java.net.URI;
  18. import org.junit.Test;
  19. import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
  20. import org.springframework.mock.web.server.MockServerWebExchange;
  21. import org.springframework.web.server.ServerWebExchange;
  22. import static org.assertj.core.api.Assertions.assertThat;
  23. import static org.springframework.cloud.gateway.filter.WebsocketRoutingFilter.changeSchemeIfIsWebSocketUpgrade;
  24. import static org.springframework.cloud.gateway.filter.WebsocketRoutingFilter.convertHttpToWs;
  25. import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
  26. import static org.springframework.http.HttpHeaders.UPGRADE;
  27. public class WebsocketRoutingFilterTests {
  28. @Test
  29. public void testConvertHttpToWs() {
  30. assertThat(convertHttpToWs("http")).isEqualTo("ws");
  31. assertThat(convertHttpToWs("HTTP")).isEqualTo("ws");
  32. assertThat(convertHttpToWs("https")).isEqualTo("wss");
  33. assertThat(convertHttpToWs("HTTPS")).isEqualTo("wss");
  34. assertThat(convertHttpToWs("tcp")).isEqualTo("tcp");
  35. }
  36. @Test
  37. public void testEncodedUrl() {
  38. MockServerHttpRequest request = MockServerHttpRequest.get("http://not-matters-that")
  39. .header(UPGRADE, "WebSocket").build();
  40. ServerWebExchange exchange = MockServerWebExchange.from(request);
  41. exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR,
  42. URI.create("http://microservice/my-service/websocket%20upgrade"));
  43. changeSchemeIfIsWebSocketUpgrade(exchange);
  44. URI wsRequestUrl = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
  45. assertThat(wsRequestUrl).isEqualTo(URI.create("ws://microservice/my-service/websocket%20upgrade"));
  46. }
  47. }