Skip to content

Commit

Permalink
add context.URL method returns full request url
Browse files Browse the repository at this point in the history
  • Loading branch information
coldstar committed Apr 15, 2016
1 parent f3a6168 commit f6d5abf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion context.go
Expand Up @@ -518,9 +518,18 @@ func (c *Context) URL(hasQuery bool) string {
} else {
scheme += "://"
}
} else {
scheme = ""
}
if hasQuery {
return scheme + host + c.Req.RequestURI
url := c.Req.RequestURI
if url == "" {
url = c.Req.URL.Path
if len(c.Req.URL.RawQuery) > 0 {
url += "?" + c.Req.URL.RawQuery
}
}
return scheme + host + url
}
return scheme + host + c.Req.URL.Path
}
Expand Down
8 changes: 8 additions & 0 deletions context_test.go
Expand Up @@ -482,6 +482,14 @@ func TestContext2(t *testing.T) {
b.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
})
Convey("Get URL", func() {
b.Get("/url", func(c *Context) {
So(c.URL(false), ShouldEqual, "/url")
So(c.URL(true), ShouldEqual, "/url?id=xx&ib=yy")
})
w := request("GET", "/url?id=xx&ib=yy")
So(w.Code, ShouldEqual, http.StatusOK)
})
})
}

Expand Down
3 changes: 3 additions & 0 deletions router_test.go
Expand Up @@ -134,6 +134,9 @@ func TestRouteAdd9(t *testing.T) {
So(w.Code, ShouldEqual, http.StatusOK)
})
Convey("set multi method", func() {
b2.Route("/mul", "*", func(c *Context) {
c.String(200, "mul")
})
b2.Route("/mul", "GET,HEAD,POST", func(c *Context) {
c.String(200, "mul")
})
Expand Down

0 comments on commit f6d5abf

Please sign in to comment.