Skip to content

Commit

Permalink
rm HttpClientMock
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Jan 1, 2021
1 parent c45fcda commit 4bd940a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 95 deletions.
6 changes: 0 additions & 6 deletions unirest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@
<version>1.7.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.paweladamski</groupId>
<artifactId>HttpClientMock</artifactId>
<version>1.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
91 changes: 14 additions & 77 deletions unirest/src/test/java/BehaviorTests/CustomClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,96 +25,33 @@

package BehaviorTests;

import com.github.paweladamski.httpclientmock.HttpClientMock;

import kong.unirest.Client;
import kong.unirest.HttpRequest;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.apache.ApacheAsyncClient;
import kong.unirest.apache.ApacheClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class CustomClientTest extends BddTest {

private final String url = "http://localhost/getme";
boolean requestConfigUsed = false;


@Override
@AfterEach
public void tearDown() {
super.tearDown();
requestConfigUsed = false;


}

@Test
void settingACustomClient() {
HttpClientMock client = getMockClient();
Client client = mock(Client.class);;

HttpResponse mock = mock(HttpResponse.class);
when(client.request(any(HttpRequest.class),
any(Function.class),
any(Class.class))).thenReturn(mock);
Unirest.config().httpClient(client);

assertMockResult();
}

@Test
void settingACustomClientWithBuilder() {
HttpClientMock client = getMockClient();

Unirest.config().httpClient(ApacheClient.builder(client)
.withRequestConfig((c, w) -> {
requestConfigUsed = true;
return RequestConfig.custom().build();
}));

assertMockResult();

Unirest.config().reset();

assertMockResult();
}

@Test
void canSetACustomAsyncClientWithBuilder() throws Exception {
try(CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().build()) {
client.start();

Unirest.config().asyncClient(ApacheAsyncClient.builder(client)
.withRequestConfig((c, w) -> {
requestConfigUsed = true;
return RequestConfig.custom().build();
})
);

assertAsyncResult();
assertTrue(requestConfigUsed);
}
}

private void assertAsyncResult() throws Exception {
MockServer.setStringResponse("Howdy Ho!");
HttpResponse<String> result = Unirest.get(MockServer.GET).asStringAsync().get();
assertEquals(200, result.getStatus());
assertEquals("Howdy Ho!", result.getBody());
}


private void assertMockResult() {
HttpResponse<String> result = Unirest.get(url).asString();
assertEquals(202, result.getStatus());
assertEquals("Howdy Ho!", result.getBody());
}

private HttpClientMock getMockClient() {
HttpClientMock client = new HttpClientMock();
client.onGet(url).doReturn(202, "Howdy Ho!");
return client;
assertEquals(mock, Unirest.get("http://localhost/getme").asEmpty());
}


Expand Down
12 changes: 0 additions & 12 deletions unirest/src/test/java/BehaviorTests/LifeCycleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package BehaviorTests;

import com.github.paweladamski.httpclientmock.HttpClientMock;
import com.google.common.collect.Sets;
import kong.unirest.*;
import kong.unirest.apache.AsyncIdleConnectionMonitorThread;
Expand Down Expand Up @@ -92,17 +91,6 @@ void canAddShutdownHooks() throws Exception {
assertShutdownHooks(1);
}

@Test
void settingClientAfterClientHasAlreadyBeenSet() {
HttpClientMock httpClientMock = new HttpClientMock();
httpClientMock.onGet("http://localhost/getme").doReturn(202, "Howdy Ho!");
assertEquals(200, Unirest.get(MockServer.GET).asString().getStatus());
Unirest.config().httpClient(httpClientMock);
HttpResponse<String> result = Unirest.get("http://localhost/getme").asString();
assertEquals(202, result.getStatus());
assertEquals("Howdy Ho!", result.getBody());
}

@Test
void willNotShutdownInactiveAsyncClient() throws IOException {
CloseableHttpAsyncClient asyncClient = mock(CloseableHttpAsyncClient.class);
Expand Down
106 changes: 106 additions & 0 deletions unirest/src/test/java/kong/unirest/apache/ApacheClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* The MIT License
*
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package kong.unirest.apache;

import BehaviorTests.BddTest;
import BehaviorTests.MockServer;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ApacheClientTest extends BddTest {
boolean requestConfigUsed = false;
boolean interceptorCalled = false;

@Override
@BeforeEach
public void setUp() {
super.setUp();
}

@Override
@AfterEach
public void tearDown() {
super.tearDown();
requestConfigUsed = false;
}

@Test
void settingACustomClientWithBuilder() {
HttpClient client = HttpClientBuilder.create()
.addInterceptorFirst((HttpRequestInterceptor) (q, w) -> interceptorCalled = true)
.build();

Unirest.config().httpClient(ApacheClient.builder(client)
.withRequestConfig((c, w) -> {
requestConfigUsed = true;
return RequestConfig.custom().build();
}));

assertMockResult();
}

@Test
void canSetACustomAsyncClientWithBuilder() throws Exception {
try(CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().build()) {
client.start();

Unirest.config().asyncClient(ApacheAsyncClient.builder(client)
.withRequestConfig((c, w) -> {
requestConfigUsed = true;
return RequestConfig.custom().build();
})
);

assertAsyncResult();
assertTrue(requestConfigUsed);
}
}

private void assertAsyncResult() throws Exception {
MockServer.setStringResponse("Howdy Ho!");
HttpResponse<String> result = Unirest.get(MockServer.GET).asStringAsync().get();
assertEquals(200, result.getStatus());
assertEquals("Howdy Ho!", result.getBody());
}

private void assertMockResult() {
Unirest.get(MockServer.GET).asString();
assertTrue(requestConfigUsed);
assertTrue(interceptorCalled);
}
}

0 comments on commit 4bd940a

Please sign in to comment.