Skip to content

Commit

Permalink
Cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jan 27, 2016
1 parent 97cd894 commit d64a1fb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
8 changes: 2 additions & 6 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 4 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,15 @@ func (c *Context) SetCookie(
if path == "" {
path = "/"
}
cookie := http.Cookie{
http.SetCookie(c.Writer, &http.Cookie{
Name: name,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: path,
Domain: domain,
Secure: secure,
HttpOnly: httpOnly,
}
c.Header("Set-Cookie", cookie.String())
})
}

func (c *Context) Cookie(name string) (string, error) {
Expand Down Expand Up @@ -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"))
}
Expand Down
3 changes: 1 addition & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,14 @@ 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")
}

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")
}

Expand Down
20 changes: 5 additions & 15 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ';' {
Expand Down

0 comments on commit d64a1fb

Please sign in to comment.