|
| 1 | +package com.example.solidconnection.websocket; |
| 2 | + |
| 3 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.assertj.core.api.ThrowableAssert.catchThrowable; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 7 | + |
| 8 | +import com.example.solidconnection.auth.service.AccessToken; |
| 9 | +import com.example.solidconnection.auth.service.AuthTokenProvider; |
| 10 | +import com.example.solidconnection.siteuser.domain.SiteUser; |
| 11 | +import com.example.solidconnection.siteuser.fixture.SiteUserFixture; |
| 12 | +import com.example.solidconnection.support.TestContainerSpringBootTest; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.ArrayBlockingQueue; |
| 15 | +import java.util.concurrent.BlockingQueue; |
| 16 | +import java.util.concurrent.ExecutionException; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.DisplayName; |
| 20 | +import org.junit.jupiter.api.Nested; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 24 | +import org.springframework.messaging.converter.MappingJackson2MessageConverter; |
| 25 | +import org.springframework.messaging.simp.stomp.StompHeaders; |
| 26 | +import org.springframework.messaging.simp.stomp.StompSession; |
| 27 | +import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter; |
| 28 | +import org.springframework.web.client.HttpClientErrorException; |
| 29 | +import org.springframework.web.socket.WebSocketHttpHeaders; |
| 30 | +import org.springframework.web.socket.client.standard.StandardWebSocketClient; |
| 31 | +import org.springframework.web.socket.messaging.WebSocketStompClient; |
| 32 | +import org.springframework.web.socket.sockjs.client.SockJsClient; |
| 33 | +import org.springframework.web.socket.sockjs.client.Transport; |
| 34 | +import org.springframework.web.socket.sockjs.client.WebSocketTransport; |
| 35 | + |
| 36 | +@TestContainerSpringBootTest |
| 37 | +@DisplayName("WebSocket/STOMP 통합 테스트") |
| 38 | +class WebSocketStompIntegrationTest { |
| 39 | + |
| 40 | + @LocalServerPort |
| 41 | + private int port; |
| 42 | + private String url; |
| 43 | + private WebSocketStompClient stompClient; |
| 44 | + private StompSession stompSession; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private AuthTokenProvider authTokenProvider; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private SiteUserFixture siteUserFixture; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void setUp() { |
| 54 | + this.url = String.format("ws://localhost:%d/connect", port); |
| 55 | + List<Transport> transports = List.of(new WebSocketTransport(new StandardWebSocketClient())); |
| 56 | + this.stompClient = new WebSocketStompClient(new SockJsClient(transports)); |
| 57 | + this.stompClient.setMessageConverter(new MappingJackson2MessageConverter()); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterEach |
| 61 | + void tearDown() { |
| 62 | + if (this.stompSession != null && this.stompSession.isConnected()) { |
| 63 | + this.stompSession.disconnect(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Nested |
| 68 | + class WebSocket_핸드셰이크_및_STOMP_세션_수립_테스트 { |
| 69 | + |
| 70 | + private final BlockingQueue<Throwable> transportErrorQueue = new ArrayBlockingQueue<>(1); |
| 71 | + |
| 72 | + private final StompSessionHandlerAdapter sessionHandler = new StompSessionHandlerAdapter() { |
| 73 | + @Override |
| 74 | + public void handleTransportError(StompSession session, Throwable exception) { |
| 75 | + transportErrorQueue.add(exception); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + @Test |
| 80 | + void 인증된_사용자는_핸드셰이크를_성공한다() throws Exception { |
| 81 | + // given |
| 82 | + SiteUser user = siteUserFixture.사용자(); |
| 83 | + AccessToken accessToken = authTokenProvider.generateAccessToken(authTokenProvider.toSubject(user), user.getRole()); |
| 84 | + |
| 85 | + WebSocketHttpHeaders handshakeHeaders = new WebSocketHttpHeaders(); |
| 86 | + handshakeHeaders.add("Authorization", "Bearer " + accessToken.token()); |
| 87 | + |
| 88 | + // when |
| 89 | + stompSession = stompClient.connectAsync(url, handshakeHeaders, new StompHeaders(), sessionHandler).get(5, SECONDS); |
| 90 | + |
| 91 | + // then |
| 92 | + assertAll( |
| 93 | + () -> assertThat(stompSession).isNotNull(), |
| 94 | + () -> assertThat(transportErrorQueue).isEmpty() |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void 인증되지_않은_사용자는_핸드셰이크를_실패한다() { |
| 100 | + // when |
| 101 | + Throwable thrown = catchThrowable(() -> { |
| 102 | + stompSession = stompClient.connectAsync(url, new WebSocketHttpHeaders(), new StompHeaders(), sessionHandler).get(5, SECONDS); |
| 103 | + }); |
| 104 | + |
| 105 | + // then |
| 106 | + assertAll( |
| 107 | + () -> assertThat(thrown) |
| 108 | + .isInstanceOf(ExecutionException.class) |
| 109 | + .hasCauseInstanceOf(HttpClientErrorException.Unauthorized.class), |
| 110 | + () -> assertThat(transportErrorQueue).hasSize(1) |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments