Skip to content

Commit d64a1fb

Browse files
committed
Cosmetic changes
1 parent 97cd894 commit d64a1fb

File tree

5 files changed

+18
-29
lines changed

5 files changed

+18
-29
lines changed

auth.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,10 @@ func BasicAuth(accounts Accounts) HandlerFunc {
6565
}
6666

6767
func processAccounts(accounts Accounts) authPairs {
68-
if len(accounts) == 0 {
69-
panic("Empty list of authorized credentials")
70-
}
68+
assert1(len(accounts) > 0, "Empty list of authorized credentials")
7169
pairs := make(authPairs, 0, len(accounts))
7270
for user, password := range accounts {
73-
if len(user) == 0 {
74-
panic("User can not be empty")
75-
}
71+
assert1(len(user) > 0, "User can not be empty")
7672
value := authorizationHeader(user, password)
7773
pairs = append(pairs, authPair{
7874
Value: value,

context.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,15 @@ func (c *Context) SetCookie(
347347
if path == "" {
348348
path = "/"
349349
}
350-
cookie := http.Cookie{
350+
http.SetCookie(c.Writer, &http.Cookie{
351351
Name: name,
352352
Value: url.QueryEscape(value),
353353
MaxAge: maxAge,
354354
Path: path,
355355
Domain: domain,
356356
Secure: secure,
357357
HttpOnly: httpOnly,
358-
}
359-
c.Header("Set-Cookie", cookie.String())
358+
})
360359
}
361360

362361
func (c *Context) Cookie(name string) (string, error) {
@@ -492,9 +491,8 @@ func (c *Context) Negotiate(code int, config Negotiate) {
492491
}
493492

494493
func (c *Context) NegotiateFormat(offered ...string) string {
495-
if len(offered) == 0 {
496-
panic("you must provide at least one offer")
497-
}
494+
assert1(len(offered) > 0, "you must provide at least one offer")
495+
498496
if c.Accepted == nil {
499497
c.Accepted = parseAccept(c.requestHeader("Accept"))
500498
}

context_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,14 @@ func TestContextPostFormMultipart(t *testing.T) {
241241
func TestContextSetCookie(t *testing.T) {
242242
c, _, _ := CreateTestContext()
243243
c.SetCookie("user", "gin", 1, "/", "localhost", true, true)
244-
c.Request, _ = http.NewRequest("GET", "/set", nil)
245244
assert.Equal(t, c.Writer.Header().Get("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure")
246245
}
247246

248247
func TestContextGetCookie(t *testing.T) {
249248
c, _, _ := CreateTestContext()
250249
c.Request, _ = http.NewRequest("GET", "/get", nil)
251250
c.Request.Header.Set("Cookie", "user=gin")
252-
cookie, _ := c.GetCookie("user")
251+
cookie, _ := c.Cookie("user")
253252
assert.Equal(t, cookie, "gin")
254253
}
255254

gin.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,15 @@ func (engine *Engine) rebuild405Handlers() {
178178
}
179179

180180
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
181-
debugPrintRoute(method, path, handlers)
182-
183-
if path[0] != '/' {
184-
panic("path must begin with '/'")
185-
}
186-
if method == "" {
187-
panic("HTTP method can not be empty")
188-
}
189-
if len(handlers) == 0 {
190-
panic("there must be at least one handler")
191-
}
181+
assert1(path[0] == '/', "path must begin with '/'")
182+
assert1(len(method) > 0, "HTTP method can not be empty")
183+
assert1(len(handlers) > 0, "there must be at least one handler")
192184

185+
debugPrintRoute(method, path, handlers)
193186
root := engine.trees.get(method)
194187
if root == nil {
195188
root = new(node)
196-
engine.trees = append(engine.trees, methodTree{
197-
method: method,
198-
root: root,
199-
})
189+
engine.trees = append(engine.trees, methodTree{method: method, root: root})
200190
}
201191
root.addRoute(path, handlers)
202192
}

utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
7171
return nil
7272
}
7373

74+
func assert1(guard bool, text string) {
75+
if !guard {
76+
panic(text)
77+
}
78+
}
79+
7480
func filterFlags(content string) string {
7581
for i, char := range content {
7682
if char == ' ' || char == ';' {

0 commit comments

Comments
 (0)