简体   繁体   English

如果服务器关闭,模拟空响应

[英]Simulate empty response if server is down

I have 2 mockservices returning success and failure responses based on request body.我有 2 个模拟服务根据请求正文返回成功和失败响应。 Now I need to simulate an empty response if the server is down or unavailable.现在我需要在服务器关闭或不可用时模拟空响应。

How can this be done in Java? Java怎么能做到呢?

You could set the timeout of your client socket to a very short value to simulate timing out trying to connect to the server.您可以将客户端套接字的超时设置为非常短的值,以模拟尝试连接到服务器的超时。

Here's a question showing how to set a socket timeout .这是一个显示如何设置套接字超时的问题 Most HTTP clients let you provide config properties for the underlying TCP socket.大多数 HTTP 客户端允许您为底层 TCP 套接字提供配置属性。

There are also test utilities to help with this, such as WebMock, explained in this question .还有一些测试实用程序可以帮助解决这个问题,例如这个问题中解释的 WebMock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM