-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockWebServerTest.java
86 lines (72 loc) · 2.67 KB
/
MockWebServerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.github.whatasame.webclient;
import java.io.IOException;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
/**
* reference: <a href="https://github.com/square/okhttp/blob/master/mockwebserver/README.md">OkHttp MockWebServer
* docs</a>
*/
public class MockWebServerTest {
static MockWebServer mockWebServer;
static int port;
static WebClient webClient;
@BeforeAll
static void beforeAll() throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.start();
port = mockWebServer.getPort();
webClient = WebClient.builder().baseUrl("http://localhost:" + port).build();
}
@AfterAll
static void afterAll() throws IOException {
mockWebServer.shutdown();
}
@Test
@DisplayName("GET 요청 테스트: 회원 정보 조회")
void testGet() {
/* given */
mockWebServer.enqueue(new MockResponse()
.setBody(
"""
{
"email": "[email protected]",
"password": "password"
}
""")
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
/* when */
final Mono<Member> memberMono =
webClient.get().uri("/member/me").retrieve().bodyToMono(Member.class);
/* then */
StepVerifier.create(memberMono)
.expectNext(new Member("[email protected]", "password"))
.verifyComplete();
}
@Test
@DisplayName("POST 요청 테스트: 회원 가입")
void testPost() {
/* given */
mockWebServer.enqueue(new MockResponse()
.setBody("777")
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
/* when */
final Mono<Long> memberIdMono = webClient
.post()
.uri("/member/signup")
.bodyValue(new Member("[email protected]", "password")) // use same member object as mock parameter
.retrieve()
.bodyToMono(Long.class);
/* then */
StepVerifier.create(memberIdMono).expectNext(777L).verifyComplete();
}
record Member(String email, String password) {}
}