亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么運行 mux API 測試時響應體為空?

為什么運行 mux API 測試時響應體為空?

Go
千萬里不及你 2022-07-18 15:44:35
在遵循他們的教程之后,我正在嘗試在 Go 中構建和測試一個非?;镜?API,以了解有關該語言的更多信息。API 和定義的四個路由在 Postman 和瀏覽器中工作,但是當嘗試為任何路由編寫測試時,ResponseRecorder 沒有正文,因此我無法驗證它是否正確。我按照此處的示例進行操作,它可以工作,但是當我為我的路線更改它時,沒有響應。這是我的main.go文件。package mainimport (    "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)}我很感激我可能犯了一個初學者的錯誤,所以請憐憫我。我已經閱讀了許多測試多路復用器的博客,但看不出我做錯了什么。提前感謝您的指導。
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我認為這是因為您的測試不包括路由器,因此未檢測到路徑變量。來,試試這個


 // 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()

 }


查看完整回答
反對 回復 2022-07-18
  • 1 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號