diff --git a/endpoints/citrus-http/src/main/java/org/citrusframework/http/client/HttpResponseErrorHandler.java b/endpoints/citrus-http/src/main/java/org/citrusframework/http/client/HttpResponseErrorHandler.java index c6c3cd2ce2..a3a8245971 100644 --- a/endpoints/citrus-http/src/main/java/org/citrusframework/http/client/HttpResponseErrorHandler.java +++ b/endpoints/citrus-http/src/main/java/org/citrusframework/http/client/HttpResponseErrorHandler.java @@ -16,13 +16,17 @@ package org.citrusframework.http.client; -import java.io.IOException; - import org.citrusframework.exceptions.CitrusRuntimeException; import org.citrusframework.message.ErrorHandlingStrategy; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatusCode; import org.springframework.http.client.ClientHttpResponse; +import org.springframework.lang.Nullable; import org.springframework.web.client.DefaultResponseErrorHandler; +import java.io.IOException; +import java.net.URI; + /** * @since 2.7 */ @@ -32,17 +36,15 @@ public class HttpResponseErrorHandler extends DefaultResponseErrorHandler { /** * Default constructor using error handling strategy. - * @param errorHandlingStrategy */ public HttpResponseErrorHandler(ErrorHandlingStrategy errorHandlingStrategy) { this.errorHandlingStrategy = errorHandlingStrategy; } @Override - public void handleError(ClientHttpResponse response) throws IOException { + protected void handleError(ClientHttpResponse response, HttpStatusCode statusCode, @Nullable URI url, @Nullable HttpMethod method) throws IOException { if (errorHandlingStrategy.equals(ErrorHandlingStrategy.PROPAGATE)) { - throw new HttpErrorPropagatingException(response.getStatusCode(), response.getStatusText(), - response.getHeaders(), getResponseBody(response), getCharset(response)); + throw new HttpErrorPropagatingException(response.getStatusCode(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } else if (errorHandlingStrategy.equals(ErrorHandlingStrategy.THROWS_EXCEPTION)) { super.handleError(response); } else { diff --git a/endpoints/citrus-http/src/test/java/org/citrusframework/http/server/HttpServerTest.java b/endpoints/citrus-http/src/test/java/org/citrusframework/http/server/HttpServerTest.java index 2edf9b11f2..ae420b1800 100644 --- a/endpoints/citrus-http/src/test/java/org/citrusframework/http/server/HttpServerTest.java +++ b/endpoints/citrus-http/src/test/java/org/citrusframework/http/server/HttpServerTest.java @@ -16,8 +16,7 @@ package org.citrusframework.http.server; -import java.util.Random; - +import org.apache.hc.core5.http.ContentType; import org.citrusframework.context.SpringBeanReferenceResolver; import org.citrusframework.context.TestContext; import org.citrusframework.endpoint.EndpointAdapter; @@ -29,7 +28,6 @@ import org.citrusframework.message.MessageHeaders; import org.citrusframework.testng.AbstractTestNGUnitTest; import org.citrusframework.util.SocketUtils; -import org.apache.hc.core5.http.ContentType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -39,10 +37,16 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import java.util.Random; + +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; +import static org.testng.Assert.fail; /** * Simple unit test for HttpServer @@ -79,7 +83,7 @@ public void shutdown() { try { client.send(new HttpMessage().method(HttpMethod.GET), context); - Assert.fail("Server supposed to be in shutdown state, but was accessible via client request"); + fail("Server supposed to be in shutdown state, but was accessible via client request"); } catch (ResourceAccessException e) { Assert.assertTrue(e.getMessage().contains("Connection refused")); } @@ -224,4 +228,26 @@ public void testCustomContentType() { verify(mockResponseEndpointAdapter).handleMessage(any(Message.class)); } + @Test + public void testHttpResponseErrorHandlerWithPropagateStrategy() { + TestContext context = testContextFactory.getObject(); + + reset(mockResponseEndpointAdapter); + doReturn(new HttpMessage().status(INTERNAL_SERVER_ERROR)) + .when(mockResponseEndpointAdapter).handleMessage(any(Message.class)); + + client.send(new HttpMessage() + .path("/hello") + .method(HttpMethod.GET), context); + + Message response = client.receive(context); + + assertThat(response) + .isInstanceOf(HttpMessage.class) + .satisfies( + r -> assertThat(((HttpMessage) r).getStatusCode()).isEqualTo(INTERNAL_SERVER_ERROR) + ); + + verify(mockResponseEndpointAdapter).handleMessage(any(Message.class)); + } } diff --git a/pom.xml b/pom.xml index b2f765442c..e947c96a9c 100644 --- a/pom.xml +++ b/pom.xml @@ -195,13 +195,13 @@ 1.8.0 3.26.3 4.2.2 - 2.27.19 - 1.78.1 - 1.15.7 + 2.29.21 + 1.79 + 1.15.10 2.12.0 1.9.0 1.17.1 - 2.17.0 + 2.18.0 3.17.0 1.3.4 3.11.1 @@ -209,12 +209,12 @@ 3.4.0 4.2.28 1.2.0 - 3.0.22 - 2.1.0 + 3.0.23 + 2.1.1 3.0 4.13.0 5.4.1 - 2.7.3 + 2.7.4 2.18.1 2.1.3 2.0.1 @@ -233,7 +233,7 @@ 2.0.0 0.1.55 2.9.0 - 1.5.2 + 1.5.4 2.5.1 r938 5.11.3 @@ -242,7 +242,7 @@ 2.10.1 6.13.4 6.13.4 - 3.8.0 + 3.9.0 6.13.4 2.22.1 5.14.2 @@ -250,20 +250,20 @@ 4.1.105.Final 4.12.0 4.7.6 - 42.7.3 + 42.7.4 3.0.4 - 4.25.0 + 4.27.0 2.0.11 1.1.10.7 2.3 - 6.1.14 + 6.2.0 4.0.11 - 6.3.5 - 3.0.2 + 6.4.0 + 3.0.3 2.14.0 1.6.9 2.1.22 - 1.20.1 + 1.20.4 7.10.2 4.5.10 1.6.3 @@ -271,7 +271,7 @@ 5.2.1 2.6.0 2.12.2 - 1.4.20 + 1.4.21 3.9.3 false diff --git a/tools/restdocs/src/main/java/org/citrusframework/restdocs/http/CachedBodyHttpRequest.java b/tools/restdocs/src/main/java/org/citrusframework/restdocs/http/CachedBodyHttpRequest.java index cbf5956691..b63b68d283 100644 --- a/tools/restdocs/src/main/java/org/citrusframework/restdocs/http/CachedBodyHttpRequest.java +++ b/tools/restdocs/src/main/java/org/citrusframework/restdocs/http/CachedBodyHttpRequest.java @@ -21,6 +21,7 @@ import org.springframework.http.HttpRequest; import java.net.URI; +import java.util.Map; /** * Helper Http request wrapper implementation provides access to the request body for usage @@ -42,6 +43,11 @@ public byte[] getBody() { return body; } + @Override + public HttpHeaders getHeaders() { + return delegate.getHeaders(); + } + @Override public HttpMethod getMethod() { return delegate.getMethod(); @@ -53,7 +59,7 @@ public URI getURI() { } @Override - public HttpHeaders getHeaders() { - return delegate.getHeaders(); + public Map getAttributes() { + return delegate.getAttributes(); } } diff --git a/tools/restdocs/src/main/java/org/citrusframework/restdocs/http/RestDocConfiguredHttpRequest.java b/tools/restdocs/src/main/java/org/citrusframework/restdocs/http/RestDocConfiguredHttpRequest.java index 0e844a1501..7b5f030969 100644 --- a/tools/restdocs/src/main/java/org/citrusframework/restdocs/http/RestDocConfiguredHttpRequest.java +++ b/tools/restdocs/src/main/java/org/citrusframework/restdocs/http/RestDocConfiguredHttpRequest.java @@ -69,6 +69,11 @@ public HttpRequest getRequest() { return delegate; } + @Override + public HttpHeaders getHeaders() { + return delegate.getHeaders(); + } + @Override public HttpMethod getMethod() { return delegate.getMethod(); @@ -80,7 +85,7 @@ public URI getURI() { } @Override - public HttpHeaders getHeaders() { - return delegate.getHeaders(); + public Map getAttributes() { + return delegate.getAttributes(); } } diff --git a/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/AbstractHttpRequestTest.java b/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/AbstractHttpRequestTest.java new file mode 100644 index 0000000000..b24dc7f608 --- /dev/null +++ b/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/AbstractHttpRequestTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.restdocs.http; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpRequest; +import org.testng.annotations.Test; + +import java.net.URI; +import java.util.HashMap; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +public abstract class AbstractHttpRequestTest { + + protected F fixture; + + protected abstract HttpRequest getDelegate(); + + @Test + public void getHeadersReturnsHeaders() { + var httpHeaders = mock(HttpHeaders.class); + doReturn(httpHeaders).when(getDelegate()).getHeaders(); + + assertThat(fixture.getHeaders()) + .isEqualTo(httpHeaders); + } + + @Test + public void getMethodReturnsMethod() { + var httpMethod = mock(HttpMethod.class); + doReturn(httpMethod).when(getDelegate()).getMethod(); + + assertThat(fixture.getMethod()) + .isEqualTo(httpMethod); + } + + @Test + public void getURIReturnsURI() { + var uri = mock(URI.class); + doReturn(uri).when(getDelegate()).getURI(); + + assertThat(fixture.getURI()) + .isEqualTo(uri); + } + + @Test + public void getAttributesReturnsAttributes() { + var attributes = new HashMap(); + doReturn(attributes).when(getDelegate()).getAttributes(); + + assertThat(fixture.getAttributes()) + .isEqualTo(attributes); + } +} diff --git a/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/CachedBodyHttpRequestTest.java b/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/CachedBodyHttpRequestTest.java new file mode 100644 index 0000000000..9cb9d02b44 --- /dev/null +++ b/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/CachedBodyHttpRequestTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.restdocs.http; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpRequest; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CachedBodyHttpRequestTest extends AbstractHttpRequestTest { + + private static final byte[] BODY = "foo".getBytes(); + + @Mock + private HttpRequest delegate; + + private AutoCloseable openedMocks; + + @Override + protected HttpRequest getDelegate() { + return delegate; + } + + @BeforeMethod + public void beforeMethodSetup() { + openedMocks = MockitoAnnotations.openMocks(this); + + fixture = new CachedBodyHttpRequest(delegate, BODY); + } + + @AfterMethod + public void afterMethodTeardown() throws Exception { + openedMocks.close(); + } + + @Test + public void getBodyReturnsBody() { + assertThat(fixture.getBody()) + .isEqualTo(BODY); + } +} diff --git a/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/RestDocConfiguredHttpRequestTest.java b/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/RestDocConfiguredHttpRequestTest.java new file mode 100644 index 0000000000..e77ee20812 --- /dev/null +++ b/tools/restdocs/src/test/java/org/citrusframework/restdocs/http/RestDocConfiguredHttpRequestTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.citrusframework.restdocs.http; + +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpRequest; +import org.springframework.restdocs.RestDocumentationContext; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RestDocConfiguredHttpRequestTest extends AbstractHttpRequestTest { + + @Mock + private HttpRequest delegate; + + @Mock + private RestDocumentationContext restDocumentationContext; + + private Map configuration; + + private AutoCloseable openedMocks; + + @Override + protected HttpRequest getDelegate() { + return delegate; + } + + @BeforeMethod + public void beforeMethodSetup() { + openedMocks = MockitoAnnotations.openMocks(this); + configuration = new HashMap<>(); + + fixture = new RestDocConfiguredHttpRequest(delegate, restDocumentationContext, configuration); + } + + @AfterMethod + public void afterMethodTeardown() throws Exception { + openedMocks.close(); + } + + @Test + public void getContextReturnsContext() { + assertThat(fixture.getContext()) + .isEqualTo(restDocumentationContext); + } + + @Test + public void getConfigurationReturnsConfiguration() { + assertThat(fixture.getConfiguration()) + .isEqualTo(configuration); + } + + @Test + public void getRequestReturnsDelegate() { + assertThat(fixture.getRequest()) + .isEqualTo(delegate); + } +}