diff --git a/auth.go b/auth.go index ab4e35d..125e659 100644 --- a/auth.go +++ b/auth.go @@ -65,14 +65,10 @@ func BasicAuth(accounts Accounts) HandlerFunc { } func processAccounts(accounts Accounts) authPairs { - if len(accounts) == 0 { - panic("Empty list of authorized credentials") - } + assert1(len(accounts) > 0, "Empty list of authorized credentials") pairs := make(authPairs, 0, len(accounts)) for user, password := range accounts { - if len(user) == 0 { - panic("User can not be empty") - } + assert1(len(user) > 0, "User can not be empty") value := authorizationHeader(user, password) pairs = append(pairs, authPair{ Value: value, diff --git a/context.go b/context.go index 5b45758..325ba59 100644 --- a/context.go +++ b/context.go @@ -347,7 +347,7 @@ func (c *Context) SetCookie( if path == "" { path = "/" } - cookie := http.Cookie{ + http.SetCookie(c.Writer, &http.Cookie{ Name: name, Value: url.QueryEscape(value), MaxAge: maxAge, @@ -355,8 +355,7 @@ func (c *Context) SetCookie( Domain: domain, Secure: secure, HttpOnly: httpOnly, - } - c.Header("Set-Cookie", cookie.String()) + }) } func (c *Context) Cookie(name string) (string, error) { @@ -492,9 +491,8 @@ func (c *Context) Negotiate(code int, config Negotiate) { } func (c *Context) NegotiateFormat(offered ...string) string { - if len(offered) == 0 { - panic("you must provide at least one offer") - } + assert1(len(offered) > 0, "you must provide at least one offer") + if c.Accepted == nil { c.Accepted = parseAccept(c.requestHeader("Accept")) } diff --git a/context_test.go b/context_test.go index 1b774ff..d394ffe 100644 --- a/context_test.go +++ b/context_test.go @@ -241,7 +241,6 @@ func TestContextPostFormMultipart(t *testing.T) { func TestContextSetCookie(t *testing.T) { c, _, _ := CreateTestContext() c.SetCookie("user", "gin", 1, "/", "localhost", true, true) - c.Request, _ = http.NewRequest("GET", "/set", nil) assert.Equal(t, c.Writer.Header().Get("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure") } @@ -249,7 +248,7 @@ func TestContextGetCookie(t *testing.T) { c, _, _ := CreateTestContext() c.Request, _ = http.NewRequest("GET", "/get", nil) c.Request.Header.Set("Cookie", "user=gin") - cookie, _ := c.GetCookie("user") + cookie, _ := c.Cookie("user") assert.Equal(t, cookie, "gin") } diff --git a/gin.go b/gin.go index 6cd6a25..fb1df9c 100644 --- a/gin.go +++ b/gin.go @@ -178,25 +178,15 @@ func (engine *Engine) rebuild405Handlers() { } func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { + assert1(path[0] == '/', "path must begin with '/'") + assert1(len(method) > 0, "HTTP method can not be empty") + assert1(len(handlers) > 0, "there must be at least one handler") + debugPrintRoute(method, path, handlers) - - if path[0] != '/' { - panic("path must begin with '/'") - } - if method == "" { - panic("HTTP method can not be empty") - } - if len(handlers) == 0 { - panic("there must be at least one handler") - } - root := engine.trees.get(method) if root == nil { root = new(node) - engine.trees = append(engine.trees, methodTree{ - method: method, - root: root, - }) + engine.trees = append(engine.trees, methodTree{method: method, root: root}) } root.addRoute(path, handlers) } diff --git a/utils.go b/utils.go index 533888d..2814791 100644 --- a/utils.go +++ b/utils.go @@ -71,6 +71,12 @@ func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error { return nil } +func assert1(guard bool, text string) { + if !guard { + panic(text) + } +} + func filterFlags(content string) string { for i, char := range content { if char == ' ' || char == ';' {