代码拉取完成,页面将自动刷新
package dolphin
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
func TestGetRequestQuery(t *testing.T) {
app := Default()
app.Use(func(c *Context) {
if c.Method() != http.MethodGet {
t.Errorf("Expect GET request, actual %s", c.Method())
c.String("Method Should be GET", http.StatusBadRequest)
return
}
name := c.Query("name")
c.String(fmt.Sprintf("Hello %s", name), http.StatusOK)
})
go func() {
app.Run()
}()
cli := http.Client{}
resp, err := cli.Get("http://localhost:8080?name=dolphin")
defer app.Shutdown()
if err != nil {
t.Errorf("GET request expect no error, actual got %v", err)
return
}
if resp.StatusCode != http.StatusOK {
t.Errorf("GET request expect status code %d, actual got %d", http.StatusOK, resp.StatusCode)
return
}
res, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Read from response body expect no error, actual got %v", err)
return
}
if string(res) != "Hello dolphin" {
t.Errorf("Expect response body 'Hello dolphin', actual got %v", string(res))
}
}
func TestGetRequestPostJSON(t *testing.T) {
type PostJsonTestPayload struct {
Name *string `json:"name"`
Message *string `json:"message"`
}
app := New(&Config{
Port: 8081,
})
app.Use(func(c *Context) {
if c.Method() != http.MethodPost {
t.Errorf("Expect POST request, actual %s", c.Method())
c.String("Method Should be POST", http.StatusBadRequest)
return
}
var payload PostJsonTestPayload
err := c.PostJSON(&payload)
if err != nil {
t.Log(err)
c.String("Invalid parameter format", http.StatusBadRequest)
return
}
c.JSON(O{
"message": fmt.Sprintf("Hello %s", *payload.Name),
})
})
go func() {
app.Run()
}()
defer app.Shutdown()
cli := http.Client{}
body, err := json.Marshal(map[string]string{
"name": "dolphin",
})
if err != nil {
t.Errorf("json.Marshal expect ni error, actual %v", err)
return
}
resp, err := cli.Post("http://localhost:8081", "application/json", bytes.NewBuffer(body))
if err != nil {
t.Errorf("GET request expect no error, actual got %v", err)
return
}
if resp.StatusCode != http.StatusOK {
t.Errorf("GET request expect status code %d, actual got %d", http.StatusOK, resp.StatusCode)
return
}
var data PostJsonTestPayload
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
t.Errorf("Decode response body expect no error, actual got %v", err)
return
}
if data.Message == nil || *data.Message != "Hello dolphin" {
t.Errorf("Expect response body 'Hello dolphin', actual got %v", *data.Message)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。