Skip to content

Commit

Permalink
Fix bug for FeignException cannot get the correct charset (#1325) (#1345
Browse files Browse the repository at this point in the history
)

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

* Add test for (Fix bug for FeignException cannot get the correct charset) (#1325)

* Add more test for (Fix bug for FeignException cannot get the correct charset) (#1325)

* Format test for (Fix bug for FeignException cannot get the correct charset) (#1325)

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

* Add test for (Fix bug for FeignException cannot get the correct charset) (#1325)

* Add more test for (Fix bug for FeignException cannot get the correct charset) (#1325)

* Format test for (Fix bug for FeignException cannot get the correct charset) (#1325)

* Add test for (Fix bug for FeignException cannot get the correct charset) (#1325)

* Add more test for (Fix bug for FeignException cannot get the correct charset) (#1325)

* Format test for (Fix bug for FeignException cannot get the correct charset) (#1325)

* Correcting License Headers for 2021

Co-authored-by: Kevin Davis <[email protected]>
  • Loading branch information
Linda-pan and kdavisk6 authored Mar 7, 2021
1 parent ee8d517 commit c8fbb85
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
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

0 comments on commit c8fbb85

Please sign in to comment.