forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicClientTest.java
More file actions
157 lines (128 loc) · 5.1 KB
/
Copy pathBasicClientTest.java
File metadata and controls
157 lines (128 loc) · 5.1 KB
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
/*
* Copyright 2012-2024 The Feign Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package feign.form;
import static feign.Logger.Level.FULL;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import lombok.val;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import feign.Feign;
import feign.Logger.JavaLogger;
import feign.Response;
import feign.jackson.JacksonEncoder;
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = Server.class)
class BasicClientTest {
private static final TestClient API;
static {
API = Feign.builder().encoder(new FormEncoder(new JacksonEncoder()))
.logger(new JavaLogger(BasicClientTest.class).appendToFile("log.txt")).logLevel(FULL)
.target(TestClient.class, "http://localhost:8080");
}
@Test
void testForm() {
assertThat(API.form("1", "1")).isNotNull().extracting(Response::status).isEqualTo(200);
}
@Test
void testFormException() {
assertThat(API.form("1", "2")).isNotNull().extracting(Response::status).isEqualTo(400);
}
@Test
void testUpload() throws Exception {
val path =
Paths.get(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
assertThat(path).exists();
assertThat(API.upload(path.toFile())).asLong().isEqualTo(Files.size(path));
}
@Test
void testUploadWithParam() throws Exception {
val path =
Paths.get(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
assertThat(path).exists();
assertThat(API.upload(10, Boolean.TRUE, path.toFile())).asLong().isEqualTo(Files.size(path));
}
@Test
void testJson() {
val dto = new Dto("Artem", 11);
assertThat(API.json(dto)).isEqualTo("ok");
}
@Test
void testQueryMap() {
Map<String, Object> value =
singletonMap("filter", (Object) asList("one", "two", "three", "four"));
assertThat(API.queryMap(value)).isEqualTo("4");
}
@Test
void testMultipleFilesArray() throws Exception {
val path1 =
Paths.get(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
assertThat(path1).exists();
val path2 = Paths.get(
Thread.currentThread().getContextClassLoader().getResource("another_file.txt").toURI());
assertThat(path2).exists();
assertThat(API.uploadWithArray(new File[] {path1.toFile(), path2.toFile()})).asLong()
.isEqualTo(Files.size(path1) + Files.size(path2));
}
@Test
void testMultipleFilesList() throws Exception {
val path1 =
Paths.get(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
assertThat(path1).exists();
val path2 = Paths.get(
Thread.currentThread().getContextClassLoader().getResource("another_file.txt").toURI());
assertThat(path2).exists();
assertThat(API.uploadWithList(asList(path1.toFile(), path2.toFile()))).asLong()
.isEqualTo(Files.size(path1) + Files.size(path2));
}
@Test
void testUploadWithDto() throws Exception {
val dto = new Dto("Artem", 11);
val path =
Paths.get(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
assertThat(path).exists();
assertThat(API.uploadWithDto(dto, path.toFile())).isNotNull().extracting(Response::status)
.isEqualTo(200);
}
@Test
void testUnknownTypeFile() throws Exception {
val path =
Paths.get(Thread.currentThread().getContextClassLoader().getResource("file.abc").toURI());
assertThat(path).exists();
assertThat(API.uploadUnknownType(path.toFile())).isEqualTo("application/octet-stream");
}
@Test
void testFormData() throws Exception {
val formData = new FormData("application/custom-type", "popa.txt", "Allo".getBytes("UTF-8"));
assertThat(API.uploadFormData(formData)).isEqualTo("popa.txt:application/custom-type");
}
@Test
void testSubmitRepeatableQueryParam() throws Exception {
val names = new String[] {"Milada", "Thais"};
val stringResponse = API.submitRepeatableQueryParam(names);
assertThat(stringResponse).isEqualTo("Milada and Thais");
}
@Test
void testSubmitRepeatableFormParam() throws Exception {
val names = Arrays.asList("Milada", "Thais");
val stringResponse = API.submitRepeatableFormParam(names);
assertThat(stringResponse).isEqualTo("Milada and Thais");
}
}