Skip to content

Commit 160ff63

Browse files
authored
Make AbstractLineClientExceptionTest more flexible (#1158)
- show all x-line- headers(to support x-line-accepted-request-id and future expansion) - add test cases
1 parent 8c33b7d commit 160ff63

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

clients/line-bot-client-base/src/main/java/com/linecorp/bot/client/base/exception/AbstractLineClientException.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
import java.io.IOException;
2020
import java.net.URL;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.StreamSupport;
2123

24+
import okhttp3.Headers;
2225
import okhttp3.Response;
2326

2427
@SuppressWarnings("serial")
@@ -28,15 +31,15 @@ public class AbstractLineClientException extends IOException {
2831
public AbstractLineClientException(Response response, String message, IOException ioException) {
2932
super("API returns error: code=" + response.code()
3033
+ " requestUrl=" + response.request().url()
31-
+ " requestId=" + response.headers().get("x-line-request-id")
34+
+ headerInfo(response.headers())
3235
+ " " + message, ioException);
3336
this.response = response;
3437
}
3538

3639
public AbstractLineClientException(Response response, String message) {
3740
super("API returns error: code=" + response.code()
3841
+ " requestUrl=" + response.request().url()
39-
+ " requestId=" + response.headers().get("x-line-request-id")
42+
+ headerInfo(response.headers())
4043
+ " " + message);
4144
this.response = response;
4245
}
@@ -52,4 +55,21 @@ public URL getRequestUrl() {
5255
public String getRequestId() {
5356
return response.headers().get("x-line-request-id");
5457
}
58+
59+
public String getHeader(String name) {
60+
return response.headers().get(name);
61+
}
62+
63+
private static String headerInfo(Headers headers) {
64+
String headerInfo = StreamSupport.stream(headers.spliterator(), false)
65+
.filter(it -> it.getFirst().startsWith("x-line-"))
66+
.sorted((a, b) -> a.getFirst().compareTo(b.getSecond()))
67+
.map(it -> it.getFirst() + "=" + it.getSecond())
68+
.collect(Collectors.joining(" "));
69+
if (headerInfo.isEmpty()) {
70+
return "";
71+
} else {
72+
return " " + headerInfo;
73+
}
74+
}
5575
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2023 LINE Corporation
3+
*
4+
* LINE Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package com.linecorp.bot.client.base.exception;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.when;
21+
22+
import java.io.IOException;
23+
import java.net.URL;
24+
import java.util.Map;
25+
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
28+
29+
import okhttp3.Headers;
30+
import okhttp3.Request;
31+
import okhttp3.Response;
32+
33+
34+
public class AbstractLineClientExceptionTest {
35+
36+
private Response response;
37+
38+
@BeforeEach
39+
void setUp() {
40+
response = mock(Response.class);
41+
Request request = new Request.Builder().url("https://example.com").build();
42+
43+
when(response.request()).thenReturn(request);
44+
when(response.code()).thenReturn(200);
45+
Headers headers = Headers.of(Map.of(
46+
"x-line-example", "headerValue",
47+
"x-line-request-id", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
48+
));
49+
when(response.headers()).thenReturn(headers);
50+
}
51+
52+
@Test
53+
void constructorWithIoException() {
54+
IOException ioException = new IOException("test exception");
55+
AbstractLineClientException exception = new AbstractLineClientException(response, "test message", ioException);
56+
57+
assertThat(exception).isNotNull();
58+
assertThat(exception.getMessage()).isEqualTo("API returns error: code=200 requestUrl=https://example.com/ x-line-example=headerValue x-line-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx test message"); assertThat(exception.getCode()).isEqualTo(200);
59+
assertThat(exception.getCause()).isEqualTo(ioException);
60+
}
61+
62+
@Test
63+
void constructorNoException() {
64+
AbstractLineClientException exception = new AbstractLineClientException(response, "test message");
65+
66+
assertThat(exception).isNotNull();
67+
assertThat(exception.getMessage()).isEqualTo("API returns error: code=200 requestUrl=https://example.com/ x-line-example=headerValue x-line-request-id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx test message"); assertThat(exception.getCode()).isEqualTo(200);
68+
assertThat(exception.getCause()).isNull();
69+
}
70+
71+
@Test
72+
void getRequestUrl() throws Exception {
73+
AbstractLineClientException exception = new AbstractLineClientException(response, "test message");
74+
assertThat(exception.getRequestUrl()).isEqualTo(new URL("https://example.com/"));
75+
}
76+
77+
@Test
78+
void getRequestId() {
79+
AbstractLineClientException exception = new AbstractLineClientException(response, "test message");
80+
assertThat(exception.getRequestId()).isEqualTo("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
81+
}
82+
83+
@Test
84+
void getHeader() {
85+
AbstractLineClientException exception = new AbstractLineClientException(response, "test message");
86+
assertThat(exception.getHeader("x-line-example")).isEqualTo("headerValue");
87+
}
88+
}

0 commit comments

Comments
 (0)