Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Sep 27, 2024
1 parent 481262c commit e32d21e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ type Context interface {
Accept() *Accepts
Protocol() string
Site() string
FullRequestURI() string
RequestURI() string
Scheme() string
Domain() string
Expand Down
13 changes: 12 additions & 1 deletion context_x_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,19 @@ func (c *xContext) Site() string {
return c.Scheme() + `://` + c.Request().Host() + `/`
}

func (c *xContext) FullRequestURI() string {
return c.Scheme() + `://` + c.Request().Host() + c.RequestURI()
}

func (c *xContext) RequestURI() string {
return c.Scheme() + `://` + c.Request().Host() + c.Request().URL().String()
if len(c.Request().URI()) == 0 {
q := c.Request().URL().RawQuery()
if len(q) > 0 {
q = `?` + q
}
return c.Request().URL().Path() + q
}
return c.Request().URI()
}

// Scheme returns request scheme as `http` or `https`.
Expand Down
5 changes: 4 additions & 1 deletion echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,17 @@ func TestEchoMiddlewareError(t *testing.T) {
e.SetDebug(true)
e.Use(mw.Log(), func(next HandlerFunc) HandlerFunc {
return func(c Context) error {
assert.Equal(t, `/`, c.RequestURI())
assert.Equal(t, `https://github.com/`, c.FullRequestURI())
assert.Equal(t, ``, c.Request().URI())
return errors.New("error")
}
})
e.Get("/", NotFoundHandler)

e.RebuildRouter()

c, r := request(GET, "/", e)
c, r := request(GET, "https://github.com/", e)
assert.Equal(t, http.StatusInternalServerError, c)
assert.Equal(t, `error`, r)
}
Expand Down
16 changes: 16 additions & 0 deletions engine/standard/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package standard

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
"github.com/webx-top/echo"
)

func TestURL(t *testing.T) {
u, err := url.Parse(`https://github.com/webx-top/echo?test=1&name=测试`)
assert.NoError(t, err)
assert.Equal(t, `https://github.com/webx-top/echo?test=1`, u.String())
echo.Dump(u)
}
10 changes: 5 additions & 5 deletions render_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type RenderData struct {
Func template.FuncMap
Data interface{}
Stored param.MapReadonly
requestURI string
}

func (r *RenderData) Now() *com.Time {
Expand Down Expand Up @@ -250,10 +249,11 @@ func (r *RenderData) Scheme() string {
}

func (r *RenderData) RequestURI() string {
if len(r.requestURI) == 0 {
r.requestURI = r.ctx.RequestURI()
}
return r.requestURI
return r.ctx.RequestURI()
}

func (r *RenderData) FullRequestURI() string {
return r.ctx.FullRequestURI()
}

func (r *RenderData) GetNextURL(varNames ...string) string {
Expand Down

0 comments on commit e32d21e

Please sign in to comment.