|
| 1 | +// Copyright 2026 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package common |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "code.gitea.io/gitea/modules/setting" |
| 14 | + "code.gitea.io/gitea/modules/test" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestFetchRedirectDelegate(t *testing.T) { |
| 20 | + defer test.MockVariableValue(&setting.AppURL, "https://gitea/")() |
| 21 | + |
| 22 | + cases := []struct { |
| 23 | + method string |
| 24 | + input string |
| 25 | + status int |
| 26 | + }{ |
| 27 | + {method: "POST", input: "/foo?k=v", status: http.StatusSeeOther}, |
| 28 | + {method: "GET", input: "/foo?k=v", status: http.StatusBadRequest}, |
| 29 | + {method: "POST", input: `\/foo?k=v`, status: http.StatusBadRequest}, |
| 30 | + {method: "POST", input: `\\/foo?k=v`, status: http.StatusBadRequest}, |
| 31 | + {method: "POST", input: "https://gitea/xxx", status: http.StatusSeeOther}, |
| 32 | + {method: "POST", input: "https://other/xxx", status: http.StatusBadRequest}, |
| 33 | + } |
| 34 | + for _, c := range cases { |
| 35 | + t.Run(c.method+" "+c.input, func(t *testing.T) { |
| 36 | + resp := httptest.NewRecorder() |
| 37 | + req := httptest.NewRequest(c.method, "/?redirect="+url.QueryEscape(c.input), nil) |
| 38 | + FetchRedirectDelegate(resp, req) |
| 39 | + assert.Equal(t, c.status, resp.Code) |
| 40 | + if c.status == http.StatusSeeOther { |
| 41 | + assert.Equal(t, c.input, resp.Header().Get("Location")) |
| 42 | + } else { |
| 43 | + assert.Empty(t, resp.Header().Get("Location")) |
| 44 | + assert.Equal(t, "Bad Request", strings.TrimSpace(resp.Body.String())) |
| 45 | + } |
| 46 | + }) |
| 47 | + } |
| 48 | +} |
0 commit comments