git.iko.soy/typeable/oauth2-proxy
on master help sign in
Extract the application/json mime type into a const
commit33261944228724bb4212d52d357fbe9842a7b00f authorCosmin Cojocar <cosmin.cojocar@gmx.ch> dateThu 31 Jan 2019, 16:22 (8 years ago) parentc12db0e
2 files changed+10−8
oauthproxy.go+4−2 oauthproxy_test.go+6−6
oauthproxy.go+4−2
@@ -30,6 +30,8 @@ const (
3030 // Cookies are limited to 4kb including the length of the cookie name,
3131 // the cookie name can be up to 256 bytes
3232 maxCookieLength = 3840
33+
34+ applicationJSON = "application/json"
3335)
3436
3537// SignatureHeaders contains the headers to be signed by the hmac algorithm
@@ -908,7 +910,7 @@ func (p *OAuthProxy) isAjax(req *http.Request) bool {
908910 if !ok {
909911 acceptValues = req.Header["Accept"]
910912 }
911− const ajaxReq = "application/json"
913+ const ajaxReq = applicationJSON
912914 for _, v := range acceptValues {
913915 if v == ajaxReq {
914916 return true
@@ -919,6 +921,6 @@ func (p *OAuthProxy) isAjax(req *http.Request) bool {
919921
920922// ErrorJSON returns the error code witht an application/json mime type
921923func (p *OAuthProxy) ErrorJSON(rw http.ResponseWriter, code int) {
922− rw.Header().Set("Content-Type", "application/json")
924+ rw.Header().Set("Content-Type", applicationJSON)
923925 rw.WriteHeader(code)
924926}
oauthproxy_test.go+6−6
@@ -903,35 +903,35 @@ func (test *ajaxRequestTest) getEndpoint(endpoint string, header http.Header) (i
903903
904904func testAjaxUnauthorizedRequest(t *testing.T, header http.Header) {
905905 test := newAjaxRequestTest()
906− const endpoint = "/test"
906+ endpoint := "/test"
907907
908908 code, rh, err := test.getEndpoint(endpoint, header)
909909 assert.NoError(t, err)
910910 assert.Equal(t, http.StatusUnauthorized, code)
911911 mime := rh.Get("Content-Type")
912− assert.Equal(t, "application/json", mime)
912+ assert.Equal(t, applicationJSON, mime)
913913}
914914func TestAjaxUnauthorizedRequest1(t *testing.T) {
915915 header := make(http.Header)
916− header.Add("accept", "application/json")
916+ header.Add("accept", applicationJSON)
917917
918918 testAjaxUnauthorizedRequest(t, header)
919919}
920920
921921func TestAjaxUnauthorizedRequest2(t *testing.T) {
922922 header := make(http.Header)
923− header.Add("Accept", "application/json")
923+ header.Add("Accept", applicationJSON)
924924
925925 testAjaxUnauthorizedRequest(t, header)
926926}
927927
928928func TestAjaxForbiddendRequest(t *testing.T) {
929929 test := newAjaxRequestTest()
930− const endpoint = "/test"
930+ endpoint := "/test"
931931 header := make(http.Header)
932932 code, rh, err := test.getEndpoint(endpoint, header)
933933 assert.NoError(t, err)
934934 assert.Equal(t, http.StatusForbidden, code)
935935 mime := rh.Get("Content-Type")
936− assert.NotEqual(t, "application/json", mime)
936+ assert.NotEqual(t, applicationJSON, mime)
937937}