本文整理汇总了Java中org.mockserver.socket.PortFactory类的典型用法代码示例。如果您正苦于以下问题:Java PortFactory类的具体用法?Java PortFactory怎么用?Java PortFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PortFactory类属于org.mockserver.socket包,在下文中一共展示了PortFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startJetty
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@BeforeClass
public static void startJetty() throws Exception {
port = PortFactory.findFreePort();
server = new Server();
SslSocketConnector sslSocketConnector = new SslSocketConnector();
sslSocketConnector.setKeystore("src/test/resources/keystore");
sslSocketConnector.setKeyPassword("password");
sslSocketConnector.setPort(port);
server.setConnectors(new Connector[]{sslSocketConnector});
server.addHandler(new AbstractHandler() {
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
response.setStatus(Response.Status.OK.getStatusCode());
Request base_request = request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest();
base_request.setHandled(true);
}
});
server.start();
}
示例2: shouldVerifyReceivedRequests
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Test
public void shouldVerifyReceivedRequests() throws Exception {
// given
Future<HttpResponse> responseSettableFuture =
httpClient.sendRequest(
request()
.withPath("/some_path")
.withHeader(HOST.toString(), "localhost:" + PortFactory.findFreePort()),
new InetSocketAddress(clientAndServer.getLocalPort())
);
// then
assertThat(responseSettableFuture.get(10, TimeUnit.SECONDS).getStatusCode(), is(404));
// then
clientAndServer.verify(request()
.withPath("/some_path"));
clientAndServer.verify(request()
.withPath("/some_path"), VerificationTimes.exactly(1));
}
示例3: setupFixture
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Before
public void setupFixture() {
httpPort = PortFactory.findFreePort();
mockServerRuleDynamicPorts = new MockServerRule(this);
mockServerRuleSinglePort = new MockServerRule(this, false, httpPort);
mockServerRuleMultiplePorts = new MockServerRule(this, false, httpPort, httpPort + 1);
mockServerRulePerSuite = new MockServerRule(this, true, httpPort);
mockServerRulePerSuiteDuplicate = new MockServerRule(this, true, httpPort);
initMocks(this);
when(mockClientAndServer.getLocalPort()).thenReturn(httpPort);
when(mockClientAndServer.getLocalPorts()).thenReturn(Arrays.asList(httpPort + 1, httpPort + 2));
when(clientAndServerFactory.newClientAndServer()).thenReturn(mockClientAndServer);
}
示例4: shouldStartMockServer
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Test
public void shouldStartMockServer() {
// given
final int freePort = PortFactory.findFreePort();
MockServerClient mockServerClient = new MockServerClient("127.0.0.1", freePort);
Level originalLogLevel = ConfigurationProperties.logLevel();
try {
// when
Main.main(
"-serverPort", String.valueOf(freePort),
"-logLevel", "DEBUG"
);
// then
assertThat(mockServerClient.isRunning(), is(true));
assertThat(ConfigurationProperties.logLevel().toString(), is("DEBUG"));
} finally {
ConfigurationProperties.logLevel(originalLogLevel.toString());
mockServerClient.stop();
}
}
示例5: shouldPrintOutUsageForInvalidRemotePort
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Test
public void shouldPrintOutUsageForInvalidRemotePort() throws UnsupportedEncodingException {
// given
final int freePort = PortFactory.findFreePort();
PrintStream originalPrintStream = Main.systemOut;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Main.systemOut = new PrintStream(baos, true, StandardCharsets.UTF_8.name());
// when
Main.main("-serverPort", String.valueOf(freePort), "-proxyRemotePort", "A");
// then
String actual = new String(baos.toByteArray(), StandardCharsets.UTF_8);
String expected = NEW_LINE +
" =======================================================================" + NEW_LINE +
" proxyRemotePort value \"A\" is invalid, please specify a port i.e. \"1080\"" + NEW_LINE +
" =======================================================================" + NEW_LINE +
NEW_LINE +
Main.USAGE;
assertThat(actual, is(expected));
} finally {
Main.systemOut = originalPrintStream;
}
}
示例6: shouldPrintOutUsageForInvalidRemoteHost
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Test
public void shouldPrintOutUsageForInvalidRemoteHost() throws UnsupportedEncodingException {
// given
final int freePort = PortFactory.findFreePort();
PrintStream originalPrintStream = Main.systemOut;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Main.systemOut = new PrintStream(byteArrayOutputStream, true, StandardCharsets.UTF_8.name());
// when
Main.main("-serverPort", String.valueOf(freePort), "-proxyRemoteHost", "%^&*(");
// then
String actual = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
String expected = NEW_LINE +
" ====================================================================================================" + NEW_LINE +
" proxyRemoteHost value \"%^&*(\" is invalid, please specify a host name i.e. \"localhost\" or \"127.0.0.1\"" + NEW_LINE +
" ====================================================================================================" + NEW_LINE +
NEW_LINE +
Main.USAGE;
assertThat(actual, is(expected));
} finally {
Main.systemOut = originalPrintStream;
}
}
示例7: shouldPrintOutUsageForInvalidLogLevel
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Test
public void shouldPrintOutUsageForInvalidLogLevel() throws UnsupportedEncodingException {
// given
final int freePort = PortFactory.findFreePort();
PrintStream originalPrintStream = Main.systemOut;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Main.systemOut = new PrintStream(byteArrayOutputStream, true, StandardCharsets.UTF_8.name());
// when
Main.main("-serverPort", String.valueOf(freePort), "-logLevel", "FOO");
// then
String actual = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
String expected = NEW_LINE +
" =======================================================================================================" + NEW_LINE +
" logLevel value \"FOO\" is invalid, please specify one of \"TRACE\", \"DEBUG\", \"INFO\", \"WARN\", \"ERROR\", \"OFF\"" + NEW_LINE +
" =======================================================================================================" + NEW_LINE +
NEW_LINE +
Main.USAGE;
assertThat(actual, is(expected));
} finally {
Main.systemOut = originalPrintStream;
}
}
示例8: initialize
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@BeforeClass
public static void initialize() throws Exception {
port = PortFactory.findFreePort();
mockServer = startClientAndServer(port);
rsContext = new ResourceSyncContext();
httpclient = HttpClients.createDefault();
}
示例9: startServer
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@BeforeClass
public static void startServer() throws Exception {
int port = PortFactory.findFreePort();
System.out.println("port is " + port);
mockServer = ClientAndServer.startClientAndServer(port);
baseUrl = "http://localhost:" + port;
}
示例10: startProxy
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@BeforeClass
public static void startProxy() throws Exception {
int port = PortFactory.findFreePort();
clientAndProxy = startClientAndProxy(port);
proxy = new HttpHost("localhost", port);
// load truststore with MockServer CA
sslContext = SSLContexts.custom().loadTrustMaterial(new File("src/test/resources/truststore.jks"), "changeit".toCharArray()).build();
httpClientBuilder = HttpClients.custom().setSSLContext(sslContext);
httpclient = httpClientBuilder.build();
serverPort = PortFactory.findFreePort();
clientAndServer = ClientAndServer.startClientAndServer(serverPort);
clientAndServer.when(new HttpRequest().withMethod("GET")).respond(HttpResponse.response("OK"));
}
示例11: setUp
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
creationUrl = new URL("http://master.tus.io");
int port = PortFactory.findFreePort();
mockServerURL = new URL("http://localhost:" + port + "/files");
mockServer = startClientAndServer(port);
}
示例12: startProxy
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@BeforeClass
public static void startProxy() throws Exception {
if (isUsingMockServer()) {
previousFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(SSLFactory.getInstance().sslContext().getSocketFactory());
proxy = ClientAndProxy.startClientAndProxy(PortFactory.findFreePort());
mockServer = ClientAndServer.startClientAndServer(MOCKSERVER_PORT);
}
}
示例13: startServer
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@BeforeClass
public static void startServer() {
mockServerClient = startClientAndServer(0, PortFactory.findFreePort(), 0, PortFactory.findFreePort());
List<Integer> boundPorts = ((ClientAndServer) mockServerClient).getLocalPorts();
severHttpPort = boundPorts.toArray(new Integer[boundPorts.size()]);
echoServer = new EchoServer(false);
}
示例14: shouldNotForwardRequestWithInvalidHostHead
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Test
public void shouldNotForwardRequestWithInvalidHostHead() throws Exception {
// when
Future<HttpResponse> responseSettableFuture =
httpClient.sendRequest(
request()
.withPath("/some_path")
.withHeader(HOST.toString(), "localhost:" + PortFactory.findFreePort()),
new InetSocketAddress(clientAndServer.getLocalPort())
);
// then
assertThat(responseSettableFuture.get(10, TimeUnit.SECONDS).getStatusCode(), is(404));
}
示例15: shouldThrowSocketCommunicationExceptionForConnectException
import org.mockserver.socket.PortFactory; //导入依赖的package包/类
@Test
public void shouldThrowSocketCommunicationExceptionForConnectException() throws Exception {
// then
int freePort = PortFactory.findFreePort();
exception.expect(ExecutionException.class);
exception.expectMessage(anyOf(
containsString("Connection refused: /127.0.0.1:" + freePort),
containsString("Channel closed before valid response")
));
// when
new NettyHttpClient().sendRequest(request().withHeader(HOST.toString(), "127.0.0.1:" + freePort))
.get(10, TimeUnit.SECONDS);
}