Skip to content

Commit 8e92929

Browse files
committed
test: 변경된 로직에 맞게 테스트 코드 수정
1 parent f5940a6 commit 8e92929

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed

src/test/java/com/example/solidconnection/websocket/WebSocketStompIntegrationTest.java

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
import static java.util.concurrent.TimeUnit.SECONDS;
44
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;
5+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
76

87
import com.example.solidconnection.auth.service.AccessToken;
98
import com.example.solidconnection.auth.service.AuthTokenProvider;
109
import com.example.solidconnection.siteuser.domain.SiteUser;
1110
import com.example.solidconnection.siteuser.fixture.SiteUserFixture;
1211
import com.example.solidconnection.support.TestContainerSpringBootTest;
1312
import java.util.List;
14-
import java.util.concurrent.ArrayBlockingQueue;
15-
import java.util.concurrent.BlockingQueue;
1613
import java.util.concurrent.ExecutionException;
14+
import java.util.concurrent.TimeUnit;
1715
import org.junit.jupiter.api.AfterEach;
1816
import org.junit.jupiter.api.BeforeEach;
1917
import org.junit.jupiter.api.DisplayName;
@@ -22,11 +20,9 @@
2220
import org.springframework.beans.factory.annotation.Autowired;
2321
import org.springframework.boot.test.web.server.LocalServerPort;
2422
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
25-
import org.springframework.messaging.simp.stomp.StompHeaders;
2623
import org.springframework.messaging.simp.stomp.StompSession;
2724
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
2825
import org.springframework.web.client.HttpClientErrorException;
29-
import org.springframework.web.socket.WebSocketHttpHeaders;
3026
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
3127
import org.springframework.web.socket.messaging.WebSocketStompClient;
3228
import org.springframework.web.socket.sockjs.client.SockJsClient;
@@ -67,48 +63,28 @@ void tearDown() {
6763
@Nested
6864
class WebSocket_핸드셰이크_및_STOMP_세션_수립_테스트 {
6965

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-
7966
@Test
8067
void 인증된_사용자는_핸드셰이크를_성공한다() throws Exception {
8168
// given
8269
SiteUser user = siteUserFixture.사용자();
8370
AccessToken accessToken = authTokenProvider.generateAccessToken(user);
84-
85-
WebSocketHttpHeaders handshakeHeaders = new WebSocketHttpHeaders();
86-
handshakeHeaders.add("Authorization", "Bearer " + accessToken.token());
71+
String tokenUrl = url + "?token=" + accessToken.token();
8772

8873
// when
89-
stompSession = stompClient.connectAsync(url, handshakeHeaders, new StompHeaders(), sessionHandler).get(5, SECONDS);
74+
stompSession = stompClient.connectAsync(tokenUrl, new StompSessionHandlerAdapter() {}).get(5, SECONDS);
9075

9176
// then
92-
assertAll(
93-
() -> assertThat(stompSession).isNotNull(),
94-
() -> assertThat(transportErrorQueue).isEmpty()
95-
);
77+
assertThat(stompSession.isConnected()).isTrue();
9678
}
9779

9880
@Test
9981
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-
);
82+
// when & then
83+
assertThatThrownBy(() -> {
84+
stompClient.connectAsync(url, new StompSessionHandlerAdapter() {
85+
}).get(5, TimeUnit.SECONDS);
86+
}).isInstanceOf(ExecutionException.class)
87+
.hasCauseInstanceOf(HttpClientErrorException.Unauthorized.class);
11288
}
11389
}
11490
}

0 commit comments

Comments
 (0)