BAEL-5294 added form-url-encoded data code (#13545)

This commit is contained in:
Kumar Prabhash Anand 2023-02-27 18:24:20 +01:00 committed by GitHub
parent b161aeec27
commit 075540af10
3 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.core.client;
import java.util.Map;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baeldung.core.defaulterrorhandling.model.FormData;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
@FeignClient(name = "form-client", url = "http://localhost:8085/api", configuration = FormFeignEncoderConfig.class)
public interface FormClient {
@PostMapping(value = "/form", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
void postFormData(@RequestBody FormData data);
@PostMapping(value = "/form/map", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
void postFormMapData(Map<String, ?> data);
}
class FormFeignEncoderConfig {
@Bean
public Encoder encoder(ObjectFactory<HttpMessageConverters> converters) {
return new SpringFormEncoder(new SpringEncoder(converters));
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.core.defaulterrorhandling.model;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class FormData {
int id;
String name;
}

View File

@ -0,0 +1,78 @@
package com.baeldung.core.client;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.baeldung.core.defaulterrorhandling.model.FormData;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import lombok.extern.slf4j.Slf4j;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Slf4j
class FormClientUnitTest {
private static WireMockServer wireMockServer;
@Autowired
FormClient formClient;
@BeforeAll
public static void startWireMockServer() {
wireMockServer = new WireMockServer(8085);
configureFor("localhost", 8085);
wireMockServer.start();
}
@AfterAll
public static void stopWireMockServer() {
wireMockServer.stop();
}
@Test
public void givenFormData_whenPostFormDataCalled_thenReturnSuccess() {
FormData formData = new FormData(1, "baeldung");
stubFor(WireMock.post(urlEqualTo("/api/form"))
.willReturn(aResponse().withStatus(HttpStatus.OK.value())));
formClient.postFormData(formData);
wireMockServer.verify(postRequestedFor(urlPathEqualTo("/api/form"))
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded; charset=UTF-8"))
.withRequestBody(equalTo("name=baeldung&id=1")));
}
@Test
public void givenFormMap_whenPostFormMapDataCalled_thenReturnSuccess() {
Map<String, String> mapData = new HashMap<>();
mapData.put("name", "baeldung");
mapData.put("id", "1");
stubFor(WireMock.post(urlEqualTo("/api/form/map"))
.willReturn(aResponse().withStatus(HttpStatus.OK.value())));
formClient.postFormMapData(mapData);
wireMockServer.verify(postRequestedFor(urlPathEqualTo("/api/form/map"))
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded; charset=UTF-8"))
.withRequestBody(equalTo("name=baeldung&id=1")));
}
}