diff --git a/core/src/main/java/feign/FeignException.java b/core/src/main/java/feign/FeignException.java index 5d182f407..de2fcdd64 100644 --- a/core/src/main/java/feign/FeignException.java +++ b/core/src/main/java/feign/FeignException.java @@ -450,11 +450,11 @@ private static String getResponseBodyPreview(byte[] body, Charset charset) { private static Charset getResponseCharset(Map> headers) { Collection 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; diff --git a/core/src/test/java/feign/FeignExceptionTest.java b/core/src/test/java/feign/FeignExceptionTest.java index b761b7735..81b16c847 100644 --- a/core/src/test/java/feign/FeignExceptionTest.java +++ b/core/src/test/java/feign/FeignExceptionTest.java @@ -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 @@ -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 { @@ -58,6 +58,58 @@ public void canCreateWithRequestOnly() { assertThat(exception.request()).isNotNull(); } + @Test + public void createFeignExceptionWithCorrectCharsetResponse() { + Map> 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> 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());