diff options
author | Bjørn Christian Seime <bjorncs@oath.com> | 2018-06-08 13:47:08 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@oath.com> | 2018-06-08 13:49:51 +0200 |
commit | 3d91147264d908748db400a901e09af773ea7b2c (patch) | |
tree | 5e78f91c70b93e1fe30f220e1c20b8128092867e /vespa-http-client/src/test/java/com | |
parent | 1428af42e5943fbb75497dac5a7f73260bbf5c58 (diff) |
Try to extract error message from non-2xx responses
Diffstat (limited to 'vespa-http-client/src/test/java/com')
-rw-r--r-- | vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java index 3136526fd42..5228d2b66f0 100644 --- a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java +++ b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/communication/ApacheGatewayConnectionTest.java @@ -1,6 +1,9 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.http.client.core.communication; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.yahoo.vespa.http.client.TestUtils; import com.yahoo.vespa.http.client.config.ConnectionParams; import com.yahoo.vespa.http.client.config.Endpoint; @@ -17,7 +20,10 @@ import org.apache.http.StatusLine; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.InputStreamEntity; +import org.apache.http.message.BasicHeader; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.stubbing.Answer; import java.io.ByteArrayInputStream; @@ -25,6 +31,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -45,6 +53,9 @@ import static org.mockito.Mockito.when; public class ApacheGatewayConnectionTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Test public void testProtocolV3() throws Exception { final Endpoint endpoint = Endpoint.create("hostname", 666, false); @@ -292,6 +303,35 @@ public class ApacheGatewayConnectionTest { verify(headerProvider, times(3)).getHeaderValue(); // 1x connect(), 2x writeOperations() } + @Test + public void detailed_error_message_is_extracted_from_error_responses_with_json() throws IOException, ServerResponseException, InterruptedException { + String reasonPhrase = "Unauthorized"; + String errorMessage = "Invalid credentials"; + expectedException.expect(ServerResponseException.class); + expectedException.expectMessage(reasonPhrase + " - " + errorMessage); + + CountDownLatch verifyContentSentLatch = new CountDownLatch(1); + + ApacheGatewayConnection.HttpClientFactory mockFactory = mockHttpClientFactory(post -> { + verifyContentSentLatch.countDown(); + return createErrorHttpResponse(401, reasonPhrase, errorMessage); + }); + + ApacheGatewayConnection apacheGatewayConnection = + new ApacheGatewayConnection( + Endpoint.create("hostname", 666, false), + new FeedParams.Builder().build(), + "", + new ConnectionParams.Builder().build(), + mockFactory, + "clientId"); + apacheGatewayConnection.connect(); + apacheGatewayConnection.handshake(); + + apacheGatewayConnection.writeOperations(Collections.singletonList(createDoc("42", "content", true))); + assertTrue(verifyContentSentLatch.await(10, TimeUnit.SECONDS)); + } + private static ApacheGatewayConnection.HttpClientFactory mockHttpClientFactory(HttpExecuteMock httpExecuteMock) throws IOException { ApacheGatewayConnection.HttpClientFactory mockFactory = mock(ApacheGatewayConnection.HttpClientFactory.class); @@ -355,4 +395,20 @@ public class ApacheGatewayConnectionTest { when(httpEntityMock.getContent()).thenReturn(inputs); return httpResponseMock; } + + private static HttpResponse createErrorHttpResponse(int statusCode, String reasonPhrase, String message) throws IOException { + HttpResponse response = mock(HttpResponse.class); + + StatusLine statusLine = mock(StatusLine.class); + when(statusLine.getStatusCode()).thenReturn(statusCode); + when(statusLine.getReasonPhrase()).thenReturn(reasonPhrase); + when(response.getStatusLine()).thenReturn(statusLine); + + HttpEntity httpEntity = mock(HttpEntity.class); + when(httpEntity.getContentType()).thenReturn(new BasicHeader("Content-Type", "application/json")); + String json = String.format("{\"message\": \"%s\"}", message); + when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(json.getBytes())); + when(response.getEntity()).thenReturn(httpEntity); + return response; + } } |