Browse Source

Cosmetic changes

Manu Mtz.-Almeida 10 years ago
parent
commit
d64a1fb91c
5 changed files with 18 additions and 29 deletions
  1. 2 6
      auth.go
  2. 4 6
      context.go
  3. 1 2
      context_test.go
  4. 5 15
      gin.go
  5. 6 0
      utils.go

+ 2 - 6
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,

+ 4 - 6
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"))
 	}

+ 1 - 2
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")
 }
 

+ 5 - 15
gin.go

@@ -178,25 +178,15 @@ func (engine *Engine) rebuild405Handlers() {
 }
 
 func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
-	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")
-	}
+	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)
 	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)
 }

+ 6 - 0
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 == ';' {