Cosmetic changes

This commit is contained in:
Manu Mtz.-Almeida 2016-01-28 00:35:09 +01:00
parent 97cd894279
commit d64a1fb91c
5 changed files with 18 additions and 29 deletions

View File

@ -65,14 +65,10 @@ func BasicAuth(accounts Accounts) HandlerFunc {
} }
func processAccounts(accounts Accounts) authPairs { func processAccounts(accounts Accounts) authPairs {
if len(accounts) == 0 { assert1(len(accounts) > 0, "Empty list of authorized credentials")
panic("Empty list of authorized credentials")
}
pairs := make(authPairs, 0, len(accounts)) pairs := make(authPairs, 0, len(accounts))
for user, password := range accounts { for user, password := range accounts {
if len(user) == 0 { assert1(len(user) > 0, "User can not be empty")
panic("User can not be empty")
}
value := authorizationHeader(user, password) value := authorizationHeader(user, password)
pairs = append(pairs, authPair{ pairs = append(pairs, authPair{
Value: value, Value: value,

View File

@ -347,7 +347,7 @@ func (c *Context) SetCookie(
if path == "" { if path == "" {
path = "/" path = "/"
} }
cookie := http.Cookie{ http.SetCookie(c.Writer, &http.Cookie{
Name: name, Name: name,
Value: url.QueryEscape(value), Value: url.QueryEscape(value),
MaxAge: maxAge, MaxAge: maxAge,
@ -355,8 +355,7 @@ func (c *Context) SetCookie(
Domain: domain, Domain: domain,
Secure: secure, Secure: secure,
HttpOnly: httpOnly, HttpOnly: httpOnly,
} })
c.Header("Set-Cookie", cookie.String())
} }
func (c *Context) Cookie(name string) (string, error) { 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 { func (c *Context) NegotiateFormat(offered ...string) string {
if len(offered) == 0 { assert1(len(offered) > 0, "you must provide at least one offer")
panic("you must provide at least one offer")
}
if c.Accepted == nil { if c.Accepted == nil {
c.Accepted = parseAccept(c.requestHeader("Accept")) c.Accepted = parseAccept(c.requestHeader("Accept"))
} }

View File

@ -241,7 +241,6 @@ func TestContextPostFormMultipart(t *testing.T) {
func TestContextSetCookie(t *testing.T) { func TestContextSetCookie(t *testing.T) {
c, _, _ := CreateTestContext() c, _, _ := CreateTestContext()
c.SetCookie("user", "gin", 1, "/", "localhost", true, true) 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") 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, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("GET", "/get", nil) c.Request, _ = http.NewRequest("GET", "/get", nil)
c.Request.Header.Set("Cookie", "user=gin") c.Request.Header.Set("Cookie", "user=gin")
cookie, _ := c.GetCookie("user") cookie, _ := c.Cookie("user")
assert.Equal(t, cookie, "gin") assert.Equal(t, cookie, "gin")
} }

20
gin.go
View File

@ -178,25 +178,15 @@ func (engine *Engine) rebuild405Handlers() {
} }
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { 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) 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) root := engine.trees.get(method)
if root == nil { if root == nil {
root = new(node) root = new(node)
engine.trees = append(engine.trees, methodTree{ engine.trees = append(engine.trees, methodTree{method: method, root: root})
method: method,
root: root,
})
} }
root.addRoute(path, handlers) root.addRoute(path, handlers)
} }

View File

@ -71,6 +71,12 @@ func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return nil return nil
} }
func assert1(guard bool, text string) {
if !guard {
panic(text)
}
}
func filterFlags(content string) string { func filterFlags(content string) string {
for i, char := range content { for i, char := range content {
if char == ' ' || char == ';' { if char == ' ' || char == ';' {