Skip to content

Commit f45c928

Browse files
thinkerouappleboy
authored andcommitted
chore: use http.Status* instead of hard code (gin-gonic#1482)
1 parent 8aef947 commit f45c928

20 files changed

+209
-192
lines changed

auth_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ func TestBasicAuthSucceed(t *testing.T) {
9393
router := New()
9494
router.Use(BasicAuth(accounts))
9595
router.GET("/login", func(c *Context) {
96-
c.String(200, c.MustGet(AuthUserKey).(string))
96+
c.String(http.StatusOK, c.MustGet(AuthUserKey).(string))
9797
})
9898

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

104-
assert.Equal(t, 200, w.Code)
104+
assert.Equal(t, http.StatusOK, w.Code)
105105
assert.Equal(t, "admin", w.Body.String())
106106
}
107107

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

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

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

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

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

143143
assert.False(t, called)
144-
assert.Equal(t, 401, w.Code)
144+
assert.Equal(t, http.StatusUnauthorized, w.Code)
145145
assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.HeaderMap.Get("WWW-Authenticate"))
146146
}

benchmarks_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func BenchmarkOneRouteJSON(B *testing.B) {
5454
Status string `json:"status"`
5555
}{"ok"}
5656
router.GET("/json", func(c *Context) {
57-
c.JSON(200, data)
57+
c.JSON(http.StatusOK, data)
5858
})
5959
runRequest(B, router, "GET", "/json")
6060
}
@@ -66,7 +66,7 @@ func BenchmarkOneRouteHTML(B *testing.B) {
6666
router.SetHTMLTemplate(t)
6767

6868
router.GET("/html", func(c *Context) {
69-
c.HTML(200, "index", "hola")
69+
c.HTML(http.StatusOK, "index", "hola")
7070
})
7171
runRequest(B, router, "GET", "/html")
7272
}
@@ -82,7 +82,7 @@ func BenchmarkOneRouteSet(B *testing.B) {
8282
func BenchmarkOneRouteString(B *testing.B) {
8383
router := New()
8484
router.GET("/text", func(c *Context) {
85-
c.String(200, "this is a plain text")
85+
c.String(http.StatusOK, "this is a plain text")
8686
})
8787
runRequest(B, router, "GET", "/text")
8888
}

0 commit comments

Comments
 (0)