duanhong8839
2017-08-14 20:12
浏览 505
已采纳

httptest.NewRequest与http.NewRequest:在测试中使用哪个,为什么?

Golang has these two similar libs http and httptest and they both have the NewRequest func.

Why do we even need the httptest.NewRequest if http.NewRequest does it all?

If I need to create a multipart/multiform request for my tests, which one do I need to use?

图片转代码服务由CSDN问答提供 功能建议

Golang具有这两个类似的库,分别是 http httptest 它们都具有 NewRequest 功能。

如果 http.NewRequest 全部完成,为什么我们甚至需要 httptest.NewRequest

如果我需要为测试创建多部分/多形式请求,我需要使用哪一个?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongshai1944 2017-08-14 20:27
    最佳回答

    As indicated in the documentation, httptest.NewRequest "returns a new incoming server Request, suitable for passing to an http.Handler for testing", while http.NewRequest "returns a Request suitable for use with Client.Do or Transport.RoundTrip." So, if you're passing the request directly to the handler in a unit test, use httptest.NewRequest, and if you're executing a full round-trip using http.Client, use http.NewRequest.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题