-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestTemplateTest.java
193 lines (165 loc) · 6.29 KB
/
RestTemplateTest.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.example;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class RestTemplateTest {
private final RestTemplate restTemplate = new RestTemplateBuilder()
.rootUri("http://localhost:8080")
.build();
@Test
public void base() {
String res = restTemplate.getForObject("/base", String.class);
Assertions.assertEquals("base", res);
}
@Test
public void entry() {
String res = restTemplate.getForObject("/entry?key=k&value=v", String.class);
Assertions.assertEquals("entry: k/v", res);
}
@Test
public void entry1() {
String res = restTemplate.getForObject("/entry/k/v", String.class);
Assertions.assertEquals("entry: k/v", res);
}
@Test
public void param() {
String res = restTemplate.getForObject("/param?param=param", String.class);
Assertions.assertEquals("param: param", res);
}
@Test
public void param1() {
String res = restTemplate.getForObject("/param1?param1=param1", String.class);
Assertions.assertEquals("param1: param1", res);
String res1 = restTemplate.getForObject("/param1", String.class);
Assertions.assertEquals("param1", res1);
}
@Test
public void uriPattern() {
String res = restTemplate.getForObject("/uriPattern/param", String.class);
Assertions.assertEquals("uriPattern: param", res);
}
@Test
public void uriRegex() {
String res = restTemplate.getForObject("/uriRegex/param", String.class);
Assertions.assertEquals("uriRegex: param", res);
}
@Test
public void uriRegex1() {
String res = restTemplate.getForObject("/uriRegex/123", String.class);
Assertions.assertEquals("uriRegex1: 123", res);
}
@Test
public void matrix() {
String res = restTemplate.getForObject("/matrix/path;a=a;b=b", String.class);
Assertions.assertEquals("matrix: a=a, b=b", res);
}
@Test
public void matrix1() {
String res = restTemplate.getForObject("/matrix1/path;a=a;b=b/path1;a=a1;b=b1", String.class);
Assertions.assertEquals("matrix: a=a, a1=a1, b=b, b1=b1", res);
}
@Test
public void matrix2() {
String res = restTemplate.getForObject("/matrix2/path;a=a;b=b/path1;a=a1;b=b1", String.class);
Assertions.assertEquals("matrix: {a=[a, a1], b=[b, b1]}", res);
}
@Test
public void head() {
HttpHeaders headers = new HttpHeaders();
headers.add("myHead", "head");
HttpEntity<Void> entity = new HttpEntity<>(null, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange("/head", HttpMethod.GET, entity, String.class);
Assertions.assertEquals("head[myHead]: head", responseEntity.getBody());
}
@Test
public void cookie() {
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.COOKIE, "cookie=cookie");
HttpEntity<Void> entity = new HttpEntity<>(null, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange("/cookie", HttpMethod.GET, entity, String.class);
Assertions.assertEquals("cookie[cookie]: cookie", responseEntity.getBody());
}
@Test
public void request() {
String res = restTemplate.getForObject("/request", String.class);
Assertions.assertEquals("request: GET", res);
}
@Test
public void response() {
String res = restTemplate.getForObject("/response", String.class);
Assertions.assertEquals("response", res);
}
@Test
public void retEntry() {
String res = restTemplate.getForObject("/retEntry", String.class);
Assertions.assertEquals("{\"key\":\"key\",\"value\":\"value\"}", res);
}
@Test
public void deferred() {
String res = restTemplate.getForObject("/deferred", String.class);
Assertions.assertTrue(Long.parseLong(res) >= 1000);
}
@Test
public void stream() {
String res = restTemplate.getForObject("/stream", String.class);
System.out.println(res);
}
@Test
public void sse() {
// 该测试不明显,建议使用浏览器访问
String res = restTemplate.getForObject("/sse", String.class);
System.out.println(res);
}
@Test
public void body() {
String res = restTemplate.postForObject("/body", "TEST", String.class);
Assertions.assertEquals("body: TEST", res);
}
@Test
public void form() throws IOException {
Path path = Files.writeString(Paths.get("form.txt"), "TEST");
String res;
try {
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("name", "test");
form.add("file", new FileSystemResource(path));
res = restTemplate.postForObject("/form", form, String.class);
} finally {
Files.delete(path);
}
Assertions.assertEquals("form[test]: TEST", res);
}
@Test
public void form1() throws IOException {
Path path = Files.writeString(Paths.get("form1.txt"), "TEST");
String res;
try {
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("name", "test");
form.add("file", new FileSystemResource(path));
res = restTemplate.postForObject("/form1", form, String.class);
} finally {
Files.delete(path);
}
Assertions.assertEquals("form1[test]: TEST", res);
}
@Test
public void exception() {
try {
String res = restTemplate.getForObject("/exception", String.class);
} catch (HttpServerErrorException e) {
Assertions.assertTrue(e.getStatusCode().is5xxServerError());
Assertions.assertEquals("ERROR", e.getResponseBodyAsString());
}
}
}