-1

I have functional tests that test a system that makes HTTP connections to some internal mock HTTP servers. By mock I mean they are real servers, just implemented by myself to simulate actual 3rd party servers. They are in java, using jetty.

So far to test for when these HTTP servers are down, I have been stopping the mock servers before starting the test, and starting them back up. This works fine but it is now getting in the way of making the tests run in parallel.

The mock HTTP servers reply differently or don't reply, based on the request.

My question is, is there a way to make the mock HTTP servers refuse some connections, for example based on the request?

I guess this is specific to Jetty, but I'm happy using another library if that's needed.

Ramin Arabbagheri
  • 790
  • 2
  • 11
  • 25

2 Answers2

1

You cannot refuse a connection based on stream data from an accepted connection.

The real questions are:

  • why are you using mocking?
  • why are you using only 1 instance of the server?

Start a real embedded server, per parallel test unit, stop it after that unit is complete. (a "parallel test unit" is whatever slicing the parallel testing uses, be it by the test method, the test case, the test suite, etc..)

Make the real server use a dynamically bound port (set ServerConnector.setPort(0)), and then use the Server.getURI() to know the base server URI (complete with scheme, host, port, and default context path).

How to run jetty server for java junit testing

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • So there are multiple environments. These are functional tests (bdd) that run against an application server that sits on an env where all of the third party http servers are replaced by 'local' http servers that reply based on input. The functional tests don't really have a way to spin up a mock server on the application server. – Ramin Arabbagheri Jul 06 '20 at 13:39
1

You cannot refuse a connection from an application.

An easy alternative is to make the client use the wrong port for the test. If you make the client try to connect to say port 12345 it will get a connection refused error.

The connection refused error is generated by the operating system network stack when it finds that no application is listening on the destination port of the connection. But your mock server is listening on its port.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • Got your point re rejecting a connection that is already accepted not being possible. I understand I have to make some architectural changes. – Ramin Arabbagheri Jul 06 '20 at 13:41