2

I am wondering how I can test an application that's written with fasthttp using the httptest package in the base library of Go.

I found this guide which explains the testing pretty well, but the issue is that httptest does not satisfy the http.Handler interface so I have no idea how to do the http.HandlerFunc since fasthttp uses it's own fasthttp.ListenAndServe that's incompatible.

Any ideas on how to create a wrapper, or how to otherwise test a fasthttp written library end to end?

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Tigraine
  • 23,358
  • 11
  • 65
  • 110
  • This is the downside to third-party muxes that don't follow the stdlib Handler interface. If the library doesn't include its own equivalent to `httptest`, you're SOL, and most of them don't. – Adrian Jul 25 '17 at 13:30

1 Answers1

5

There are two possible approaches. Unit testing a handler isn't really viable as you would need to create a RequestCtx and stub/mock all necessary fields.

Instead, I would unit test the code that your fasthttp handlers call out to. I would do e2e testing of the actual handlers themselves.

There is an in memory listener implementation that you could use to avoid actually listening on a TCP port or Unix socket. You would initialise the server but serve on this listener instead of on a network connection.

You would then create a HTTP client and call the relevant methods as normal but use this listener as the transport.

If you stub/fake anything that your handlers interact with then you could make this in-memory only with no external dependencies, i.e. like a unit test but it will actually doing a full system test.

Martin Campbell
  • 1,728
  • 10
  • 11
  • Thanks for the recommendation. After looking at GitHub I decided to rewrite the application to go back to standard net/http as there is just more support behind it and the tool integration is also much better. But I am also doing the stubbing/faking on the handlers to simplify testing. Thanks! – Tigraine Jul 26 '17 at 14:03
  • 2
    Could you post a complete example of using an in-memory listener, please. Thanks a lot. I am having a bit of trouble understanding it. Also, do you think you could make it work with `WebSocket`s? It'd save me a lot of trouble if you know the answer to the latter question already. – Mohamed Bana Mar 05 '19 at 16:23