valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.94k stars 1.76k forks source link

Question: How to mock client disconnections in tests? #1868

Closed grivera64 closed 2 months ago

grivera64 commented 2 months ago

How do you mock a client disconnection in tests? I am trying to write a test for a handler that uses ctx.SetBodyStreamWriter(sw) to write a response to the client.

Currently, I have tried ctx.CloseBodyStream(), but this seems to cause issues when reading the Response programmatically. My next thought was to use ctx.Hijack(...), but from my understanding, Hijack overrides default functionality over interrupting an existing running connection.

Thanks in advance for the help!

erikdubbelboer commented 2 months ago

There is no proper API for something like this. When I want to mock client disconnects, I always put a simple proxy in between. This way I can close the connection in that proxy, and simulate a real disconnect more accurately.