Skip to content

Commit

Permalink
Stubby client improvements (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
azagniotov authored Jan 18, 2022
1 parent 448eb2d commit a1a341b
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 64 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#### 7.5.2-SNAPSHOT `master` branch
#### 7.5.3-SNAPSHOT `master` branch

#### 7.5.2

* PR [#453](https://github.com/azagniotov/stubby4j/pull/453) - Revisited `StubbyClient` when starting without a local YAML config (https://github.com/azagniotov)

#### 7.5.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.azagniotov.stubby4j.cli.ANSITerminal;
import io.github.azagniotov.stubby4j.client.StubbyClient;
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import io.github.azagniotov.stubby4j.yaml.YamlBuilder;
import org.eclipse.jetty.http.HttpStatus;
Expand All @@ -21,9 +22,9 @@

public class AdminPortalTest {

private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.azagniotov.stubby4j.cli.ANSITerminal;
import io.github.azagniotov.stubby4j.client.StubbyClient;
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import io.github.azagniotov.stubby4j.yaml.YamlBuilder;
import org.eclipse.jetty.http.HttpMethod;
Expand All @@ -30,9 +31,9 @@

public class ProxyConfigWithStubsTest {

private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String STUBS_URL = String.format("http://localhost:%s", STUBS_PORT);
private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.azagniotov.stubby4j.client.StubbyClient;
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.common.Common;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import io.github.azagniotov.stubby4j.yaml.YamlBuilder;
import org.eclipse.jetty.http.HttpStatus;
Expand All @@ -26,9 +27,9 @@

public class StubsAdminPortalsTest {

private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String STUBS_URL = String.format("http://localhost:%s", STUBS_PORT);
private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.azagniotov.stubby4j.client.StubbyClient;
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.server.ssl.SslUtils;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down Expand Up @@ -35,9 +36,9 @@
public class StubsPortalHttp11OverTlsTests {

private static final StubbyClient STUBBY_CLIENT = new StubbyClient();
private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);
private static final String STUBS_SSL_URL = String.format("https://localhost:%s", STUBS_SSL_PORT);
Expand All @@ -52,7 +53,7 @@ public static void beforeClass() throws Exception {

ANSITerminal.muteConsole(true);

final URL url = StubsPortalTest.class.getResource("/yaml/main-test-stubs.yaml");
final URL url = StubsPortalHttp11OverTlsTests.class.getResource("/yaml/main-test-stubs.yaml");
assert url != null;

final InputStream stubsDataInputStream = url.openStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.server.JettyFactory;
import io.github.azagniotov.stubby4j.utils.FileUtils;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.websocket.api.Session;
Expand Down Expand Up @@ -58,9 +59,9 @@
public class StubsPortalHttp11WebSocketOverTlsTests {

private static final StubbyClient STUBBY_CLIENT = new StubbyClient();
private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);
private static final String WEBSOCKET_SSL_ROOT_PATH_URL = String.format("wss://localhost:%s/ws", STUBS_SSL_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.server.JettyFactory;
import io.github.azagniotov.stubby4j.utils.FileUtils;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.websocket.api.Session;
Expand Down Expand Up @@ -55,9 +56,9 @@
public class StubsPortalHttp11WebSocketTests {

private static final StubbyClient STUBBY_CLIENT = new StubbyClient();
private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);
private static final String WEBSOCKET_ROOT_PATH_URL = String.format("ws://localhost:%s/ws", STUBS_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.azagniotov.stubby4j.client.StubbyClient;
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.server.JettyFactory;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
Expand Down Expand Up @@ -47,9 +48,9 @@
public class StubsPortalHttp20ClearTextTests {

private static final StubbyClient STUBBY_CLIENT = new StubbyClient();
private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);

Expand All @@ -61,7 +62,7 @@ public static void beforeClass() throws Exception {

ANSITerminal.muteConsole(true);

final URL url = StubsPortalTest.class.getResource("/yaml/main-test-stubs.yaml");
final URL url = StubsPortalHttp20ClearTextTests.class.getResource("/yaml/main-test-stubs.yaml");
assert url != null;

final InputStream stubsDataInputStream = url.openStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.server.JettyFactory;
import io.github.azagniotov.stubby4j.server.ssl.SslUtils;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
Expand Down Expand Up @@ -54,9 +55,9 @@
public class StubsPortalHttp20OverTlsWithAlpnProtocolTests {

private static final StubbyClient STUBBY_CLIENT = new StubbyClient();
private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);

Expand All @@ -68,7 +69,7 @@ public static void beforeClass() throws Exception {

ANSITerminal.muteConsole(true);

final URL url = StubsPortalTest.class.getResource("/yaml/main-test-stubs.yaml");
final URL url = StubsPortalHttp20OverTlsWithAlpnProtocolTests.class.getResource("/yaml/main-test-stubs.yaml");
assert url != null;

final InputStream stubsDataInputStream = url.openStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.azagniotov.stubby4j.client.StubbyClient;
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.http.HttpMethodExtended;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.After;
Expand All @@ -27,9 +28,9 @@

public class StubsPortalRaisedIssueTests {

private static final int STUBS_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String STUBS_URL = String.format("http://localhost:%s", STUBS_PORT);
private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.github.azagniotov.stubby4j.client.StubbyResponse;
import io.github.azagniotov.stubby4j.common.Common;
import io.github.azagniotov.stubby4j.utils.FileUtils;
import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.After;
Expand Down Expand Up @@ -38,8 +39,8 @@ public class StubsPortalTest {
// 1. recordable response behavior (the `response` `body` key value starts with http://..... )
// 2. redirect response behavior (the `location` header is set on the `response` )
private static final int STUBS_PORT = 5892;
private static final int STUBS_SSL_PORT = PortTestUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = PortTestUtils.findAvailableTcpPort();
private static final int STUBS_SSL_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final String STUBS_URL = String.format("http://localhost:%s", STUBS_PORT);
private static final String ADMIN_URL = String.format("http://localhost:%s", ADMIN_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class StubbyClientTest {
@BeforeClass
public static void beforeClass() throws Exception {
final URL url = StubbyClientTest.class.getResource(MAIN_TEST_STUBS_YAML);
assert url != null;
STUBBY_CLIENT.startJetty(JettyFactory.DEFAULT_STUBS_PORT, SSL_PORT, JettyFactory.DEFAULT_ADMIN_PORT, url.getFile());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.azagniotov.stubby4j.client;

import io.github.azagniotov.stubby4j.utils.NetworkPortUtils;
import io.github.azagniotov.stubby4j.utils.StringUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.InputStream;

import static com.google.common.truth.Truth.assertThat;


public class StubbyClientYamlessTest {

private static final String ADDRESS_TO_BIND = "127.0.0.1";
private static final int STUBS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int STUBS_TLS_PORT = NetworkPortUtils.findAvailableTcpPort();
private static final int ADMIN_PORT = NetworkPortUtils.findAvailableTcpPort();

private static final StubbyClient STUBBY_CLIENT = new StubbyClient();

@BeforeClass
public static void beforeClass() throws Exception {

// For example, passing additional command line args. But, they are not needed for this specific test
final String[] additionalFlags = new String[]{"--debug"};

final InputStream resourceAsStream = StubbyClientYamlessTest.class.getResourceAsStream("/yaml/standalone-stub.yaml");
final String stubsYamlConfigurationData = StringUtils.inputStreamToString(resourceAsStream);

STUBBY_CLIENT.startJettyYamless(stubsYamlConfigurationData,
STUBS_PORT,
STUBS_TLS_PORT,
ADMIN_PORT,
ADDRESS_TO_BIND,
additionalFlags);
}

@AfterClass
public static void afterClass() throws Exception {
STUBBY_CLIENT.stopJetty();
}

@Test
public void shouldStartStubby4jUsingStubbyClientByCallingYamlessAPI() throws Exception {
final String uri = "/standalone/stub/uri";
final StubbyResponse stubbyResponse = STUBBY_CLIENT.doGetOverSsl(ADDRESS_TO_BIND, uri, STUBS_TLS_PORT);

assertThat(stubbyResponse.body()).isEqualTo("This is working!");
assertThat(stubbyResponse.statusCode()).isEqualTo(HttpStatus.OK_200);
}
}
9 changes: 9 additions & 0 deletions src/functional-test/resources/yaml/standalone-stub.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- request:
method: GET
url: /standalone/stub/uri

response:
status: 200
body: This is working!
headers:
content-type: application/json
Loading

0 comments on commit a1a341b

Please sign in to comment.