Tomcat实现java与http_java – 使用嵌入式tomcat服务器的JUnit测试,如何为http和https连接器指定自动端口?...

描述

我做了一个JUnit测试,专注于尝试测试一个SOAP Web服务的调用.

我正在使用嵌入式tomcat服务器进行测试,以便使用模拟服务器运行我的测试.

我也使用http和https连接器.

我需要为这两个连接器使用自动端口,因为该测试正在Jenkins服务器上运行,并且我不能仅仅使用端口443或8443,因为它们已经被占用.

我明白,使用端口0作为标准端口将导致tomcat使用自动端口分配,但是我无法使用它与两个连接器.

预期行为

我也想为自定义的ssl连接器使用自动端口分配.

有可能以某种方式这样做吗?

示例代码

这是我的tomcat实例的代码:

@Before

public void setup() throws Throwable {

File tomcatWorkingDir = new File(mWorkingDir);

//Empty the target/tomcat-working-dir directory if it exist

//Create the directory otherwise

if(tomcatWorkingDir.exists() && tomcatWorkingDir.isDirectory()){

LOGGER.info("cleaning tomcat-working-dir directory");

FileUtils.cleanDirectory(new File(mWorkingDir));

} else {

LOGGER.info("create tomcat-working-dir directory");

tomcatWorkingDir.mkdir();

}

LOGGER.info("disabling ssl certification validation");

//Disable JVM ssl sockets connection

disableJVMCertificate();

//Add server certificate

createServerCertificate();

//Custom SSL Connector

Connector SSLConnector = getSSLConnector();

mTomcat = new Tomcat();

//Standard http startup port

mTomcat.setPort(0);

//Set up base directory

//Otherwise, tomcat would use the current directory

mTomcat.setBaseDir(mWorkingDir);

LOGGER.info("setting the ssl connector in TOMCAT");

Service service = mTomcat.getService();

service.addConnector(SSLConnector);

//Redirect current port

Connector defaultConnector = mTomcat.getConnector();

defaultConnector.setRedirectPort(SERVER_HTTPS_PORT);

//Configure the way WAR are managed by the engine

mTomcat.getHost().setAutoDeploy(true);

mTomcat.getHost().setDeployOnStartup(true);

//Add mock server into our webApp

String servletName = "/server";

File webApp = new File(mWorkingDir,"../../../ws-mock-server/src/main/webapp");

mTomcat.addWebapp(mTomcat.getHost(), servletName, webApp.getAbsolutePath());

//start tomcat

LOGGER.info("starting TOMCAT");

mTomcat.start();

}

这里为我的自定义ssl连接器.

private static Connector getSSLConnector(){

Connector connector = new Connector();

connector.setPort(SERVER_HTTPS_PORT);

connector.setSecure(true);

//Http protocol Http11AprProtocol

connector.setAttribute("protocol", "org.apache.coyote.http11.Http11AprProtocol");

//Maximum threads allowedd on this instance of tomcat

connector.setAttribute("maxThreads","200");

connector.setAttribute("SSLEnabled", true);

//No client Authentification is required in order to connect

connector.setAttribute("clientAuth", false);

//SSL TLSv1 protocol

connector.setAttribute("sslProtocol","TLS");

//Ciphers configuration describing how server will encrypt his messages

//A common cipher suite need to exist between server and client in an ssl

//communication in order for the handshake to succeed

connector.setAttribute("ciphers","TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA");

LOGGER.info("setting keystore file");

//Here an absolute file path is needed in order to properly set up the keystore attribute

connector.setAttribute("keystoreFile",new File(".").getAbsolutePath().replace("\\", "/")+"/"+mWorkingDir+"/server.jks");

LOGGER.info("setting keystore pass");

connector.setAttribute("keystorePass","changeit");

return connector;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值