Go · 1667 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package repos_test
4
5 import (
6 "errors"
7 "strings"
8 "testing"
9
10 "github.com/tenseleyFlow/shithub/internal/repos"
11 )
12
13 func TestNormalizeSourceRemoteURL(t *testing.T) {
14 t.Parallel()
15 for _, tt := range []struct {
16 name string
17 raw string
18 want string
19 }{
20 {name: "https", raw: " https://github.com/FortranGoingOnForty/fgof-process.git ", want: "https://github.com/FortranGoingOnForty/fgof-process.git"},
21 {name: "http", raw: "HTTP://git.example.com/owner/repo.git", want: "http://git.example.com/owner/repo.git"},
22 {name: "empty clears", raw: " ", want: ""},
23 } {
24 tt := tt
25 t.Run(tt.name, func(t *testing.T) {
26 t.Parallel()
27 got, err := repos.NormalizeSourceRemoteURL(tt.raw)
28 if err != nil {
29 t.Fatalf("NormalizeSourceRemoteURL(%q): %v", tt.raw, err)
30 }
31 if got != tt.want {
32 t.Fatalf("NormalizeSourceRemoteURL(%q) = %q, want %q", tt.raw, got, tt.want)
33 }
34 })
35 }
36 }
37
38 func TestNormalizeSourceRemoteURLRejectsUnsafeShapes(t *testing.T) {
39 t.Parallel()
40 for _, raw := range []string{
41 "git@github.com:owner/repo.git",
42 "ssh://git@github.com/owner/repo.git",
43 "file:///tmp/repo.git",
44 "https://user:pass@example.com/owner/repo.git",
45 "https://example.com",
46 "https://example.com/owner/repo.git?token=secret",
47 "https://example.com/owner/repo.git#main",
48 strings.Repeat("a", repos.MaxSourceRemoteURLLen+1),
49 } {
50 raw := raw
51 t.Run(raw, func(t *testing.T) {
52 t.Parallel()
53 if got, err := repos.NormalizeSourceRemoteURL(raw); !errors.Is(err, repos.ErrInvalidSourceRemote) {
54 t.Fatalf("NormalizeSourceRemoteURL(%q) = %q, %v; want ErrInvalidSourceRemote", raw, got, err)
55 }
56 })
57 }
58 }
59