Adds Support for webflux ProxyExchange to use body in delete method

Fixes gh-2103
Fixes gh-2132
This commit is contained in:
Bruno Silva
2021-01-28 02:30:37 -03:00
committed by spencergibb
parent 24273cc83d
commit ee9c18c2c4
2 changed files with 46 additions and 2 deletions

View File

@@ -287,8 +287,8 @@ public class ProxyExchange<T> {
}
public Mono<ResponseEntity<T>> delete() {
RequestEntity<Void> requestEntity = headers(
(BodyBuilder) RequestEntity.delete(uri)).build();
RequestEntity<Object> requestEntity = headers(
(BodyBuilder) RequestEntity.delete(uri)).body(body());
return exchange(requestEntity);
}

View File

@@ -39,7 +39,9 @@ import org.springframework.cloud.gateway.webflux.ProductionConfigurationTests.Te
import org.springframework.cloud.gateway.webflux.ProductionConfigurationTests.TestApplication.Bar;
import org.springframework.cloud.gateway.webflux.ProductionConfigurationTests.TestApplication.Foo;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
@@ -47,6 +49,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@@ -225,6 +228,24 @@ public class ProductionConfigurationTests {
assertThat(headers.get("forwarded").get(0)).isEqualTo("host=localhost:" + port);
}
@Test
public void deleteWithoutBody() throws Exception {
ResponseEntity<Void> deleteResponse = rest.exchange("/proxy/{id}/no-body", HttpMethod.DELETE, null, Void.TYPE,
Collections.singletonMap("id", "123"));
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@Test
public void deleteWithBody() throws Exception {
Foo foo = new Foo("to-be-deleted");
ParameterizedTypeReference<Map<String, Foo>> returnType = new ParameterizedTypeReference<Map<String, Foo>>() {
};
ResponseEntity<Map<String, Foo>> deleteResponse = rest.exchange("/proxy/{id}", HttpMethod.DELETE,
new HttpEntity<Foo>(foo), returnType, Collections.singletonMap("id", "123"));
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(deleteResponse.getBody().get("deleted")).isEqualToComparingFieldByField(foo);
}
@SpringBootApplication
static class TestApplication {
@@ -360,6 +381,19 @@ public class ProductionConfigurationTests {
.forward(this::first);
}
@DeleteMapping("/proxy/{id}/no-body")
public Mono<ResponseEntity<Object>> deleteWithoutBody(@PathVariable Integer id, ProxyExchange<Object> proxy)
throws Exception {
return proxy.uri(home.toString() + "/foos/" + id + "/no-body").delete();
}
@DeleteMapping("/proxy/{id}")
public Mono<ResponseEntity<Object>> deleteWithBody(@PathVariable Integer id, @RequestBody Foo foo,
ProxyExchange<Object> proxy) throws Exception {
return proxy.uri(home.toString() + "/foos/" + id).body(foo).delete(response -> ResponseEntity
.status(response.getStatusCode()).headers(response.getHeaders()).body(response.getBody()));
}
}
@RestController
@@ -391,6 +425,16 @@ public class ProductionConfigurationTests {
return headers;
}
@DeleteMapping("/foos/{id}/no-body")
public ResponseEntity<?> deleteFoo(@PathVariable Integer id) {
return ResponseEntity.ok().build();
}
@DeleteMapping("/foos/{id}")
public ResponseEntity<?> deleteFoo(@PathVariable Integer id, @RequestBody Foo foo) {
return ResponseEntity.ok().body(Collections.singletonMap("deleted", foo));
}
}
@JsonIgnoreProperties(ignoreUnknown = true)