Skip to content

Commit

Permalink
fix: add tests in error_redirect_test.go (#522)
Browse files Browse the repository at this point in the history
Increased tests coverage to cover for all the three valid scenarios - http absolute, https absolute, relative. Explicitly checked Location path to ensure that correct uri scheme was returned
  • Loading branch information
xlanor authored Sep 28, 2020
1 parent 95bda99 commit 24bdd9b
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions pipeline/errors/error_redirect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func TestErrorRedirect(t *testing.T) {
assert.Equal(t, "http://test/test", rw.Header().Get("Location"))
},
},
{
d: "redirect with 302 should contain a return_to param - absolute (HTTP) ",
givenError: &herodot.ErrNotFound,
config: `{"to":"http://test/signin","return_to_query_param":"return_to"}`,
assert: func(t *testing.T, rw *httptest.ResponseRecorder) {
assert.Equal(t, 302, rw.Code)
location, err := url.Parse(rw.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "http://test/signin?return_to=%2Ftest", rw.Header().Get("Location"))
assert.Equal(t, "/test", location.Query().Get("return_to"))
},
},
{
d: "should redirect with 302 - absolute (HTTPS)",
givenError: &herodot.ErrNotFound,
Expand All @@ -51,6 +63,18 @@ func TestErrorRedirect(t *testing.T) {
assert.Equal(t, "https://test/test", rw.Header().Get("Location"))
},
},
{
d: "redirect with 302 should contain a return_to param - absolute (HTTPS) ",
givenError: &herodot.ErrNotFound,
config: `{"to":"https://test/signin","return_to_query_param":"return_to"}`,
assert: func(t *testing.T, rw *httptest.ResponseRecorder) {
assert.Equal(t, 302, rw.Code)
location, err := url.Parse(rw.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "https://test/signin?return_to=%2Ftest", rw.Header().Get("Location"))
assert.Equal(t, "/test", location.Query().Get("return_to"))
},
},
{
d: "should redirect with 302 - relative",
givenError: &herodot.ErrNotFound,
Expand All @@ -60,6 +84,18 @@ func TestErrorRedirect(t *testing.T) {
assert.Equal(t, "/test", rw.Header().Get("Location"))
},
},
{
d: "redirect with 302 should contain a return_to param - relative ",
givenError: &herodot.ErrNotFound,
config: `{"to":"/test/signin","return_to_query_param":"return_to"}`,
assert: func(t *testing.T, rw *httptest.ResponseRecorder) {
assert.Equal(t, 302, rw.Code)
location, err := url.Parse(rw.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "/test/signin?return_to=%2Ftest", rw.Header().Get("Location"))
assert.Equal(t, "/test", location.Query().Get("return_to"))
},
},
{
d: "should redirect with 301 - absolute (HTTP)",
givenError: &herodot.ErrNotFound,
Expand All @@ -69,6 +105,18 @@ func TestErrorRedirect(t *testing.T) {
assert.Equal(t, "http://test/test", rw.Header().Get("Location"))
},
},
{
d: "redirect with 301 should contain a return_to param - absolute (HTTP) ",
givenError: &herodot.ErrNotFound,
config: `{"to":"http://test/signin","return_to_query_param":"return_to","code":301}`,
assert: func(t *testing.T, rw *httptest.ResponseRecorder) {
assert.Equal(t, 301, rw.Code)
location, err := url.Parse(rw.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "http://test/signin?return_to=%2Ftest", rw.Header().Get("Location"))
assert.Equal(t, "/test", location.Query().Get("return_to"))
},
},
{
d: "should redirect with 301 - absolute (HTTPS)",
givenError: &herodot.ErrNotFound,
Expand All @@ -78,6 +126,18 @@ func TestErrorRedirect(t *testing.T) {
assert.Equal(t, "https://test/test", rw.Header().Get("Location"))
},
},
{
d: "redirect with 301 should contain a return_to param - absolute (HTTPS) ",
givenError: &herodot.ErrNotFound,
config: `{"to":"https://test/signin","return_to_query_param":"return_to","code":301}`,
assert: func(t *testing.T, rw *httptest.ResponseRecorder) {
assert.Equal(t, 301, rw.Code)
location, err := url.Parse(rw.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "https://test/signin?return_to=%2Ftest", rw.Header().Get("Location"))
assert.Equal(t, "/test", location.Query().Get("return_to"))
},
},
{
d: "should redirect with 301 - relative",
givenError: &herodot.ErrNotFound,
Expand All @@ -88,13 +148,14 @@ func TestErrorRedirect(t *testing.T) {
},
},
{
d: "should redirect with return_to param",
d: "redirect with 301 should contain a return_to param - relative ",
givenError: &herodot.ErrNotFound,
config: `{"to":"http://test/signin","return_to_query_param":"return_to"}`,
config: `{"to":"/test/signin","return_to_query_param":"return_to","code":301}`,
assert: func(t *testing.T, rw *httptest.ResponseRecorder) {
assert.Equal(t, 302, rw.Code)
assert.Equal(t, 301, rw.Code)
location, err := url.Parse(rw.Header().Get("Location"))
require.NoError(t, err)
assert.Equal(t, "/test/signin?return_to=%2Ftest", rw.Header().Get("Location"))
assert.Equal(t, "/test", location.Query().Get("return_to"))
},
},
Expand Down

0 comments on commit 24bdd9b

Please sign in to comment.