mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
add ut cases for package ghttp_request (#2351)
* add ut cases for package ghttp_middleware * add ut cases for package ghttp_request * add ut cases for package ghttp_request * add ut cases for package ghttp_request * add ut cases for package ghttp_request - form * add ut cases for package ghttp_request - query * add ut cases for package ghttp_request - request * add ut cases for package ghttp_request - router
This commit is contained in:
parent
1030434ce6
commit
b0c9c68c9c
@ -236,3 +236,111 @@ func Test_Request_SetCtx(t *testing.T) {
|
||||
t.Assert(c.GetContent(ctx, "/"), "1")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Request_GetCtx(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(func(r *ghttp.Request) {
|
||||
ctx := context.WithValue(r.GetCtx(), "test", 1)
|
||||
r.SetCtx(ctx)
|
||||
r.Middleware.Next()
|
||||
})
|
||||
group.ALL("/", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.Context().Value("test"))
|
||||
})
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
c := g.Client()
|
||||
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
|
||||
t.Assert(c.GetContent(ctx, "/"), "1")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Request_GetCtxVar(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(func(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
})
|
||||
group.GET("/", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetCtxVar("key", "val"))
|
||||
})
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/"), "val")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Request_Form(t *testing.T) {
|
||||
type User struct {
|
||||
Id int
|
||||
Name string
|
||||
}
|
||||
type Default struct {
|
||||
D string
|
||||
}
|
||||
s := g.Server(guid.S())
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.ALL("/", func(r *ghttp.Request) {
|
||||
r.SetForm("key", "val")
|
||||
r.Response.Write(r.GetForm("key"))
|
||||
})
|
||||
group.ALL("/useDef", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetForm("key", "defVal"))
|
||||
})
|
||||
group.ALL("/GetFormMap", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetFormMap(map[string]interface{}{"key": "val"}))
|
||||
})
|
||||
group.ALL("/GetFormMap1", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetFormMap(map[string]interface{}{"array": "val"}))
|
||||
})
|
||||
group.ALL("/GetFormMapStrVar", func(r *ghttp.Request) {
|
||||
if r.Get("a") != nil {
|
||||
r.Response.Write(r.GetFormMapStrVar()["a"])
|
||||
}
|
||||
})
|
||||
group.ALL("/GetFormStruct", func(r *ghttp.Request) {
|
||||
var user User
|
||||
if err := r.GetFormStruct(&user); err != nil {
|
||||
r.Response.Write(err.Error())
|
||||
} else {
|
||||
r.Response.Write(user.Name)
|
||||
}
|
||||
})
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/"), "val")
|
||||
t.Assert(client.GetContent(ctx, "/useDef"), "defVal")
|
||||
t.Assert(client.PostContent(ctx, "/GetFormMap"), "{\"key\":\"val\"}")
|
||||
t.Assert(client.PostContent(ctx, "/GetFormMap", "array[]=1&array[]=2"), "{\"key\":\"val\"}")
|
||||
t.Assert(client.PostContent(ctx, "/GetFormMap1", "array[]=1&array[]=2"), "{\"array\":[\"1\",\"2\"]}")
|
||||
t.Assert(client.GetContent(ctx, "/GetFormMapStrVar", "a=1&b=2"), nil)
|
||||
t.Assert(client.PostContent(ctx, "/GetFormMapStrVar", "a=1&b=2"), `1`)
|
||||
t.Assert(client.PostContent(ctx, "/GetFormStruct", g.Map{
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
}), "john")
|
||||
})
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ package ghttp_test
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -241,3 +243,34 @@ func Test_Params_File_Upload_Required(t *testing.T) {
|
||||
t.Assert(content, `{"code":51,"message":"upload file is required","data":null}`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_File_MarshalJSON(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/upload/single", func(r *ghttp.Request) {
|
||||
file := r.GetUploadFile("file")
|
||||
if file == nil {
|
||||
r.Response.WriteExit("upload file cannot be empty")
|
||||
}
|
||||
|
||||
if bytes, err := json.Marshal(file); err != nil {
|
||||
r.Response.WriteExit(err)
|
||||
} else {
|
||||
r.Response.WriteExit(bytes)
|
||||
}
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
// normal name
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
|
||||
srcPath := gtest.DataPath("upload", "file1.txt")
|
||||
content := client.PostContent(ctx, "/upload/single", g.Map{
|
||||
"file": "@file:" + srcPath,
|
||||
})
|
||||
t.Assert(strings.Contains(content, "file1.txt"), true)
|
||||
})
|
||||
}
|
||||
|
@ -32,6 +32,11 @@ func Test_Params_Parse(t *testing.T) {
|
||||
}
|
||||
r.Response.WriteExit(user.Map["id"], user.Map["score"])
|
||||
})
|
||||
s.BindHandler("/parseErr", func(r *ghttp.Request) {
|
||||
var user User
|
||||
err := r.Parse(user)
|
||||
r.Response.WriteExit(err != nil)
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
@ -41,6 +46,7 @@ func Test_Params_Parse(t *testing.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
t.Assert(client.PostContent(ctx, "/parse", `{"id":1,"name":"john","map":{"id":1,"score":100}}`), `1100`)
|
||||
t.Assert(client.PostContent(ctx, "/parseErr", `{"id":1,"name":"john","map":{"id":1,"score":100}}`), true)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -439,6 +439,19 @@ func Test_Params_GetRequestMap(t *testing.T) {
|
||||
s.BindHandler("/map", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetRequestMap())
|
||||
})
|
||||
s.BindHandler("/withKVMap", func(r *ghttp.Request) {
|
||||
m := r.GetRequestMap(map[string]interface{}{"id": 2})
|
||||
r.Response.Write(m["id"])
|
||||
})
|
||||
s.BindHandler("/paramsMapWithKVMap", func(r *ghttp.Request) {
|
||||
r.SetParam("name", "john")
|
||||
m := r.GetRequestMap(map[string]interface{}{"id": 2})
|
||||
r.Response.Write(m["id"])
|
||||
})
|
||||
s.BindHandler("/{name}.map", func(r *ghttp.Request) {
|
||||
m := r.GetRequestMap(map[string]interface{}{"id": 2})
|
||||
r.Response.Write(m["id"])
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
@ -456,6 +469,15 @@ func Test_Params_GetRequestMap(t *testing.T) {
|
||||
),
|
||||
`{"attach":"","returnmsg":"Success"}`,
|
||||
)
|
||||
|
||||
t.Assert(client.PostContent(ctx, "/john.map", "name=john"), 2)
|
||||
|
||||
t.Assert(client.PostContent(ctx, "/withKVMap", "name=john"), 2)
|
||||
|
||||
t.Assert(client.PostContent(ctx, "/paramsMapWithKVMap"), 2)
|
||||
|
||||
client.SetContentType("application/json")
|
||||
t.Assert(client.GetContent(ctx, "/withKVMap", "name=john"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
@ -636,3 +658,206 @@ func Test_Params_Parse_EmbeddedWithAliasName2(t *testing.T) {
|
||||
t.Assert(client.GetContent(ctx, "/parse?cate=1&page=2&size=10"), `{"Type":"","CategoryId":1,"Page":2,"Size":10,"Sort":0,"UserId":0}`)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_GetParam(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetParam("key", "val"))
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
t.Assert(client.PostContent(ctx, "/"), "val")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_SetQuery(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/SetQuery", func(r *ghttp.Request) {
|
||||
r.SetQuery("a", 100)
|
||||
r.Response.Write(r.GetQuery("a"))
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/SetQuery"), "100")
|
||||
t.Assert(client.GetContent(ctx, "/SetQuery?a=1"), "100")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_GetQuery(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/GetQuery", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetQuery("a", 200))
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/GetQuery"), 200)
|
||||
t.Assert(client.SetContentType("application/json").GetContent(ctx, "/GetQuery", "a=100"), 100)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_GetQueryMap(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/GetQueryMap", func(r *ghttp.Request) {
|
||||
if m := r.GetQueryMap(); len(m) > 0 {
|
||||
r.Response.Write(m["name"])
|
||||
}
|
||||
})
|
||||
s.BindHandler("/GetQueryMapWithKVMap", func(r *ghttp.Request) {
|
||||
if m := r.GetQueryMap(map[string]interface{}{"id": 1}); len(m) > 0 {
|
||||
r.Response.Write(m["id"])
|
||||
}
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
client.SetContentType("application/json")
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMap", "id=1&name=john"), `john`)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap"), 1)
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap", "name=john"), 1)
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap", "id=2&name=john"), 2)
|
||||
client.SetContentType("application/json")
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap", "name=john"), 1)
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap", "id=2&name=john"), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_GetQueryMapStrStr(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/GetQueryMapStrStr", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetQueryMapStrStr())
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMapStrStr"), "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_GetQueryMapStrVar(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/GetQueryMapStrVar", func(r *ghttp.Request) {
|
||||
m := r.GetQueryMapStrVar()
|
||||
r.Response.Write(m["id"])
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMapStrVar"), "")
|
||||
t.Assert(client.GetContent(ctx, "/GetQueryMapStrVar", "id=1"), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_GetRequest(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/GetRequest", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetRequest("id"))
|
||||
})
|
||||
s.BindHandler("/GetRequestWithDef", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetRequest("id", 2))
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/GetRequestWithDef"), 2)
|
||||
|
||||
client.SetContentType("application/json")
|
||||
t.Assert(client.GetContent(ctx, "/GetRequest", "id=1"), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_GetRequestMapStrStr(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/GetRequestMapStrStr", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetRequestMapStrStr())
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/GetRequestMapStrStr"), "")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Params_GetRequestMapStrVar(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/GetRequestMapStrVar", func(r *ghttp.Request) {
|
||||
m := r.GetRequestMapStrVar()
|
||||
r.Response.Write(m["id"])
|
||||
})
|
||||
s.SetDumpRouterMap(false)
|
||||
s.Start()
|
||||
defer s.Shutdown()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
|
||||
client := g.Client()
|
||||
client.SetPrefix(prefix)
|
||||
|
||||
t.Assert(client.GetContent(ctx, "/GetRequestMapStrVar"), "")
|
||||
t.Assert(client.GetContent(ctx, "/GetRequestMapStrVar", "id=1"), 1)
|
||||
})
|
||||
}
|
||||
|
@ -72,6 +72,12 @@ func Test_Router_Basic2(t *testing.T) {
|
||||
|
||||
func Test_Router_Value(t *testing.T) {
|
||||
s := g.Server(guid.S())
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetRouterMap()["hash"])
|
||||
})
|
||||
s.BindHandler("/GetRouter", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetRouter("name", "john").String())
|
||||
})
|
||||
s.BindHandler("/{hash}", func(r *ghttp.Request) {
|
||||
r.Response.Write(r.GetRouter("hash").String())
|
||||
})
|
||||
@ -89,6 +95,8 @@ func Test_Router_Value(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
client := g.Client()
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||
t.Assert(client.GetContent(ctx, "/"), "")
|
||||
t.Assert(client.GetContent(ctx, "/GetRouter"), "john")
|
||||
t.Assert(client.GetContent(ctx, "/data"), "data")
|
||||
t.Assert(client.GetContent(ctx, "/data.json"), "json")
|
||||
t.Assert(client.GetContent(ctx, "/data.json.map"), "json")
|
||||
|
Loading…
Reference in New Issue
Block a user