Skip to content

Commit

Permalink
added more coverage for kerberos http client
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Al committed Sep 5, 2024
1 parent 739a9ac commit eb98607
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ public SplitHttpResponse get(URI uri, FetchOptions options, Map<String, List<Str

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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<String>> 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 {
Expand Down Expand Up @@ -164,23 +191,6 @@ public Map<String, List<String>> 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 {
Expand Down Expand Up @@ -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<String, List<String>> 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)
Expand Down

0 comments on commit eb98607

Please sign in to comment.