Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug for FeignException cannot get the correct charset (#1325) #1345

Merged
merged 18 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
a5b0a33
Fix bug for FeignException cannot get the correct charset (#1325)
Linda-pan Dec 29, 2020
349e530
Merge branch 'master' into master
kdavisk6 Dec 29, 2020
209f6fc
Add test for (Fix bug for FeignException cannot get the correct chars…
Linda-pan Dec 30, 2020
b428bcc
Merge remote-tracking branch 'origin/master'
Linda-pan Dec 30, 2020
1d3a41c
Add more test for (Fix bug for FeignException cannot get the correct …
Linda-pan Dec 30, 2020
db0ff87
Format test for (Fix bug for FeignException cannot get the correct ch…
Linda-pan Jan 4, 2021
033d89f
Merge branch 'master' into master
Linda-pan Jan 7, 2021
ee92aa3
Merge branch 'master' into master
Linda-pan Jan 14, 2021
b8a6b65
Fix bug for FeignException cannot get the correct charset (#1325)
Linda-pan Dec 29, 2020
4fdc495
Add test for (Fix bug for FeignException cannot get the correct chars…
Linda-pan Dec 30, 2020
93f63aa
Add more test for (Fix bug for FeignException cannot get the correct …
Linda-pan Dec 30, 2020
35c802e
Format test for (Fix bug for FeignException cannot get the correct ch…
Linda-pan Jan 4, 2021
4c1e01e
Add test for (Fix bug for FeignException cannot get the correct chars…
Linda-pan Dec 30, 2020
ac315eb
Add more test for (Fix bug for FeignException cannot get the correct …
Linda-pan Dec 30, 2020
7f2e7c9
Format test for (Fix bug for FeignException cannot get the correct ch…
Linda-pan Jan 4, 2021
0cc7b96
Merge remote-tracking branch 'origin/master'
Linda-pan Jan 18, 2021
c7229a8
Merge branch 'master' into master
Linda-pan Feb 20, 2021
a3fcf37
Correcting License Headers for 2021
kdavisk6 Mar 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,11 @@ private static String getResponseBodyPreview(byte[] body, Charset charset) {
private static Charset getResponseCharset(Map<String, Collection<String>> headers) {

Collection<String> strings = headers.get("content-type");
if (strings == null || strings.size() == 0) {
if (strings == null || strings.isEmpty()) {
return null;
}

Pattern pattern = Pattern.compile("charset=([^\\s])");
Pattern pattern = Pattern.compile(".*charset=([^\\s|^;]+).*");
Matcher matcher = pattern.matcher(strings.iterator().next());
if (!matcher.lookingAt()) {
return null;
Expand Down
56 changes: 54 additions & 2 deletions core/src/test/java/feign/FeignExceptionTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2020 The Feign Authors
* Copyright 2012-2021 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
Expand All @@ -16,7 +16,7 @@
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.*;
import static org.assertj.core.api.Assertions.assertThat;

public class FeignExceptionTest {
Expand Down Expand Up @@ -58,6 +58,58 @@ public void canCreateWithRequestOnly() {
assertThat(exception.request()).isNotNull();
}

@Test
public void createFeignExceptionWithCorrectCharsetResponse() {
Map<String, Collection<String>> map = new HashMap<>();
map.put("connection", new ArrayList<>(Collections.singletonList("keep-alive")));
map.put("content-length", new ArrayList<>(Collections.singletonList("100")));
map.put("content-type",
new ArrayList<>(Collections.singletonList("application/json;charset=UTF-16BE")));

Request request = Request.create(Request.HttpMethod.GET,
"/home", Collections.emptyMap(),
"data".getBytes(StandardCharsets.UTF_16BE),
StandardCharsets.UTF_16BE,
null);

Response response = Response.builder()
.status(400)
.body("response".getBytes(StandardCharsets.UTF_16BE))
.headers(map)
.request(request)
.build();

FeignException exception = FeignException.errorStatus("methodKey", response);
assertThat(exception.getMessage())
.isEqualTo("[400] during [GET] to [/home] [methodKey]: [response]");
}

@Test
public void createFeignExceptionWithErrorCharsetResponse() {
Map<String, Collection<String>> map = new HashMap<>();
map.put("connection", new ArrayList<>(Collections.singletonList("keep-alive")));
map.put("content-length", new ArrayList<>(Collections.singletonList("100")));
map.put("content-type",
new ArrayList<>(Collections.singletonList("application/json;charset=UTF-8")));

Request request = Request.create(Request.HttpMethod.GET,
"/home", Collections.emptyMap(),
"data".getBytes(StandardCharsets.UTF_16BE),
StandardCharsets.UTF_16BE,
null);

Response response = Response.builder()
.status(400)
.body("response".getBytes(StandardCharsets.UTF_16BE))
.headers(map)
.request(request)
.build();

FeignException exception = FeignException.errorStatus("methodKey", response);
assertThat(exception.getMessage())
.isNotEqualTo("[400] during [GET] to [/home] [methodKey]: [response]");
}

@Test(expected = NullPointerException.class)
public void nullRequestShouldThrowNPEwThrowable() {
new Derived(404, "message", null, new Throwable());
Expand Down