Tolerate responses with non-standard status codes with WebTestClient

Closes gh-847
This commit is contained in:
Andy Wilkinson
2022-10-12 10:50:40 +01:00
parent 53811b777f
commit 37c77f3285
3 changed files with 55 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 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.
@@ -36,10 +36,19 @@ class WebTestClientResponseConverter implements ResponseConverter<ExchangeResult
@Override
public OperationResponse convert(ExchangeResult result) {
return new OperationResponseFactory().create(result.getStatus().value(), extractHeaders(result),
return new OperationResponseFactory().create(extractStatus(result), extractHeaders(result),
result.getResponseBodyContent());
}
private int extractStatus(ExchangeResult result) {
try {
return (int) result.getClass().getMethod("getRawStatusCode").invoke(result);
}
catch (Throwable ex) {
return result.getStatus().value();
}
}
private HttpHeaders extractHeaders(ExchangeResult result) {
HttpHeaders headers = result.getResponseHeaders();
if (result.getResponseCookies().isEmpty() || headers.containsKey(HttpHeaders.SET_COOKIE)) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 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.
@@ -18,6 +18,8 @@ package org.springframework.restdocs.webtestclient;
import java.util.Collections;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
@@ -27,10 +29,12 @@ import org.springframework.http.ResponseCookie;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.test.web.reactive.server.ExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeThat;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
/**
@@ -72,4 +76,42 @@ public class WebTestClientResponseConverterTests {
Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
}
@Test
public void responseWithNonStandardStatusCode() {
assumeThat(ExchangeResult.class, hasMethod("getRawStatusCode"));
ExchangeResult result = WebTestClient
.bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.status(210).build()))
.configureClient().baseUrl("http://localhost").build().get().uri("/foo").exchange().expectBody()
.returnResult();
OperationResponse response = this.converter.convert(result);
assertThat(response.getStatusCode()).isEqualTo(210);
}
private HasMethodMatcher hasMethod(String name) {
return new HasMethodMatcher(name);
}
private static final class HasMethodMatcher extends BaseMatcher<Class<?>> {
private final String methodName;
private HasMethodMatcher(String methodName) {
this.methodName = methodName;
}
@Override
public boolean matches(Object item) {
if (!(item instanceof Class)) {
return false;
}
return ReflectionUtils.findMethod((Class<?>) item, this.methodName) != null;
}
@Override
public void describeTo(Description description) {
description.appendText("method '" + this.methodName + "' to exist on class");
}
}
}