|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package url |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestParseGitURLs(t *testing.T) { |
| 15 | + kases := []struct { |
| 16 | + kase string |
| 17 | + expected *GitURL |
| 18 | + }{ |
| 19 | + { |
| 20 | + kase: "[email protected]:go-gitea/gitea.git", |
| 21 | + expected: &GitURL{ |
| 22 | + URL: &url.URL{ |
| 23 | + Scheme: "ssh", |
| 24 | + User: url.User("git"), |
| 25 | + Host: "127.0.0.1", |
| 26 | + Path: "go-gitea/gitea.git", |
| 27 | + }, |
| 28 | + extraMark: 1, |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + kase: "git@[fe80:14fc:cec5:c174:d88%2510]:go-gitea/gitea.git", |
| 33 | + expected: &GitURL{ |
| 34 | + URL: &url.URL{ |
| 35 | + Scheme: "ssh", |
| 36 | + User: url.User("git"), |
| 37 | + Host: "[fe80:14fc:cec5:c174:d88%10]", |
| 38 | + Path: "go-gitea/gitea.git", |
| 39 | + }, |
| 40 | + extraMark: 1, |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + kase: "git@[::1]:go-gitea/gitea.git", |
| 45 | + expected: &GitURL{ |
| 46 | + URL: &url.URL{ |
| 47 | + Scheme: "ssh", |
| 48 | + User: url.User("git"), |
| 49 | + Host: "[::1]", |
| 50 | + Path: "go-gitea/gitea.git", |
| 51 | + }, |
| 52 | + extraMark: 1, |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + kase: "[email protected]:go-gitea/gitea.git", |
| 57 | + expected: &GitURL{ |
| 58 | + URL: &url.URL{ |
| 59 | + Scheme: "ssh", |
| 60 | + User: url.User("git"), |
| 61 | + Host: "github.com", |
| 62 | + Path: "go-gitea/gitea.git", |
| 63 | + }, |
| 64 | + extraMark: 1, |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + kase: "ssh://[email protected]/go-gitea/gitea.git", |
| 69 | + expected: &GitURL{ |
| 70 | + URL: &url.URL{ |
| 71 | + Scheme: "ssh", |
| 72 | + User: url.User("git"), |
| 73 | + Host: "github.com", |
| 74 | + Path: "/go-gitea/gitea.git", |
| 75 | + }, |
| 76 | + extraMark: 0, |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + kase: "ssh://git@[::1]/go-gitea/gitea.git", |
| 81 | + expected: &GitURL{ |
| 82 | + URL: &url.URL{ |
| 83 | + Scheme: "ssh", |
| 84 | + User: url.User("git"), |
| 85 | + Host: "[::1]", |
| 86 | + Path: "/go-gitea/gitea.git", |
| 87 | + }, |
| 88 | + extraMark: 0, |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + kase: "/repositories/go-gitea/gitea.git", |
| 93 | + expected: &GitURL{ |
| 94 | + URL: &url.URL{ |
| 95 | + Scheme: "file", |
| 96 | + Path: "/repositories/go-gitea/gitea.git", |
| 97 | + }, |
| 98 | + extraMark: 2, |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + kase: "file:///repositories/go-gitea/gitea.git", |
| 103 | + expected: &GitURL{ |
| 104 | + URL: &url.URL{ |
| 105 | + Scheme: "file", |
| 106 | + Path: "/repositories/go-gitea/gitea.git", |
| 107 | + }, |
| 108 | + extraMark: 0, |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + kase: "https://github.com/go-gitea/gitea.git", |
| 113 | + expected: &GitURL{ |
| 114 | + URL: &url.URL{ |
| 115 | + Scheme: "https", |
| 116 | + Host: "github.com", |
| 117 | + Path: "/go-gitea/gitea.git", |
| 118 | + }, |
| 119 | + extraMark: 0, |
| 120 | + }, |
| 121 | + }, |
| 122 | + { |
| 123 | + kase: "https://git:[email protected]/go-gitea/gitea.git", |
| 124 | + expected: &GitURL{ |
| 125 | + URL: &url.URL{ |
| 126 | + Scheme: "https", |
| 127 | + Host: "github.com", |
| 128 | + User: url.UserPassword("git", "git"), |
| 129 | + Path: "/go-gitea/gitea.git", |
| 130 | + }, |
| 131 | + extraMark: 0, |
| 132 | + }, |
| 133 | + }, |
| 134 | + { |
| 135 | + kase: "https://[fe80:14fc:cec5:c174:d88%2510]:20/go-gitea/gitea.git", |
| 136 | + expected: &GitURL{ |
| 137 | + URL: &url.URL{ |
| 138 | + Scheme: "https", |
| 139 | + Host: "[fe80:14fc:cec5:c174:d88%10]:20", |
| 140 | + Path: "/go-gitea/gitea.git", |
| 141 | + }, |
| 142 | + extraMark: 0, |
| 143 | + }, |
| 144 | + }, |
| 145 | + |
| 146 | + { |
| 147 | + kase: "git://github.com/go-gitea/gitea.git", |
| 148 | + expected: &GitURL{ |
| 149 | + URL: &url.URL{ |
| 150 | + Scheme: "git", |
| 151 | + Host: "github.com", |
| 152 | + Path: "/go-gitea/gitea.git", |
| 153 | + }, |
| 154 | + extraMark: 0, |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + for _, kase := range kases { |
| 160 | + t.Run(kase.kase, func(t *testing.T) { |
| 161 | + u, err := Parse(kase.kase) |
| 162 | + assert.NoError(t, err) |
| 163 | + assert.EqualValues(t, kase.expected.extraMark, u.extraMark) |
| 164 | + assert.EqualValues(t, *kase.expected, *u) |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments