gitech

Форк
0
/
sanitize_test.go 
74 строки · 1.8 Кб
1
// Copyright 2021 The Gitea Authors. All rights reserved.
2
// SPDX-License-Identifier: MIT
3

4
package util
5

6
import (
7
	"errors"
8
	"testing"
9

10
	"github.com/stretchr/testify/assert"
11
)
12

13
func TestSanitizeErrorCredentialURLs(t *testing.T) {
14
	err := errors.New("error with https://a@b.com")
15
	se := SanitizeErrorCredentialURLs(err)
16
	assert.Equal(t, "error with https://"+userPlaceholder+"@b.com", se.Error())
17
}
18

19
func TestSanitizeCredentialURLs(t *testing.T) {
20
	cases := []struct {
21
		input    string
22
		expected string
23
	}{
24
		{
25
			"https://github.com/go-gitea/test_repo.git",
26
			"https://github.com/go-gitea/test_repo.git",
27
		},
28
		{
29
			"https://mytoken@github.com/go-gitea/test_repo.git",
30
			"https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git",
31
		},
32
		{
33
			"https://user:password@github.com/go-gitea/test_repo.git",
34
			"https://" + userPlaceholder + "@github.com/go-gitea/test_repo.git",
35
		},
36
		{
37
			"ftp://x@",
38
			"ftp://" + userPlaceholder + "@",
39
		},
40
		{
41
			"ftp://x/@",
42
			"ftp://x/@",
43
		},
44
		{
45
			"ftp://u@x/@", // test multiple @ chars
46
			"ftp://" + userPlaceholder + "@x/@",
47
		},
48
		{
49
			"😊ftp://u@x😊", // test unicode
50
			"😊ftp://" + userPlaceholder + "@x😊",
51
		},
52
		{
53
			"://@",
54
			"://@",
55
		},
56
		{
57
			"//u:p@h", // do not process URLs without explicit scheme, they are not treated as "valid" URLs because there is no scheme context in string
58
			"//u:p@h",
59
		},
60
		{
61
			"s://u@h", // the minimal pattern to be sanitized
62
			"s://" + userPlaceholder + "@h",
63
		},
64
		{
65
			"URLs in log https://u:b@h and https://u:b@h:80/, with https://h.com and u@h.com",
66
			"URLs in log https://" + userPlaceholder + "@h and https://" + userPlaceholder + "@h:80/, with https://h.com and u@h.com",
67
		},
68
	}
69

70
	for n, c := range cases {
71
		result := SanitizeCredentialURLs(c.input)
72
		assert.Equal(t, c.expected, result, "case %d: error should match", n)
73
	}
74
}
75

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.