Skip to content

Commit

Permalink
Add support for custom HTTP status codes (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
pasichenko committed Apr 18, 2024
1 parent ef7581a commit 596f9b3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;

/**
Expand All @@ -36,6 +36,7 @@
*
* @author chad jaros
* @author Olga Maciaszek-Sharma
* @author Maksym Pasichenko
*/
public class ResponseEntityDecoder implements Decoder {

Expand Down Expand Up @@ -84,7 +85,7 @@ private <T> ResponseEntity<T> createResponse(Object instance, Response response)
headers.put(key, new LinkedList<>(response.headers().get(key)));
}

return new ResponseEntity<>((T) instance, headers, HttpStatus.valueOf(response.status()));
return new ResponseEntity<>((T) instance, headers, HttpStatusCode.valueOf(response.status()));
}

}
Expand Up @@ -32,7 +32,7 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.HttpMessageConverterExtractor;
Expand All @@ -42,6 +42,7 @@
/**
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
* @author Maksym Pasichenko
*/
public class SpringDecoder implements Decoder {

Expand Down Expand Up @@ -82,8 +83,8 @@ private FeignResponseAdapter(Response response) {
}

@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(response.status());
public HttpStatusCode getStatusCode() {
return HttpStatusCode.valueOf(response.status());
}

@Override
Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
Expand All @@ -49,6 +50,7 @@
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
* @author Szymon Linowski
* @author Maksym Pasichenko
*/
@SpringBootTest(classes = SpringDecoderIntegrationTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT,
value = { "spring.application.name=springdecodertest", "spring.jmx.enabled=false" })
Expand Down Expand Up @@ -152,11 +154,21 @@ void testResponseEntityHeaders() {
assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
}

@Test
// Issue: https://github.com/spring-cloud/spring-cloud-openfeign/issues/1010
void testResponseEntityWithCustomHttpStatusCode() {
ResponseEntity<Hello> response = testClient().getCustomHttpStatusCodeResponse();
assertThat(response.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(245));
}

protected interface TestClient {

@GetMapping("/helloresponse")
ResponseEntity<Hello> getHelloResponse();

@GetMapping("/hellocustomhttpstatuscode")
ResponseEntity<Hello> getCustomHttpStatusCodeResponse();

@GetMapping("/hellovoid")
ResponseEntity<Void> getHelloVoid();

Expand Down Expand Up @@ -229,6 +241,11 @@ public ResponseEntity<Hello> getHelloResponse() {
return ResponseEntity.ok(new Hello("hello world via response"));
}

@Override
public ResponseEntity<Hello> getCustomHttpStatusCodeResponse() {
return ResponseEntity.status(245).body(new Hello("hello world via response with custom HTTP status code"));
}

@Override
public ResponseEntity<Void> getHelloVoid() {
return ResponseEntity.noContent().header("X-test-header", "myval").build();
Expand Down

0 comments on commit 596f9b3

Please sign in to comment.