如何用httprouter编写测试 [英] How to write test with httprouter

查看:102
本文介绍了如何用httprouter编写测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为GoLang编写一个简单的REST服务测试。但是,因为我使用 julienschmidt / httprouter 作为路由库。



main.go

  package main 
$ b func main(){
router:= httprouter.New()
bookController:= controllers.NewBookController()
router.GET(/ book /:id,bookController.GetBook)
http.ListenAndServe(:8080,router)
}

控制器

 包控制器

类型BookController struct {}

func NewBookController * BookController {
return& BookController()
}

func(bc BookController)GetBook(w http.ResponseWriter,r * http .Request,p httprouter.Params){
fmt.Fprintf(w,%s,p)
}

我的问题是:如何测试这个,而GetBook既不是HttpHandler也不是HttpHandle如果我使用传统的处理程序,测试将很容易就像这样

  func TestGetBook(t * testing.T){
req,_:= http.NewRequest ( get,/ book / sampleid,nil)
rr:= httptest.NewRecorder()
handler:= controllers.NewBookController()。GetBook
handler.ServeHTTP(rr,req )
如果status:= rr.code;状态!= http.StatusOK {
t.Errorf(错误状态)
}
}

问题是,httprouter不是handler或handlefunc。所以我现在被卡住了

解决方案

只需为每个测试启动一个新路由器,然后注册处于测试状态的处理程序,测试请求到路由器,而不是处理程序,以便路由器可以解析路径参数并将它们传递给处理程序。

  func TestGetBook(t * testing.T){
handler:= controllers.NewBookController()
router:= httprouter.New()
router.GET(/ book /:id, handler.GetBook)

req,_:= http.NewRequest(GET,/ book / sampleid,nil)
rr:= httptest.NewRecorder()

router.ServeHTTP(rr,req)
如果status:= rr.Code;状态!= http.StatusOK {
t.Errorf(错误状态)
}
}


I am writing tests for a simple REST service in GoLang. But, because I am using julienschmidt/httprouter as the routing library. I am struggling on how to write test.

main.go

package main

func main() {
   router := httprouter.New()
   bookController := controllers.NewBookController()
   router.GET("/book/:id", bookController.GetBook)
   http.ListenAndServe(":8080", router)
}

controllers

package controllers

type BookController struct {}

func NewBookController *BookController {
   return &BookController()
}

func (bc BookController) GetBook(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
   fmt.Fprintf(w,"%s", p)
}

My question is: How can test this while GetBook is neither HttpHandler nor HttpHandle

If I use a traditional handler, the test will be easy like this

func TestGetBook(t *testing.T) {
  req, _ := http.NewRequest("GET", "/book/sampleid", nil)
  rr := httptest.NewRecorder()
  handler := controllers.NewBookController().GetBook
  handler.ServeHTTP(rr,req)
  if status := rr.code; status != http.StatusOK {
    t.Errorf("Wrong status")
  }
}

The problem is, httprouter is not handler, or handlefunc. So I am stuck now

解决方案

Just spin up a new router for each test and then register the handler under test, then pass the test request to the router, not the handler, so that the router can parse the path parameters and pass them to the handler.

func TestGetBook(t *testing.T) {
    handler := controllers.NewBookController()
    router := httprouter.New()
    router.GET("/book/:id", handler.GetBook)

    req, _ := http.NewRequest("GET", "/book/sampleid", nil)
    rr := httptest.NewRecorder()

    router.ServeHTTP(rr, req)
    if status := rr.Code; status != http.StatusOK {
        t.Errorf("Wrong status")
    }
}

这篇关于如何用httprouter编写测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆