Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ut cases for package ghttp_middleware and ghttp_request #2344

Merged
merged 3 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions net/ghttp/ghttp_z_unit_feature_middleware_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,31 @@ func Test_Middleware_JsonBody(t *testing.T) {
t.Assert(client.PutContent(ctx, "/", `{"name":}`), "the request body content should be JSON format")
})
}

func Test_MiddlewareHandlerResponse(t *testing.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.GET("/403", func(r *ghttp.Request) {
r.Response.WriteStatus(http.StatusForbidden, "")
})
group.GET("/default", func(r *ghttp.Request) {
r.Response.WriteStatus(http.StatusInternalServerError, "")
})
})
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()))

rsp, err := client.Get(ctx, "/403")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusForbidden)
rsp, err = client.Get(ctx, "/default")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusInternalServerError)
})
}
22 changes: 22 additions & 0 deletions net/ghttp/ghttp_z_unit_feature_middleware_cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,25 @@ func Test_Middleware_CORS2(t *testing.T) {
resp.Close()
})
}

func Test_Middleware_CORS3(t *testing.T) {
s := g.Server(guid.S())
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareCORS)
group.GET("/user/list/{type}", func(r *ghttp.Request) {
r.Response.Write(r.Get("type"))
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

gtest.C(t, func(t *gtest.T) {
client := g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
client.SetHeader("Access-Control-Request-Method", "POST")
resp, err := client.Get(ctx, "/api.v2/user/list/1")
t.AssertNil(err)
resp.Close()
})
}
194 changes: 194 additions & 0 deletions net/ghttp/ghttp_z_unit_feature_request_ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package ghttp_test
import (
"context"
"fmt"
"github.com/gogf/gf/v2/encoding/gbase64"
"net/http"
"testing"
"time"

Expand All @@ -18,6 +20,198 @@ import (
"github.com/gogf/gf/v2/util/guid"
)

func Test_Request_IsFileRequest(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/", func(r *ghttp.Request) {
r.Response.Write(r.IsFileRequest())
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

t.Assert(c.GetContent(ctx, "/"), false)
})
}

func Test_Request_IsAjaxRequest(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/", func(r *ghttp.Request) {
r.Response.Write(r.IsAjaxRequest())
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

t.Assert(c.GetContent(ctx, "/"), false)
})
}

func Test_Request_GetClientIp(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/", func(r *ghttp.Request) {
r.Response.Write(r.GetClientIp())
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetHeader("X-Forwarded-For", "192.168.0.1")
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

t.Assert(c.GetContent(ctx, "/"), "192.168.0.1")
})
}

func Test_Request_GetUrl(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/", func(r *ghttp.Request) {
r.Response.Write(r.GetUrl())
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetHeader("X-Forwarded-Proto", "https")
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

t.Assert(c.GetContent(ctx, "/"), fmt.Sprintf("https://127.0.0.1:%d/", s.GetListenedPort()))
})
}

func Test_Request_GetReferer(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/", func(r *ghttp.Request) {
r.Response.Write(r.GetReferer())
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetHeader("Referer", "Referer")
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

t.Assert(c.GetContent(ctx, "/"), "Referer")
})
}

func Test_Request_GetServeHandler(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/", func(r *ghttp.Request) {
r.Response.Write(r.GetServeHandler() != nil)
})
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()

time.Sleep(100 * time.Millisecond)

c := g.Client()
c.SetHeader("Referer", "Referer")
c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))

t.Assert(c.GetContent(ctx, "/"), true)
})
}

func Test_Request_BasicAuth(t *testing.T) {
const (
user = "root"
pass = "123456"
wrongPass = "12345"
)

s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.ALL("/auth1", func(r *ghttp.Request) {
r.BasicAuth(user, pass, "tips")
})
group.ALL("/auth2", func(r *ghttp.Request) {
r.BasicAuth(user, pass)
})
})
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()))

rsp, err := c.Get(ctx, "/auth1")
t.AssertNil(err)
t.Assert(rsp.Header.Get("WWW-Authenticate"), "Basic realm=\"tips\"")
t.Assert(rsp.StatusCode, http.StatusUnauthorized)

rsp, err = c.SetHeader("Authorization", user+pass).Get(ctx, "/auth1")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusForbidden)

rsp, err = c.SetHeader("Authorization", "Test "+user+pass).Get(ctx, "/auth1")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusForbidden)

rsp, err = c.SetHeader("Authorization", "Basic "+user+pass).Get(ctx, "/auth1")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusForbidden)

rsp, err = c.SetHeader("Authorization", "Basic "+gbase64.EncodeString(user+pass)).Get(ctx, "/auth1")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusForbidden)

rsp, err = c.SetHeader("Authorization", "Basic "+gbase64.EncodeString(user+":"+wrongPass)).Get(ctx, "/auth1")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusUnauthorized)

rsp, err = c.BasicAuth(user, pass).Get(ctx, "/auth1")
t.AssertNil(err)
t.Assert(rsp.StatusCode, http.StatusOK)

rsp, err = c.Get(ctx, "/auth2")
t.AssertNil(err)
t.Assert(rsp.Header.Get("WWW-Authenticate"), "Basic realm=\"Need Login\"")
t.Assert(rsp.StatusCode, http.StatusUnauthorized)
})
}

func Test_Request_SetCtx(t *testing.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
Expand Down