Go httptest 包在单元测试中使用

发表于:2022-2-15 09:26

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:banjming    来源:稀土掘金

  httptest 方法介绍
  NewRequest
  NewRequest 方法用来创建一个 http 的请求体。
  方法说明:
  func NewRequest(method, target string, body io.Reader) *http.Request

  ·method 参数表示测试的接口的 HTTP 方法。
  · target 参数表示接口定义的路由。
  · body 参数表示请求体。
  NewRecorder(响应体)
  func NewRecorder() *ResponseRecorder

  NewRecorder 方法用来创建 http 的响应体。返回的类型是 *httptest.ResponseRecorder ,包含接口返回信息,等价于 http.ResponseWriter。
  看个 http get 例子:
  // Req: http://localhost:1234/upper?word=abc
  // Res: ABC

  func TestUpperCaseHandler(t *testing.T) {
  req := httptest.NewRequest(http.MethodGet, "/upper?word=abc", nil)
  w := httptest.NewRecorder()
  UpperCaseHandler(w, req)
  res := w.Result()
  defer res.Body.Close()
  data, err := ioutil.ReadAll(res.Body)
  if err != nil {
  t.Errorf("expected error to be nil got %v", err)
  }
  if string(data) != "ABC" {
  t.Errorf("expected ABC got %v", string(data))
  }
  }
  func UpperCaseHandler(w http.ResponseWriter, r *http.Request) {
  query, err := url.ParseQuery(r.URL.RawQuery)
  if err != nil {
  w.WriteHeader(http.StatusBadRequest)
  fmt.Fprintf(w, "invalid request")
  return
  }
  word := query.Get("word")
  if len(word) == 0 {
  w.WriteHeader(http.StatusBadRequest)
  fmt.Fprintf(w, "missing word")
  return
  }
  w.WriteHeader(http.StatusOK)
  fmt.Fprintf(w, strings.ToUpper(word))
  }

  运行结果如下:
  === RUN   TestUpperCaseHandler
  --- PASS: TestUpperCaseHandler (0.00s)
  PASS

  再看一个 post 请求 例子 :
  // 业务代码
  func UpperCaseHandle1(w http.ResponseWriter, r *http.Request) {
  body, _ := ioutil.ReadAll(r.Body)
  fmt.Println(string(body))
  str := string(body)
  mp := make(map[string]string)
  err := json.Unmarshal([]byte(str), &mp)
  if err != nil {
  fmt.Println(err)
  }
  word := mp["params"]
  if len(word) == 0 {
  w.WriteHeader(http.StatusBadRequest)
  fmt.Fprintf(w, "missing word")
  return
  }
  w.WriteHeader(http.StatusOK)
  fmt.Fprintf(w, strings.ToUpper(word))
  }
  func Test_testApi(t *testing.T) {
  tests := []struct {
  name string
  }{
  {
  name: "test api",
  },
  }
  for _, tt := range tests {
  t.Run(tt.name, func(t *testing.T) {
  ts := httptest.NewServer(http.HandlerFunc(UpperCaseHandle1))
  defer ts.Close()
  params := make(map[string]string)
  params["params"] = "paramsBody"
  paramsByte, _ := json.Marshal(params)
  resp, err := http.Post(ts.URL, "application/json", bytes.NewBuffer(paramsByte))
  if err != nil {
  t.Error(err)
  }
  defer resp.Body.Close()
  t.Log(resp.StatusCode)
  if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
  body, _ := ioutil.ReadAll(resp.Body)
  t.Error(string(body))
  }
  })
  }
  }

  执行结果:
  === RUN   Test_testApi
  --- PASS: Test_testApi (208.72s)
  === RUN   Test_testApi/test_api
  {"params":"paramsBody"}
      http_close_resp_test.go:107: 200
      --- PASS: Test_testApi/test_api (208.72s)
  PASS

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号