Skip to content

Commit

Permalink
chore: use http.Status* instead of hard code (gin-gonic#1482)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkerou authored and appleboy committed Aug 14, 2018
1 parent 8aef947 commit f45c928
Show file tree
Hide file tree
Showing 20 changed files with 209 additions and 192 deletions.
12 changes: 6 additions & 6 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func TestBasicAuthSucceed(t *testing.T) {
router := New()
router.Use(BasicAuth(accounts))
router.GET("/login", func(c *Context) {
c.String(200, c.MustGet(AuthUserKey).(string))
c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
})

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/login", nil)
req.Header.Set("Authorization", authorizationHeader("admin", "password"))
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "admin", w.Body.String())
}

Expand All @@ -112,7 +112,7 @@ func TestBasicAuth401(t *testing.T) {
router.Use(BasicAuth(accounts))
router.GET("/login", func(c *Context) {
called = true
c.String(200, c.MustGet(AuthUserKey).(string))
c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
})

w := httptest.NewRecorder()
Expand All @@ -121,7 +121,7 @@ func TestBasicAuth401(t *testing.T) {
router.ServeHTTP(w, req)

assert.False(t, called)
assert.Equal(t, 401, w.Code)
assert.Equal(t, http.StatusUnauthorized, w.Code)
assert.Equal(t, "Basic realm=\"Authorization Required\"", w.HeaderMap.Get("WWW-Authenticate"))
}

Expand All @@ -132,7 +132,7 @@ func TestBasicAuth401WithCustomRealm(t *testing.T) {
router.Use(BasicAuthForRealm(accounts, "My Custom \"Realm\""))
router.GET("/login", func(c *Context) {
called = true
c.String(200, c.MustGet(AuthUserKey).(string))
c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
})

w := httptest.NewRecorder()
Expand All @@ -141,6 +141,6 @@ func TestBasicAuth401WithCustomRealm(t *testing.T) {
router.ServeHTTP(w, req)

assert.False(t, called)
assert.Equal(t, 401, w.Code)
assert.Equal(t, http.StatusUnauthorized, w.Code)
assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.HeaderMap.Get("WWW-Authenticate"))
}
6 changes: 3 additions & 3 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func BenchmarkOneRouteJSON(B *testing.B) {
Status string `json:"status"`
}{"ok"}
router.GET("/json", func(c *Context) {
c.JSON(200, data)
c.JSON(http.StatusOK, data)
})
runRequest(B, router, "GET", "/json")
}
Expand All @@ -66,7 +66,7 @@ func BenchmarkOneRouteHTML(B *testing.B) {
router.SetHTMLTemplate(t)

router.GET("/html", func(c *Context) {
c.HTML(200, "index", "hola")
c.HTML(http.StatusOK, "index", "hola")
})
runRequest(B, router, "GET", "/html")
}
Expand All @@ -82,7 +82,7 @@ func BenchmarkOneRouteSet(B *testing.B) {
func BenchmarkOneRouteString(B *testing.B) {
router := New()
router.GET("/text", func(c *Context) {
c.String(200, "this is a plain text")
c.String(http.StatusOK, "this is a plain text")
})
runRequest(B, router, "GET", "/text")
}
Expand Down
Loading

0 comments on commit f45c928

Please sign in to comment.