Skip to content

Commit 37c77f3

Browse files
committed
Tolerate responses with non-standard status codes with WebTestClient
Closes gh-847
1 parent 53811b7 commit 37c77f3

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

config/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
<suppressions>
66
<suppress files="com[\\/]example[\\/]restassured" checks="IllegalImport" />
77
<suppress files="src[\\/]testFixtures" checks="JavadocPackage" />
8+
<suppress files="WebTestClientResponseConverterTests\.java" checks="IllegalImport" />
89
</suppressions>

spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,10 +36,19 @@ class WebTestClientResponseConverter implements ResponseConverter<ExchangeResult
3636

3737
@Override
3838
public OperationResponse convert(ExchangeResult result) {
39-
return new OperationResponseFactory().create(result.getStatus().value(), extractHeaders(result),
39+
return new OperationResponseFactory().create(extractStatus(result), extractHeaders(result),
4040
result.getResponseBodyContent());
4141
}
4242

43+
private int extractStatus(ExchangeResult result) {
44+
try {
45+
return (int) result.getClass().getMethod("getRawStatusCode").invoke(result);
46+
}
47+
catch (Throwable ex) {
48+
return result.getStatus().value();
49+
}
50+
}
51+
4352
private HttpHeaders extractHeaders(ExchangeResult result) {
4453
HttpHeaders headers = result.getResponseHeaders();
4554
if (result.getResponseCookies().isEmpty() || headers.containsKey(HttpHeaders.SET_COOKIE)) {

spring-restdocs-webtestclient/src/test/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverterTests.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import java.util.Collections;
2020

21+
import org.hamcrest.BaseMatcher;
22+
import org.hamcrest.Description;
2123
import org.junit.Test;
2224

2325
import org.springframework.http.HttpHeaders;
@@ -27,10 +29,12 @@
2729
import org.springframework.restdocs.operation.OperationResponse;
2830
import org.springframework.test.web.reactive.server.ExchangeResult;
2931
import org.springframework.test.web.reactive.server.WebTestClient;
32+
import org.springframework.util.ReflectionUtils;
3033
import org.springframework.web.reactive.function.server.RouterFunctions;
3134
import org.springframework.web.reactive.function.server.ServerResponse;
3235

3336
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.junit.Assume.assumeThat;
3438
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
3539

3640
/**
@@ -72,4 +76,42 @@ public void responseWithCookie() {
7276
Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
7377
}
7478

79+
@Test
80+
public void responseWithNonStandardStatusCode() {
81+
assumeThat(ExchangeResult.class, hasMethod("getRawStatusCode"));
82+
ExchangeResult result = WebTestClient
83+
.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.status(210).build()))
84+
.configureClient().baseUrl("http://localhost").build().get().uri("/foo").exchange().expectBody()
85+
.returnResult();
86+
OperationResponse response = this.converter.convert(result);
87+
assertThat(response.getStatusCode()).isEqualTo(210);
88+
}
89+
90+
private HasMethodMatcher hasMethod(String name) {
91+
return new HasMethodMatcher(name);
92+
}
93+
94+
private static final class HasMethodMatcher extends BaseMatcher<Class<?>> {
95+
96+
private final String methodName;
97+
98+
private HasMethodMatcher(String methodName) {
99+
this.methodName = methodName;
100+
}
101+
102+
@Override
103+
public boolean matches(Object item) {
104+
if (!(item instanceof Class)) {
105+
return false;
106+
}
107+
return ReflectionUtils.findMethod((Class<?>) item, this.methodName) != null;
108+
}
109+
110+
@Override
111+
public void describeTo(Description description) {
112+
description.appendText("method '" + this.methodName + "' to exist on class");
113+
}
114+
115+
}
116+
75117
}

0 commit comments

Comments
 (0)