From d64a1fb91c1cc5e14ae78c97d7ba623abd82965c Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Thu, 28 Jan 2016 00:35:09 +0100 Subject: [PATCH] Cosmetic changes --- auth.go | 8 ++------ context.go | 10 ++++------ context_test.go | 3 +-- gin.go | 20 +++++--------------- utils.go | 6 ++++++ 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/auth.go b/auth.go index ab4e35d7..125e659f 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 5b457588..325ba592 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 1b774ff1..d394ffed 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 6cd6a25b..fb1df9cd 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 533888d1..2814791f 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 == ';' {