检查在Go单元测试中是否已将标头分配给请求。

huangapple go评论121阅读模式
英文:

Check if the header has been assigned to the request in Go unit-testing

问题

我正在尝试测试以下代码行:

  1. httpReq.Header.Set("Content-Type", "application/json")

我以这种方式模拟对外部 API 的请求:

  1. httpmock.RegisterResponder(http.MethodPost, "do-not-exist.com",
  2. httpmock.NewStringResponder(http.StatusOK, `{
  3. "data":{"Random": "Stuff"}}`),
  4. )

我想测试一下是否请求到 API 的头部是否包含我分配的头部。有没有办法可以实现这个?

英文:

I am trying to test the following line of code:

  1. httpReq.Header.Set("Content-Type", "application/json")

I am mocking the request to an external api in this way:

  1. httpmock.RegisterResponder(http.MethodPost, "do-not-exist.com",
  2. httpmock.NewStringResponder(http.StatusOK, `{
  3. "data":{"Random": "Stuff"}}`),
  4. )

And want to test if the request to the api has the header that I assigned. Is there a way I could achieve this?

答案1

得分: 2

通过@Kelsnare的评论帮助,我能够以以下方式解决这个问题:

  1. httpmock.RegisterResponder(http.MethodPost, "do-not-exist.com",
  2. func(req *http.Request) (*http.Response, error) {
  3. require.Equal(t, req.Header.Get("Content-Type"), "application/json")
  4. resp, _ := httpmock.NewStringResponder(http.StatusOK, `{
  5. "data":{"Random": "Stuff"}}`)(req)
  6. return resp, nil},
  7. )

我编写了自己的http.Responder类型的func,并在该func中使用了httpmock.NewStringResponder

英文:

With the help of the comment by @Kelsnare I was able to solve this issue in the following way:

  1. httpmock.RegisterResponder(http.MethodPost, "do-not-exist.com",
  2. func(req *http.Request) (*http.Response, error) {
  3. require.Equal(t, req.Header.Get("Content-Type"), "application/json")
  4. resp, _ := httpmock.NewStringResponder(http.StatusOK, `{
  5. "data":{"Random": "Stuff"}}`)(req)
  6. return resp, nil},
  7. )

I wrote my own func of http.Responder type and used httpmock.NewStringResponder inside that func.

答案2

得分: 0

response_test.go展示了如何测试头部信息:

  1. response, err := NewJsonResponse(200, test.body)
  2. if err != nil {
  3. t.Errorf("#%d NewJsonResponse 失败: %s", i, err)
  4. continue
  5. }
  6. if response.StatusCode != 200 {
  7. t.Errorf("#%d 响应状态不匹配: %d ≠ 200", i, response.StatusCode)
  8. continue
  9. }
  10. if response.Header.Get("Content-Type") != "application/json" {
  11. t.Errorf("#%d 响应 Content-Type 不匹配: %s ≠ application/json",
  12. i, response.Header.Get("Content-Type"))
  13. continue
  14. }

你可以在这里看到一个使用httpmock.RegisterResponder表驱动测试的示例

英文:

response_test.go illustrates how the header is tested:

  1. response, err := NewJsonResponse(200, test.body)
  2. if err != nil {
  3. t.Errorf("#%d NewJsonResponse failed: %s", i, err)
  4. continue
  5. }
  6. if response.StatusCode != 200 {
  7. t.Errorf("#%d response status mismatch: %d 200", i, response.StatusCode)
  8. continue
  9. }
  10. if response.Header.Get("Content-Type") != "application/json" {
  11. t.Errorf("#%d response Content-Type mismatch: %s application/json",
  12. i, response.Header.Get("Content-Type"))
  13. continue

You can see an example of table-driven test with httpmock.RegisterResponder here.

huangapple
  • 本文由 发表于 2021年10月6日 11:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/69459575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定