diff --git a/client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java b/client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java index b335e233..ef5106e1 100644 --- a/client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java +++ b/client/src/main/java/io/split/service/SplitHttpClientKerberosImpl.java @@ -75,11 +75,9 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map= HttpURLConnection.HTTP_MULT_CHOICE) { @@ -120,11 +118,9 @@ public SplitHttpResponse post(URI url, HttpEntity entity, int responseCode = response.code(); - if (_log.isDebugEnabled()) { - _log.debug(String.format("[GET] %s. Status code: %s", - request.url().toString(), - responseCode)); - } + _log.debug(String.format("[GET] %s. Status code: %s", + request.url().toString(), + responseCode)); String statusMessage = ""; if (responseCode < HttpURLConnection.HTTP_OK || responseCode >= HttpURLConnection.HTTP_MULT_CHOICE) { diff --git a/client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java b/client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java index bf017996..25898e24 100644 --- a/client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java +++ b/client/src/test/java/io/split/service/HttpSplitClientKerberosTest.java @@ -107,8 +107,35 @@ public void testGetWithSpecialCharacters() throws IOException, InterruptedExcept Assert.assertEquals("{\"test\": \"blue\",\"grüne Straße\": 13}", configs.get("on")); Assert.assertEquals("{\"test\": \"blue\",\"size\": 15}", configs.get("off")); Assert.assertEquals(2, split.sets.size()); + splitHttpClientKerberosImpl.close(); } + @Test + public void testGetErrors() throws IOException, InterruptedException { + MockWebServer server = new MockWebServer(); + server.enqueue(new MockResponse().setBody("").setResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR)); + server.start(); + HttpUrl baseUrl = server.url("/v1/"); + URI rootTarget = baseUrl.uri(); + RequestDecorator decorator = new RequestDecorator(null); + OkHttpClient client = new Builder().build(); + + SplitHttpClientKerberosImpl splitHttpClientKerberosImpl = new SplitHttpClientKerberosImpl(client, decorator, "qwerty", metadata()); + + Map> additionalHeaders = Collections.singletonMap("AdditionalHeader", + Collections.singletonList("add")); + + SplitHttpResponse splitHttpResponse = splitHttpClientKerberosImpl.get(rootTarget, + new FetchOptions.Builder().cacheControlHeaders(true).build(), additionalHeaders); + + + RecordedRequest request = server.takeRequest(); + server.shutdown(); + assertThat(splitHttpResponse.statusCode(), is(equalTo(HttpURLConnection.HTTP_INTERNAL_ERROR))); + splitHttpClientKerberosImpl.close(); + } + + @Test public void testGetParameters() throws URISyntaxException, IOException, InterruptedException { class MyCustomHeaders implements CustomHeaderDecorator { @@ -164,23 +191,6 @@ public Map> getHeaderOverrides(RequestContext context) { assertThat(request.getMethod(), is(equalTo("GET"))); } - @Test - public void testGetError() throws URISyntaxException, IOException { - URI uri = new URI("https://api.split.io/splitChanges?since=1234567"); - RequestDecorator decorator = new RequestDecorator(null); - - Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("1.0.0.127", 8080)); - OkHttpClient client = new OkHttpClient.Builder() - .proxy(proxy) - .build(); - try { - SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(client, decorator, "qwerty", metadata()); - SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.get(uri, - new FetchOptions.Builder().cacheControlHeaders(true).build(), null); - } catch (Exception e) { - } - } - @Test(expected = IllegalStateException.class) public void testException() throws URISyntaxException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException { @@ -251,24 +261,28 @@ public void testPost() throws URISyntaxException, IOException, ParseException, I } @Test - public void testPosttError() throws URISyntaxException, IOException { - URI uri = new URI("https://kubernetesturl.com/split/api/testImpressions/bulk"); + public void testPostErrors() throws IOException, InterruptedException { + MockWebServer server = new MockWebServer(); + server.enqueue(new MockResponse().setBody("").setResponseCode(HttpURLConnection.HTTP_INTERNAL_ERROR)); + server.start(); + HttpUrl baseUrl = server.url("/v1/"); + URI rootTarget = baseUrl.uri(); RequestDecorator decorator = new RequestDecorator(null); - ByteArrayOutputStream mockOs = Mockito.mock( ByteArrayOutputStream.class); + OkHttpClient client = new Builder().build(); - Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("1.0.0.127", 8080)); - OkHttpClient client = new OkHttpClient.Builder() - .proxy(proxy) - .build(); - SplitHttpClientKerberosImpl splitHtpClientKerberos = SplitHttpClientKerberosImpl.create(client, decorator, "qwerty", metadata()); - try { - SplitHttpResponse splitHttpResponse = splitHtpClientKerberos.post(uri, - Utils.toJsonEntity(Arrays.asList(new String[] { "A", "B", "C", "D" })), null); + SplitHttpClientKerberosImpl splitHttpClientKerberosImpl = new SplitHttpClientKerberosImpl(client, decorator, "qwerty", metadata()); - Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, (long) splitHttpResponse.statusCode()); - } catch (Exception e) { - } + Map> additionalHeaders = Collections.singletonMap("AdditionalHeader", + Collections.singletonList("add")); + + SplitHttpResponse splitHttpResponse = splitHttpClientKerberosImpl.post(rootTarget, + Utils.toJsonEntity("<>"), additionalHeaders); + + RecordedRequest request = server.takeRequest(); + server.shutdown(); + assertThat(splitHttpResponse.statusCode(), is(equalTo(HttpURLConnection.HTTP_INTERNAL_ERROR))); + splitHttpClientKerberosImpl.close(); } @Test(expected = IllegalStateException.class)