|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 The Feign Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | +package feign.fastjson2; |
| 15 | + |
| 16 | +import static feign.Util.UTF_8; |
| 17 | +import static feign.assertj.FeignAssertions.assertThat; |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.LinkedList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import com.alibaba.fastjson2.TypeReference; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import feign.Request; |
| 31 | +import feign.RequestTemplate; |
| 32 | +import feign.Response; |
| 33 | +import feign.Util; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author changjin wei(魏昌进) |
| 37 | + */ |
| 38 | +@SuppressWarnings("deprecation") |
| 39 | +class FastJsonCodecTest { |
| 40 | + |
| 41 | + private String zonesJson = ""// |
| 42 | + + "[" + System.lineSeparator() // |
| 43 | + + " {" + System.lineSeparator() // |
| 44 | + + " \"name\": \"denominator.io.\"" + System.lineSeparator()// |
| 45 | + + " }," + System.lineSeparator()// |
| 46 | + + " {" + System.lineSeparator()// |
| 47 | + + " \"name\": \"denominator.io.\"," + System.lineSeparator()// |
| 48 | + + " \"id\": \"ABCD\"" + System.lineSeparator()// |
| 49 | + + " }" + System.lineSeparator()// |
| 50 | + + "]" + System.lineSeparator(); |
| 51 | + |
| 52 | + @Test |
| 53 | + void encodesMapObjectNumericalValuesAsInteger() { |
| 54 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 55 | + map.put("foo", 1); |
| 56 | + |
| 57 | + RequestTemplate template = new RequestTemplate(); |
| 58 | + new Fastjson2Encoder().encode(map, map.getClass(), template); |
| 59 | + |
| 60 | + assertThat(template).hasBody(""// |
| 61 | + + "{" // |
| 62 | + + "\"foo\":1" // |
| 63 | + + "}"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void encodesFormParams() { |
| 68 | + Map<String, Object> form = new LinkedHashMap<>(); |
| 69 | + form.put("foo", 1); |
| 70 | + form.put("bar", Arrays.asList(2, 3)); |
| 71 | + |
| 72 | + RequestTemplate template = new RequestTemplate(); |
| 73 | + new Fastjson2Encoder().encode(form, new TypeReference<Map<String, ?>>() {}.getType(), template); |
| 74 | + |
| 75 | + assertThat(template).hasBody(""// |
| 76 | + + "{" // |
| 77 | + + "\"foo\":1,"// |
| 78 | + + "\"bar\":[2,3]"// |
| 79 | + + "}"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void decodes() throws Exception { |
| 84 | + List<Zone> zones = new LinkedList<>(); |
| 85 | + zones.add(new Zone("denominator.io.")); |
| 86 | + zones.add(new Zone("denominator.io.", "ABCD")); |
| 87 | + |
| 88 | + Response response = Response.builder() |
| 89 | + .status(200) |
| 90 | + .reason("OK") |
| 91 | + .request(Request.create(Request.HttpMethod.GET, "/api", Collections.emptyMap(), null, |
| 92 | + Util.UTF_8)) |
| 93 | + .headers(Collections.emptyMap()) |
| 94 | + .body(zonesJson, UTF_8) |
| 95 | + .build(); |
| 96 | + assertThat( |
| 97 | + new Fastjson2Decoder().decode(response, new TypeReference<List<Zone>>() {}.getType())) |
| 98 | + .isEqualTo(zones); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void nullBodyDecodesToNull() throws Exception { |
| 103 | + Response response = Response.builder() |
| 104 | + .status(204) |
| 105 | + .reason("OK") |
| 106 | + .request(Request.create(Request.HttpMethod.GET, "/api", Collections.emptyMap(), null, |
| 107 | + Util.UTF_8)) |
| 108 | + .headers(Collections.emptyMap()) |
| 109 | + .build(); |
| 110 | + assertThat(new Fastjson2Decoder().decode(response, String.class)).isNull(); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void emptyBodyDecodesToNull() throws Exception { |
| 115 | + Response response = Response.builder() |
| 116 | + .status(204) |
| 117 | + .reason("OK") |
| 118 | + .request(Request.create(Request.HttpMethod.GET, "/api", Collections.emptyMap(), null, |
| 119 | + Util.UTF_8)) |
| 120 | + .headers(Collections.emptyMap()) |
| 121 | + .body(new byte[0]) |
| 122 | + .build(); |
| 123 | + assertThat(new Fastjson2Decoder().decode(response, String.class)).isNull(); |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + @Test |
| 128 | + void decoderCharset() throws IOException { |
| 129 | + Zone zone = new Zone("denominator.io.", "ÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÑ"); |
| 130 | + |
| 131 | + Map<String, Collection<String>> headers = new HashMap<>(); |
| 132 | + headers.put("Content-Type", Arrays.asList("application/json;charset=ISO-8859-1")); |
| 133 | + |
| 134 | + Response response = Response.builder() |
| 135 | + .status(200) |
| 136 | + .reason("OK") |
| 137 | + .request(Request.create(Request.HttpMethod.GET, "/api", Collections.emptyMap(), null, |
| 138 | + Util.UTF_8)) |
| 139 | + .headers(headers) |
| 140 | + .body(new String("" // |
| 141 | + + "{" |
| 142 | + + " \"name\" : \"DENOMINATOR.IO.\"," |
| 143 | + + " \"id\" : \"ÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÑ\"" |
| 144 | + + "}").getBytes(StandardCharsets.ISO_8859_1)) |
| 145 | + .build(); |
| 146 | + assertThat( |
| 147 | + ((Zone) new Fastjson2Decoder().decode(response, new TypeReference<Zone>() {}.getType()))) |
| 148 | + .containsEntry("id", zone.get("id")); |
| 149 | + } |
| 150 | + |
| 151 | + static class Zone extends LinkedHashMap<String, Object> { |
| 152 | + |
| 153 | + private static final long serialVersionUID = 1L; |
| 154 | + |
| 155 | + Zone() { |
| 156 | + // for reflective instantiation. |
| 157 | + } |
| 158 | + |
| 159 | + Zone(String name) { |
| 160 | + this(name, null); |
| 161 | + } |
| 162 | + |
| 163 | + Zone(String name, String id) { |
| 164 | + put("name", name); |
| 165 | + if (id != null) { |
| 166 | + put("id", id); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + /** Enabled via {@link feign.Feign.Builder#dismiss404()} */ |
| 174 | + @Test |
| 175 | + void notFoundDecodesToEmpty() throws Exception { |
| 176 | + Response response = Response.builder() |
| 177 | + .status(404) |
| 178 | + .reason("NOT FOUND") |
| 179 | + .request(Request.create(Request.HttpMethod.GET, "/api", Collections.emptyMap(), null, |
| 180 | + Util.UTF_8)) |
| 181 | + .headers(Collections.emptyMap()) |
| 182 | + .build(); |
| 183 | + assertThat((byte[]) new Fastjson2Decoder().decode(response, byte[].class)).isEmpty(); |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | +} |
0 commit comments