|
1 | 1 | /*
|
2 |
| - * Copyright 2014-2019 the original author or authors. |
| 2 | + * Copyright 2014-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.util.Collections;
|
20 | 20 |
|
| 21 | +import org.hamcrest.BaseMatcher; |
| 22 | +import org.hamcrest.Description; |
21 | 23 | import org.junit.Test;
|
22 | 24 |
|
23 | 25 | import org.springframework.http.HttpHeaders;
|
|
27 | 29 | import org.springframework.restdocs.operation.OperationResponse;
|
28 | 30 | import org.springframework.test.web.reactive.server.ExchangeResult;
|
29 | 31 | import org.springframework.test.web.reactive.server.WebTestClient;
|
| 32 | +import org.springframework.util.ReflectionUtils; |
30 | 33 | import org.springframework.web.reactive.function.server.RouterFunctions;
|
31 | 34 | import org.springframework.web.reactive.function.server.ServerResponse;
|
32 | 35 |
|
33 | 36 | import static org.assertj.core.api.Assertions.assertThat;
|
| 37 | +import static org.junit.Assume.assumeThat; |
34 | 38 | import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
35 | 39 |
|
36 | 40 | /**
|
@@ -72,4 +76,42 @@ public void responseWithCookie() {
|
72 | 76 | Collections.singletonList("name=value; Domain=localhost; HttpOnly"));
|
73 | 77 | }
|
74 | 78 |
|
| 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 | + |
75 | 117 | }
|
0 commit comments