2017-08-20 03:52:42 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-02-16 15:53:47 +05:30
|
|
|
"errors"
|
2020-01-02 02:12:45 +05:30
|
|
|
"io/ioutil"
|
2020-02-16 15:53:47 +05:30
|
|
|
"net/http"
|
2020-01-02 02:12:45 +05:30
|
|
|
"net/http/httptest"
|
2017-08-20 03:52:42 +05:30
|
|
|
"testing"
|
|
|
|
|
|
|
|
testify "github.com/stretchr/testify/assert"
|
2020-01-29 04:04:15 +05:30
|
|
|
"github.com/stretchr/testify/mock"
|
2017-08-20 03:52:42 +05:30
|
|
|
)
|
|
|
|
|
2020-02-16 15:53:47 +05:30
|
|
|
type authCheckerMock struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *authCheckerMock) Authenticate(req *http.Request) error {
|
|
|
|
args := m.Called(req)
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateAuthenticationMiddleware(t *testing.T) {
|
|
|
|
t.Run("pass", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
|
|
|
|
auth := &authCheckerMock{}
|
|
|
|
auth.On("Authenticate", req).Once().Return(nil)
|
|
|
|
|
|
|
|
isHandlerCalled := false
|
|
|
|
middlewareFunc := CreateAuthenticationMiddleware(auth)
|
|
|
|
middlewareFunc.Middleware(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
isHandlerCalled = true
|
|
|
|
})).ServeHTTP(resp, req)
|
|
|
|
|
|
|
|
testify.True(t, isHandlerCalled, "Handler isn't called from the middleware")
|
|
|
|
|
|
|
|
auth.AssertExpectations(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("fail", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
|
|
|
|
auth := &authCheckerMock{}
|
|
|
|
auth.On("Authenticate", req).Once().Return(errors.New("error reason"))
|
|
|
|
|
|
|
|
isHandlerCalled := false
|
|
|
|
middlewareFunc := CreateAuthenticationMiddleware(auth)
|
|
|
|
middlewareFunc.Middleware(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
isHandlerCalled = true
|
|
|
|
})).ServeHTTP(resp, req)
|
|
|
|
|
|
|
|
testify.False(t, isHandlerCalled, "Handler shouldn't be called")
|
|
|
|
testify.Equal(t, 403, resp.Code)
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
testify.JSONEq(t, `{
|
|
|
|
"error": "error reason"
|
|
|
|
}`, string(body))
|
|
|
|
|
|
|
|
auth.AssertExpectations(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func TestNotFoundHandler(t *testing.T) {
|
2017-08-20 03:52:42 +05:30
|
|
|
assert := testify.New(t)
|
2019-04-28 03:13:22 +05:30
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
req := httptest.NewRequest("GET", "http://example.com", nil)
|
|
|
|
w := httptest.NewRecorder()
|
2019-04-28 03:13:22 +05:30
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
NotFoundHandler(w, req)
|
2019-04-28 03:13:22 +05:30
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
resp := w.Result()
|
|
|
|
assert.Equal(404, resp.StatusCode)
|
|
|
|
assert.Equal("application/json", resp.Header.Get("Content-Type"))
|
|
|
|
response, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
assert.JSONEq(`{
|
|
|
|
"status": "404",
|
|
|
|
"message": "Not Found"
|
|
|
|
}`, string(response))
|
2019-04-28 03:13:22 +05:30
|
|
|
}
|