From 0edfa651600661c93fa3f34ea386fa5f6805ef03 Mon Sep 17 00:00:00 2001 From: Pavel Zhur Date: Thu, 3 Nov 2016 21:56:24 +0300 Subject: [PATCH] Fix for https://github.com/OpenFeign/feign-form/issues/12 --- .../src/main/java/feign/form/FormEncoder.java | 2 +- .../src/test/java/feign/form/Server.java | 11 ++++ .../test/java/feign/form/WildCardMapTest.java | 64 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 feign-form/src/test/java/feign/form/WildCardMapTest.java diff --git a/feign-form/src/main/java/feign/form/FormEncoder.java b/feign-form/src/main/java/feign/form/FormEncoder.java index d157c1188..e1f3b5c1b 100644 --- a/feign-form/src/main/java/feign/form/FormEncoder.java +++ b/feign-form/src/main/java/feign/form/FormEncoder.java @@ -100,7 +100,7 @@ public FormEncoder(Encoder delegate) { @Override public void encode(Object object, Type bodyType, RequestTemplate template) { - if (bodyType != MAP_STRING_WILDCARD) { + if (!MAP_STRING_WILDCARD.equals(bodyType)) { deligate.encode(object, bodyType, template); return; } diff --git a/feign-form/src/test/java/feign/form/Server.java b/feign-form/src/test/java/feign/form/Server.java index a0127415a..9990df0d4 100644 --- a/feign-form/src/test/java/feign/form/Server.java +++ b/feign-form/src/test/java/feign/form/Server.java @@ -21,6 +21,7 @@ import static org.springframework.http.HttpStatus.I_AM_A_TEAPOT; import static org.springframework.http.HttpStatus.LOCKED; import static org.springframework.http.HttpStatus.OK; +import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.web.bind.annotation.RequestMethod.POST; @@ -95,4 +96,14 @@ public ResponseEntity queryMap(@RequestParam("filter") List fil } return ResponseEntity.status(status).body(filters.size()); } + + @RequestMapping( + value = "/wild-card-map", + method = POST, + consumes = APPLICATION_FORM_URLENCODED_VALUE) + public ResponseEntity wildCardMap( + @RequestParam("key1") String key1, @RequestParam("key2") String key2) { + HttpStatus status = key1.equals(key2) ? OK : BAD_REQUEST; + return ResponseEntity.status(status).body(null); + } } diff --git a/feign-form/src/test/java/feign/form/WildCardMapTest.java b/feign-form/src/test/java/feign/form/WildCardMapTest.java new file mode 100644 index 000000000..a215685c2 --- /dev/null +++ b/feign-form/src/test/java/feign/form/WildCardMapTest.java @@ -0,0 +1,64 @@ +package feign.form; + +import static feign.Logger.Level.FULL; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; + +import feign.*; +import java.util.HashMap; +import java.util.Map; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = DEFINED_PORT, classes = Server.class) +public class WildCardMapTest { + + private static FormUrlEncodedApi API; + + @BeforeClass + public static void configureClient() { + API = + Feign.builder() + .encoder(new FormEncoder()) + .logger(new Logger.JavaLogger().appendToFile("log.txt")) + .logLevel(FULL) + .target(FormUrlEncodedApi.class, "http://localhost:8080"); + } + + @Test + public void testOk() { + Map param = + new HashMap() { + { + put("key1", "1"); + put("key2", "1"); + } + }; + Response response = API.wildCardMap(param); + Assert.assertEquals(200, response.status()); + } + + @Test + public void testBadRequest() { + Map param = + new HashMap() { + { + put("key1", "1"); + put("key2", "2"); + } + }; + Response response = API.wildCardMap(param); + Assert.assertEquals(400, response.status()); + } + + interface FormUrlEncodedApi { + + @RequestLine("POST /wild-card-map") + @Headers("Content-Type: application/x-www-form-urlencoded") + Response wildCardMap(Map param); + } +}