Exemple #1
0
func (s *Server) handler() http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) {
		tid := uuid.NewV4().String()
		cLog := s.log.WithFields(logrus.Fields{"tid": tid})
		cLog.WithFields(logrus.Fields{"method": r.Method, "uri": helpers.SanitizeURL(r.URL)}).Info("request started")
		r = keys.SetLog(r, cLog)
		defer func() {
			cLog.Info("request ended")
			// Catch panic and return 500 with corresponding tid for debugging
			var err error
			r := recover()
			if r != nil {
				switch t := r.(type) {
				case string:
					err = errors.New(t)
				case error:
					err = t
				default:
					err = errors.New(fmt.Sprintln(r))
				}
				trace := make([]byte, 2048)
				count := runtime.Stack(trace, true)
				cLog.Error(fmt.Sprintf("recover from panic: %s\nstack of %d bytes: %s\n", err.Error(), count, trace))
				w.WriteHeader(http.StatusInternalServerError)
				w.Write([]byte(tid))
				return
			}

		}()
		s.router.ServeHTTP(w, r)
	}
	return http.HandlerFunc(handlerFunc)
}
Exemple #2
0
func (suite *TestSuite) TestJWTMiddleware_with401() {
	r, err := http.NewRequest("GET", "", nil)
	require.Nil(suite.T(), err)
	keys.SetLog(r, logrus.WithField("test", "test"))
	w := httptest.NewRecorder()
	suite.middleware(w, r)
	require.Equal(suite.T(), http.StatusUnauthorized, w.Code)
}
Exemple #3
0
func (suite *TestSuite) TestJWTMiddleware() {
	token, err := suite.authenticator.CreateToken(user)
	require.Nil(suite.T(), err)
	r, err := http.NewRequest("GET", "", nil)
	r.Header.Set("Authorization", "Bearer "+token)
	keys.SetLog(r, logrus.WithField("test", "test"))
	require.Nil(suite.T(), err)
	w := httptest.NewRecorder()
	suite.middleware(w, r)
	require.Equal(suite.T(), http.StatusOK, w.Code)
}
Exemple #4
0
func TestToken_withNilBody(t *testing.T) {
	dirs := defaultDirs
	o := newObject(t)
	o.setupService(t, &dirs)

	r, err := http.NewRequest("POST", tokenURL, nil)
	require.Nil(t, err)
	keys.SetLog(r, logrus.WithField("test", "test"))

	w := httptest.NewRecorder()
	handler := o.service.Endpoints()["/token"]["POST"]
	o.wrapRequest(w, r, handler)
	require.Equal(t, http.StatusInternalServerError, w.Code)
}
Exemple #5
0
func TestToken_withInvalidJSON(t *testing.T) {
	dirs := defaultDirs
	o := newObject(t)
	o.setupService(t, &dirs)

	o.mockAuthenticationController.On("Authenticate").Return("testtoken", nil)

	body := strings.NewReader("")
	r, err := http.NewRequest("POST", tokenURL, body)
	require.Nil(t, err)
	keys.SetLog(r, logrus.WithField("test", "test"))

	w := httptest.NewRecorder()
	handler := o.service.Endpoints()["/token"]["POST"]
	o.wrapRequest(w, r, handler)
	require.Equal(t, http.StatusBadRequest, w.Code)
}
Exemple #6
0
func TestToken(t *testing.T) {
	dirs := defaultDirs
	o := newObject(t)
	o.setupService(t, &dirs)

	o.mockAuthenticationController.On("Authenticate").Return("testtoken", nil)

	body := strings.NewReader(`{"username":"******", "password":"******"}`)
	r, err := http.NewRequest("POST", tokenURL, body)
	require.Nil(t, err)
	keys.SetLog(r, logrus.WithField("test", "test"))

	w := httptest.NewRecorder()
	handler := o.service.Endpoints()["/token"]["POST"]
	o.wrapRequest(w, r, handler)
	require.Equal(t, http.StatusCreated, w.Code)

	authNRes := &TokenResponse{}
	err = json.NewDecoder(w.Body).Decode(authNRes)
	require.Nil(t, err)
	require.Equal(t, "testtoken", authNRes.AccessToken)
}
Exemple #7
0
func (o *testObject) wrapAuthenticatedRequest(w *httptest.ResponseRecorder, r *http.Request, handler http.Handler) {
	keys.SetLog(r, logrus.WithField("test", "test"))
	keys.SetUser(r, o.user)
	handler.ServeHTTP(w, r)
}