github.com/xmidt-org/webpa-common@v1.11.9/service/servicehttp/redirectHandler_test.go (about) 1 package servicehttp 2 3 import ( 4 "errors" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/xmidt-org/webpa-common/service" 11 ) 12 13 func testRedirectHandlerKeyFuncError(t *testing.T) { 14 var ( 15 assert = assert.New(t) 16 17 expectedError = errors.New("expected") 18 keyFunc = func(*http.Request) ([]byte, error) { return nil, expectedError } 19 accessor = new(service.MockAccessor) 20 21 response = httptest.NewRecorder() 22 request = httptest.NewRequest("GET", "/", nil) 23 24 handler = RedirectHandler{ 25 KeyFunc: keyFunc, 26 Accessor: accessor, 27 RedirectCode: http.StatusTemporaryRedirect, 28 } 29 ) 30 31 handler.ServeHTTP(response, request) 32 33 assert.Equal(http.StatusBadRequest, response.Code) 34 accessor.AssertExpectations(t) 35 } 36 37 func testRedirectHandlerAccessorError(t *testing.T) { 38 var ( 39 assert = assert.New(t) 40 41 expectedKey = []byte("34589lkdjasd") 42 keyFunc = func(*http.Request) ([]byte, error) { return expectedKey, nil } 43 expectedError = errors.New("expected") 44 accessor = new(service.MockAccessor) 45 46 response = httptest.NewRecorder() 47 request = httptest.NewRequest("GET", "/", nil) 48 49 handler = RedirectHandler{ 50 KeyFunc: keyFunc, 51 Accessor: accessor, 52 RedirectCode: http.StatusTemporaryRedirect, 53 } 54 ) 55 56 accessor.On("Get", expectedKey).Return("", expectedError).Once() 57 handler.ServeHTTP(response, request) 58 59 assert.Equal(http.StatusInternalServerError, response.Code) 60 accessor.AssertExpectations(t) 61 } 62 63 func testRedirectHandlerSuccess(t *testing.T) { 64 var ( 65 assert = assert.New(t) 66 67 expectedKey = []byte("asdfqwer") 68 expectedInstance = "https://ahost123.com:324" 69 keyFunc = func(*http.Request) ([]byte, error) { return expectedKey, nil } 70 accessor = new(service.MockAccessor) 71 72 response = httptest.NewRecorder() 73 request = httptest.NewRequest("GET", "/", nil) 74 75 handler = RedirectHandler{ 76 KeyFunc: keyFunc, 77 Accessor: accessor, 78 RedirectCode: http.StatusTemporaryRedirect, 79 } 80 ) 81 82 accessor.On("Get", expectedKey).Return(expectedInstance, error(nil)).Once() 83 handler.ServeHTTP(response, request) 84 85 assert.Equal(handler.RedirectCode, response.Code) 86 assert.Equal(expectedInstance, response.HeaderMap.Get("Location")) 87 accessor.AssertExpectations(t) 88 } 89 90 func testRedirectHandlerSuccessWithPath(t *testing.T) { 91 var ( 92 assert = assert.New(t) 93 94 expectedKey = []byte("asdfqwer") 95 expectedInstance = "https://ahost123.com:324" 96 requestURI = "/this/awesome/path" 97 expectedRedirectURL = expectedInstance + requestURI 98 keyFunc = func(*http.Request) ([]byte, error) { return expectedKey, nil } 99 accessor = new(service.MockAccessor) 100 101 response = httptest.NewRecorder() 102 request = httptest.NewRequest("GET", "https://someIrrelevantHost.com"+requestURI, nil) 103 104 handler = RedirectHandler{ 105 KeyFunc: keyFunc, 106 Accessor: accessor, 107 RedirectCode: http.StatusTemporaryRedirect, 108 } 109 ) 110 111 //setting this manually as we assume the net client would provide it 112 request.RequestURI = requestURI 113 114 accessor.On("Get", expectedKey).Return(expectedInstance, error(nil)).Once() 115 handler.ServeHTTP(response, request) 116 117 assert.Equal(handler.RedirectCode, response.Code) 118 assert.Equal(expectedRedirectURL, response.HeaderMap.Get("Location")) 119 accessor.AssertExpectations(t) 120 } 121 122 func TestRedirectHandler(t *testing.T) { 123 t.Run("KeyFuncError", testRedirectHandlerKeyFuncError) 124 t.Run("AccessorError", testRedirectHandlerAccessorError) 125 t.Run("Success", testRedirectHandlerSuccess) 126 t.Run("SuccessPath", testRedirectHandlerSuccessWithPath) 127 }