mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 18:22:23 +08:00
check parameter in path
This commit is contained in:
parent
a816f9e9db
commit
ec8d4ee42e
8
gin.go
8
gin.go
@ -21,6 +21,7 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
default400Body = []byte("400 parameter invalid")
|
||||
default404Body = []byte("404 page not found")
|
||||
default405Body = []byte("405 method not allowed")
|
||||
defaultAppEngine bool
|
||||
@ -346,6 +347,13 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
||||
root := t[i].root
|
||||
// Find route in tree
|
||||
handlers, params, tsr := root.getValue(path, c.Params, unescape)
|
||||
// Check parameter in path
|
||||
for _, item := range params {
|
||||
if ":"+item.Key == item.Value {
|
||||
serveError(c, http.StatusBadRequest, default400Body)
|
||||
return
|
||||
}
|
||||
}
|
||||
if handlers != nil {
|
||||
c.handlers = handlers
|
||||
c.Params = params
|
||||
|
@ -133,3 +133,22 @@ func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
|
||||
|
||||
// testRequest(t, "http://localhost:8033/example")
|
||||
// }
|
||||
|
||||
func TestParameterInPath(t *testing.T) {
|
||||
router := New()
|
||||
|
||||
go func() {
|
||||
router.GET("/user/:name", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||
assert.NoError(t, router.Run(":4123"))
|
||||
}()
|
||||
// have to wait for the goroutine to start and run the server
|
||||
// otherwise the main thread will complete
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
resp, err := http.Get("http://localhost:4123/user/:name")
|
||||
assert.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
assert.Equal(t, "400 Bad Request", resp.Status, "should get a 400")
|
||||
testRequest(t, "http://localhost:4123/user/gin")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user