模拟测试中的连接丢失

How can I simulate (for purpose of unit tests) loss of network connection or redirect a request anywhere else?

I have some hardcoded adresses in class definitions :( and I would like to test script's behavior where address is, for example, unreachable.

I can modify etc/hosts (I am on win7), but I would like deactivate / modify connection more than one time during execution of test suite. Also, modyfiyng it before and after every test is not practical.

So, I need to set up a proxy for php on windows, a proxy capable to accept commands from running script (to switch redirection on/off) and be able to have redirect rules based on called url.

Sounds like possible plugin to Fiddler, but I didn't found any :(

The problem is there are a number of things going on here. You're basically wanting to change DNS settings while the app is running...

You need a custom DNS server Set Windows to look at the DNS Server

While testing you have to log on to the DNS server, change the name resolution to a non working IP, and flush your dns in windows (ipconfig /flushdns).

Either that, or can you not just implement a firewall which blocks connections a certain address?

You want http://en.wikipedia.org/wiki/Mock_object for this. It now depends on your app is structured to see if you can actually use somethting like this:)


Edit: If you really cant use mock objects, try using them again:) If you still cant, i used this (bat script) at some point in the past to test how a program reacts when the connection drops. I used ping to simulate a sleep and devcon to control the network settings. It aint covering all your needs, but it might give a starting point

:loop
    devcon disable *VEN_1969*
    ping -n 5 localhost
    devcon restart *VEN_1969*
    ping -n 5 localhost
    devcon enable *VEN_1969*
    ping -n 5 localhost
# rem goto loop