dongzi1209 2017-07-25 07:07
浏览 127
已采纳

使用httptest测试fasthttp

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?

  • 写回答

1条回答 默认 最新

  • dongzhidian3538 2017-07-25 08:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器