用例

我正在研究企业级支付应用程序(用JAVA编写).我希望模拟对银行进行的HTTP POST调用的延迟.这将允许我模拟可能发生的不同延迟/不可用场景.

代码

以下代码将请求发送给银行:


try {
    // Set the location of the Bank Of America payment gateway
    URL url = new URL(getParameterGatewayUrl());

    // Open the connection
    urlConnection = url.openConnection();

    // Set the connection timeout
    urlConnection.setConnectTimeout(getTimeoutSeconds() * 1000);

    // Set the DoOutput flag to true because we intend
    // to use the URL connection for output
    urlConnection.setDoOutput(true);

    // Send the transaction via HTTPS POST
    OutputStream outputStream = urlConnection.getOutputStream();
    outputStream.write(postVars.getBytes());
    outputStream.close();
} catch (TimeoutException exception){

}

try {
    //Set the read timeout
    urlConnection.setReadTimeout(getTimeoutSeconds() * 1000);

    // Get the response from Bank Of America
    InputStream inputStream = urlConnection.getInputStream();
    while ((inputStreamCharacter = inputStream.read()) != -1)
        responseText.append((char) inputStreamCharacter);

    inputStream.close();
    LOG.debug("Bank Of America responseText: " + responseText);
} catch (SocketTimeoutException exception){

}

这段代码在异步付款任务中运行.

脚本

这是发生的事情:

我们连接到银行的支付网关
我们向银行发送付款申请.
我们等待银行向我们发送回复我们的请求.
我们收到回复并解析并检查成功.

现在,我们希望在请求发送到银行之后,即在我们收到银行的回复之前模拟延迟.这样在第二个try / catch块中引发了一个SocketTimeoutException异常.

问题

我需要处理的应用程序实例托管在运行14.04.1-Ubuntu的服务器VM上.我过去在Windows托管的应用程序上使用过Fiddlerintroduce latency.但棘手的是,Fiddler是一个基于UI的程序,我无法在Linux shell上使用它.
此外,我们没有从银行得到太多帮助.否则,如果在服务器端而不是在客户端模拟这一切,那将会容易得多.

我用Google搜索并且无法为此找到解决方案.有没有人尝试过这些方法?如果是这样我们怎么做呢?欢迎大家提出意见.

解决方法:

我已经能够找到一种解决方法来测试它.我从this answer那里得到了帮助.虽然httpbin是一个了不起的项目,但它缺少延迟POST请求响应的能力.因此,我分叉了他们的存储库,并自己添加了所需的端点. fork适用于任何需要它的人.

现在,可以简单地将网关URL更改为基于带有/ post / delay /的httpbin的URL,结果将生成延迟的POST请求响应.

标签: linux, fiddler, latency, http-post, simulate

相关文章推荐

添加新评论,含*的栏目为必填