Extract the application/json mime type into a const

This commit is contained in:
Cosmin Cojocar 2019-01-31 16:22:30 +01:00
parent c12db0ebf7
commit 3326194422
2 changed files with 10 additions and 8 deletions

View File

@ -30,6 +30,8 @@ const (
// Cookies are limited to 4kb including the length of the cookie name, // Cookies are limited to 4kb including the length of the cookie name,
// the cookie name can be up to 256 bytes // the cookie name can be up to 256 bytes
maxCookieLength = 3840 maxCookieLength = 3840
applicationJSON = "application/json"
) )
// SignatureHeaders contains the headers to be signed by the hmac algorithm // SignatureHeaders contains the headers to be signed by the hmac algorithm
@ -908,7 +910,7 @@ func (p *OAuthProxy) isAjax(req *http.Request) bool {
if !ok { if !ok {
acceptValues = req.Header["Accept"] acceptValues = req.Header["Accept"]
} }
const ajaxReq = "application/json" const ajaxReq = applicationJSON
for _, v := range acceptValues { for _, v := range acceptValues {
if v == ajaxReq { if v == ajaxReq {
return true return true
@ -919,6 +921,6 @@ func (p *OAuthProxy) isAjax(req *http.Request) bool {
// ErrorJSON returns the error code witht an application/json mime type // ErrorJSON returns the error code witht an application/json mime type
func (p *OAuthProxy) ErrorJSON(rw http.ResponseWriter, code int) { func (p *OAuthProxy) ErrorJSON(rw http.ResponseWriter, code int) {
rw.Header().Set("Content-Type", "application/json") rw.Header().Set("Content-Type", applicationJSON)
rw.WriteHeader(code) rw.WriteHeader(code)
} }

View File

@ -903,35 +903,35 @@ func (test *ajaxRequestTest) getEndpoint(endpoint string, header http.Header) (i
func testAjaxUnauthorizedRequest(t *testing.T, header http.Header) { func testAjaxUnauthorizedRequest(t *testing.T, header http.Header) {
test := newAjaxRequestTest() test := newAjaxRequestTest()
const endpoint = "/test" endpoint := "/test"
code, rh, err := test.getEndpoint(endpoint, header) code, rh, err := test.getEndpoint(endpoint, header)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, code) assert.Equal(t, http.StatusUnauthorized, code)
mime := rh.Get("Content-Type") mime := rh.Get("Content-Type")
assert.Equal(t, "application/json", mime) assert.Equal(t, applicationJSON, mime)
} }
func TestAjaxUnauthorizedRequest1(t *testing.T) { func TestAjaxUnauthorizedRequest1(t *testing.T) {
header := make(http.Header) header := make(http.Header)
header.Add("accept", "application/json") header.Add("accept", applicationJSON)
testAjaxUnauthorizedRequest(t, header) testAjaxUnauthorizedRequest(t, header)
} }
func TestAjaxUnauthorizedRequest2(t *testing.T) { func TestAjaxUnauthorizedRequest2(t *testing.T) {
header := make(http.Header) header := make(http.Header)
header.Add("Accept", "application/json") header.Add("Accept", applicationJSON)
testAjaxUnauthorizedRequest(t, header) testAjaxUnauthorizedRequest(t, header)
} }
func TestAjaxForbiddendRequest(t *testing.T) { func TestAjaxForbiddendRequest(t *testing.T) {
test := newAjaxRequestTest() test := newAjaxRequestTest()
const endpoint = "/test" endpoint := "/test"
header := make(http.Header) header := make(http.Header)
code, rh, err := test.getEndpoint(endpoint, header) code, rh, err := test.getEndpoint(endpoint, header)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, http.StatusForbidden, code) assert.Equal(t, http.StatusForbidden, code)
mime := rh.Get("Content-Type") mime := rh.Get("Content-Type")
assert.NotEqual(t, "application/json", mime) assert.NotEqual(t, applicationJSON, mime)
} }