forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLoggerTest.java
More file actions
114 lines (101 loc) · 4.21 KB
/
Copy pathJavaLoggerTest.java
File metadata and controls
114 lines (101 loc) · 4.21 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
/*
* Copyright © 2012 The Feign Authors (feign@commonhaus.dev)
*
* 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;
import feign.Logger.JavaLogger;
import feign.Request.HttpMethod;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Level;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class JavaLoggerTest {
private static final String CONFIG_KEY = "TestApi#testMethod()";
private java.util.logging.Logger julLogger;
private JavaLogger logger;
@BeforeEach
void setUp() {
julLogger = java.util.logging.Logger.getLogger(JavaLoggerTest.class.getName());
logger = new JavaLogger(JavaLoggerTest.class);
}
@Test
void rebuffersResponseBodyWhenJulLevelIsInfo() throws Exception {
// given
julLogger.setLevel(Level.INFO);
Response responseWithBody =
Response.builder()
.status(200)
.reason("OK")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, null))
.headers(Collections.<String, Collection<String>>emptyMap())
.body("{\"error\":\"test\"}", Util.UTF_8)
.build();
// when
Response result =
logger.logAndRebufferResponse(
CONFIG_KEY, feign.Logger.Level.HEADERS, responseWithBody, 273);
// then
String body1 = Util.toString(result.body().asReader(Util.UTF_8));
String body2 = Util.toString(result.body().asReader(Util.UTF_8));
assert body1.equals("{\"error\":\"test\"}") : "First read should return body content";
assert body2.equals("{\"error\":\"test\"}") : "Second read should return same body content";
}
@Test
void rebuffersResponseBodyWhenJulLevelIsWarning() throws Exception {
// given
julLogger.setLevel(Level.WARNING);
Response responseWithBody =
Response.builder()
.status(500)
.reason("Internal Server Error")
.request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, null))
.headers(Collections.<String, Collection<String>>emptyMap())
.body("{\"message\":\"error details\"}", Util.UTF_8)
.build();
// when
Response result =
logger.logAndRebufferResponse(CONFIG_KEY, feign.Logger.Level.FULL, responseWithBody, 100);
// then
byte[] bodyBytes = Util.toByteArray(result.body().asInputStream());
assert new String(bodyBytes, Util.UTF_8).equals("{\"message\":\"error details\"}")
: "Body should be readable after rebuffering";
}
@Test
void responseBodyReadableMultipleTimesForErrorDecoder() throws Exception {
// given
julLogger.setLevel(Level.SEVERE);
String originalBody = "{\"errorCode\":\"E001\",\"message\":\"Validation failed\"}";
Response responseWithBody =
Response.builder()
.status(400)
.reason("Bad Request")
.request(
Request.create(HttpMethod.POST, "/api/submit", Collections.emptyMap(), null, null))
.headers(Collections.<String, Collection<String>>emptyMap())
.body(originalBody, Util.UTF_8)
.build();
// when
Response result =
logger.logAndRebufferResponse(
CONFIG_KEY, feign.Logger.Level.HEADERS, responseWithBody, 150);
// then
String read1 = Util.toString(result.body().asReader(Util.UTF_8));
String read2 = Util.toString(result.body().asReader(Util.UTF_8));
String read3 = Util.toString(result.body().asReader(Util.UTF_8));
assert read1.equals(originalBody) : "First read should match original body";
assert read2.equals(originalBody) : "Second read should match original body";
assert read3.equals(originalBody) : "Third read should match original body";
}
}