1

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

我按照此处的示例进行操作,它可以工作,但是当我为我的路线更改它时,没有响应。

这是我的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)
    // }
}

我很感激我可能犯了一个初学者的错误,所以请怜悯我。我已经阅读了许多测试多路复用器的博客,但看不出我做错了什么。

提前感谢您的指导。

更新

移动我的 SeeData 呼叫以init()解决人员呼叫的主体为空的问题。

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()解决人们呼叫的身体为空的问题。

func init() {
    SeedData()
}

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

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

1 回答 1

2

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

 // 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 路由器实例初始化处理程序时,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()
 }
于 2020-12-02T15:01:01.943 回答