From 37c77f32859af542c28d80ef614344a409589655 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 12 Oct 2022 10:50:40 +0100 Subject: [PATCH] Tolerate responses with non-standard status codes with WebTestClient Closes gh-847 --- config/checkstyle/checkstyle-suppressions.xml | 1 + .../WebTestClientResponseConverter.java | 13 +++++- .../WebTestClientResponseConverterTests.java | 44 ++++++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/config/checkstyle/checkstyle-suppressions.xml b/config/checkstyle/checkstyle-suppressions.xml index 85f240e2..6898a954 100644 --- a/config/checkstyle/checkstyle-suppressions.xml +++ b/config/checkstyle/checkstyle-suppressions.xml @@ -5,4 +5,5 @@ + diff --git a/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java b/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java index df0a6084..c5eae390 100644 --- a/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java +++ b/spring-restdocs-webtestclient/src/main/java/org/springframework/restdocs/webtestclient/WebTestClientResponseConverter.java @@ -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 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> { + + 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"); + } + + } + }