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
175 lines (143 loc) · 5.39 KB
/
Copy pathBasicClientTest.java
File metadata and controls
175 lines (143 loc) · 5.39 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright © 2012 The Feign Authors ([email protected])
*
* 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 feign.Feign;
import feign.Logger.JavaLogger;
import feign.Response;
import feign.jackson.JacksonEncoder;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(webEnvironment = DEFINED_PORT, classes = Server.class)
class BasicClientTest {
private static TestClient API;
@TempDir static Path logDir;
@BeforeAll
static void configureClient() {
var logFile = logDir.resolve("log.txt").toString();
API =
Feign.builder()
.encoder(new FormEncoder(new JacksonEncoder()))
.logger(new JavaLogger(BasicClientTest.class).appendToFile(logFile))
.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 {
var path =
Path.of(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 {
var path =
Path.of(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() {
var 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 {
var path1 =
Path.of(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
assertThat(path1).exists();
var path2 =
Path.of(
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 {
var path1 =
Path.of(Thread.currentThread().getContextClassLoader().getResource("file.txt").toURI());
assertThat(path1).exists();
var path2 =
Path.of(
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 {
var dto = new Dto("Artem", 11);
var path =
Path.of(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 {
var path =
Path.of(Thread.currentThread().getContextClassLoader().getResource("file.abc").toURI());
assertThat(path).exists();
assertThat(API.uploadUnknownType(path.toFile())).isEqualTo("application/octet-stream");
}
@Test
void testFormData() throws Exception {
var 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 {
var names = new String[] {"Milada", "Thais"};
var stringResponse = API.submitRepeatableQueryParam(names);
assertThat(stringResponse).isEqualTo("Milada and Thais");
}
@Test
void testSubmitRepeatableFormParam() throws Exception {
var names = Arrays.asList("Milada", "Thais");
var stringResponse = API.submitRepeatableFormParam(names);
assertThat(stringResponse).isEqualTo("Milada and Thais");
}
}