summaryrefslogtreecommitdiffstats
path: root/services/context/base_test.go
blob: 823f20e00bc0d23ee83670c4dfc732cf0a3f9195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package context

import (
	"net/http"
	"net/http/httptest"
	"testing"

	"code.gitea.io/gitea/modules/setting"

	"github.com/stretchr/testify/assert"
)

func TestRedirect(t *testing.T) {
	req, _ := http.NewRequest("GET", "/", nil)

	cases := []struct {
		url  string
		keep bool
	}{
		{"http://test", false},
		{"https://test", false},
		{"//test", false},
		{"/://test", true},
		{"/test", true},
	}
	for _, c := range cases {
		resp := httptest.NewRecorder()
		b, cleanup := NewBaseContext(resp, req)
		resp.Header().Add("Set-Cookie", (&http.Cookie{Name: setting.SessionConfig.CookieName, Value: "dummy"}).String())
		b.Redirect(c.url)
		cleanup()
		has := resp.Header().Get("Set-Cookie") == "i_like_gitea=dummy"
		assert.Equal(t, c.keep, has, "url = %q", c.url)
	}

	req, _ = http.NewRequest("GET", "/", nil)
	resp := httptest.NewRecorder()
	req.Header.Add("HX-Request", "true")
	b, cleanup := NewBaseContext(resp, req)
	b.Redirect("/other")
	cleanup()
	assert.Equal(t, "/other", resp.Header().Get("HX-Redirect"))
	assert.Equal(t, http.StatusNoContent, resp.Code)
}