运行 mux API 测试时为什么响应体为空?

Why is the response body empty when running a test of mux API?

我正在尝试在 Go 中构建和测试一个非常基础的 API,以便在遵循他们的教程后了解有关该语言的更多信息。 API 和定义的四个路由在 Postman 和浏览器中工作,但是当尝试为任何路由编写测试时,ResponseRecorder 没有主体,所以我无法验证它是否正确。

我按照示例 here 进行了操作,但是当我为我的路线更改它时,没有任何响应。

这是我的 main.go 文件。

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

// A Person represents a user.
type Person struct {
    ID        string    `json:"id,omitempty"`
    Firstname string    `json:"firstname,omitempty"`
    Lastname  string    `json:"lastname,omitempty"`
    Location  *Location `json:"location,omitempty"`
}

// A Location represents a Person's location.
type Location struct {
    City    string `json:"city,omitempty"`
    Country string `json:"country,omitempty"`
}

var people []Person

// GetPersonEndpoint returns an individual from the database.
func GetPersonEndpoint(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    params := mux.Vars(req)
    for _, item := range people {
        if item.ID == params["id"] {
            json.NewEncoder(w).Encode(item)
            return
        }
    }
    json.NewEncoder(w).Encode(&Person{})
}

// GetPeopleEndpoint returns all people from the database.
func GetPeopleEndpoint(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(people)
}

// CreatePersonEndpoint creates a new person in the database.
func CreatePersonEndpoint(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    params := mux.Vars(req)
    var person Person
    _ = json.NewDecoder(req.Body).Decode(&person)
    person.ID = params["id"]
    people = append(people, person)
    json.NewEncoder(w).Encode(people)
}

// DeletePersonEndpoint deletes a person from the database.
func DeletePersonEndpoint(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    params := mux.Vars(req)
    for index, item := range people {
        if item.ID == params["id"] {
            people = append(people[:index], people[index+1:]...)
            break
        }
    }
    json.NewEncoder(w).Encode(people)
}

// SeedData is just for this example and mimics a database in the 'people' variable.
func SeedData() {
    people = append(people, Person{ID: "1", Firstname: "John", Lastname: "Smith", Location: &Location{City: "London", Country: "United Kingdom"}})
    people = append(people, Person{ID: "2", Firstname: "John", Lastname: "Doe", Location: &Location{City: "New York", Country: "United States Of America"}})
}

func main() {
    router := mux.NewRouter()
    SeedData()
    router.HandleFunc("/people", GetPeopleEndpoint).Methods("GET")
    router.HandleFunc("/people/{id}", GetPersonEndpoint).Methods("GET")
    router.HandleFunc("/people/{id}", CreatePersonEndpoint).Methods("POST")
    router.HandleFunc("/people/{id}", DeletePersonEndpoint).Methods("DELETE")
    fmt.Println("Listening on http://localhost:12345")
    fmt.Println("Press 'CTRL + C' to stop server.")
    log.Fatal(http.ListenAndServe(":12345", router))
}

这是我的 main_test.go 文件。

package main

import (
    "fmt"
    "net/http"
    "net/http/httptest"
    "testing"
)

func TestGetPeopleEndpoint(t *testing.T) {
    req, err := http.NewRequest("GET", "/people", nil)
    if err != nil {
        t.Fatal(err)
    }

    // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
    rr := httptest.NewRecorder()
    handler := http.HandlerFunc(GetPeopleEndpoint)

    // Our handlers satisfy http.Handler, so we can call their ServeHTTP method
    // directly and pass in our Request and ResponseRecorder.
    handler.ServeHTTP(rr, req)

    // Trying to see here what is in the response.
    fmt.Println(rr)
    fmt.Println(rr.Body.String())

    // Check the status code is what we expect.
    if status := rr.Code; status != http.StatusOK {
        t.Errorf("handler returned wrong status code: got %v want %v",
            status, http.StatusOK)
    }

    // Check the response body is what we expect - Commented out because it will fail because there is no body at the moment.
    // expected := `[{"id":"1","firstname":"John","lastname":"Smith","location":{"city":"London","country":"United Kingdom"}},{"id":"2","firstname":"John","lastname":"Doe","location":{"city":"New York","country":"United States Of America"}}]`
    // if rr.Body.String() != expected {
    //  t.Errorf("handler returned unexpected body: got %v want %v",
    //      rr.Body.String(), expected)
    // }
}

我明白我可能犯了一个初学者的错误,所以请怜悯我。我已经阅读了一些测试 mux 的博客,但看不出我做错了什么。

在此先感谢您的指导。

更新

将我的 SeeData 调用移至 init() 解决了 people 调用的正文为空问题。

func init() {
    SeedData()
}

但是,我现在在测试特定 ID 时没有返回正文。

func TestGetPersonEndpoint(t *testing.T) {
    id := 1
    path := fmt.Sprintf("/people/%v", id)
    req, err := http.NewRequest("GET", path, nil)
    if err != nil {
        t.Fatal(err)
    }

    // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
    rr := httptest.NewRecorder()
    handler := http.HandlerFunc(GetPersonEndpoint)

    // Our handlers satisfy http.Handler, so we can call their ServeHTTP method
    // directly and pass in our Request and ResponseRecorder.
    handler.ServeHTTP(rr, req)

    // Check request is made correctly and responses.
    fmt.Println(path)
    fmt.Println(rr)
    fmt.Println(req)
    fmt.Println(handler)

    // expected response for id 1.
    expected := `{"id":"1","firstname":"John","lastname":"Smith","location":{"city":"London","country":"United Kingdom"}}` + "\n"

    if status := rr.Code; status != http.StatusOK {
        message := fmt.Sprintf("The test returned the wrong status code: got %v, but expected %v", status, http.StatusOK)
        t.Fatal(message)
    }

    if rr.Body.String() != expected {
        message := fmt.Sprintf("The test returned the wrong data:\nFound: %v\nExpected: %v", rr.Body.String(), expected)
        t.Fatal(message)
    }
}

将我的 SeedData 调用移至 init() 解决了 people 调用的正文为空问题。

func init() {
    SeedData()
}

创建新的路由器实例解决了访问路由变量的问题。

rr := httptest.NewRecorder()
router := mux.NewRouter()
router.HandleFunc("/people/{id}", GetPersonEndpoint)
router.ServeHTTP(rr, req)

我认为这是因为您的测试不包括路由器,因此未检测到路径变量。在这里,试试这个

 // main.go
 func router() *mux.Router {
    router := mux.NewRouter()
    router.HandleFunc("/people", GetPeopleEndpoint).Methods("GET")
    router.HandleFunc("/people/{id}", GetPersonEndpoint).Methods("GET")
    router.HandleFunc("/people/{id}", CreatePersonEndpoint).Methods("POST")
    router.HandleFunc("/people/{id}", DeletePersonEndpoint).Methods("DELETE")
    return router
 }

并在您的测试用例中,从路由器方法启动,如下所示

 handler := router()
 // Our handlers satisfy http.Handler, so we can call their ServeHTTP method
 // directly and pass in our Request and ResponseRecorder.
 handler.ServeHTTP(rr, req)

现在,如果您尝试访问路径变量 id,它应该出现在 mux 返回的映射中,因为当您从 router() 返回的 mux Router 实例启动 Handler 时,mux 注册了它

 params := mux.Vars(req)
 for index, item := range people {
    if item.ID == params["id"] {
        people = append(people[:index], people[index+1:]...)
        break
    }
 }

也像你提到的那样,使用 init 函数进行一次性设置。

 // main.go
 func init(){
    SeedData()
 }