-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cli#1626 from cli/ghe-auth-tweaks
Make GitHub remote parsing and authentication stricter
- Loading branch information
Showing
19 changed files
with
555 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package git | ||
|
||
import "testing" | ||
|
||
func TestIsURL(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
url string | ||
want bool | ||
}{ | ||
{ | ||
name: "scp-like", | ||
url: "[email protected]:owner/repo", | ||
want: true, | ||
}, | ||
{ | ||
name: "scp-like with no user", | ||
url: "example.com:owner/repo", | ||
want: false, | ||
}, | ||
{ | ||
name: "ssh", | ||
url: "ssh://[email protected]/owner/repo", | ||
want: true, | ||
}, | ||
{ | ||
name: "git", | ||
url: "git://example.com/owner/repo", | ||
want: true, | ||
}, | ||
{ | ||
name: "https", | ||
url: "https://example.com/owner/repo.git", | ||
want: true, | ||
}, | ||
{ | ||
name: "no protocol", | ||
url: "example.com/owner/repo", | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IsURL(tt.url); got != tt.want { | ||
t.Errorf("IsURL() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestParseURL(t *testing.T) { | ||
type url struct { | ||
Scheme string | ||
User string | ||
Host string | ||
Path string | ||
} | ||
tests := []struct { | ||
name string | ||
url string | ||
want url | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "HTTPS", | ||
url: "https://example.com/owner/repo.git", | ||
want: url{ | ||
Scheme: "https", | ||
User: "", | ||
Host: "example.com", | ||
Path: "/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "HTTP", | ||
url: "http://example.com/owner/repo.git", | ||
want: url{ | ||
Scheme: "http", | ||
User: "", | ||
Host: "example.com", | ||
Path: "/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "git", | ||
url: "git://example.com/owner/repo.git", | ||
want: url{ | ||
Scheme: "git", | ||
User: "", | ||
Host: "example.com", | ||
Path: "/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "ssh", | ||
url: "ssh://[email protected]/owner/repo.git", | ||
want: url{ | ||
Scheme: "ssh", | ||
User: "git", | ||
Host: "example.com", | ||
Path: "/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "ssh with port", | ||
url: "ssh://[email protected]:443/owner/repo.git", | ||
want: url{ | ||
Scheme: "ssh", | ||
User: "git", | ||
Host: "example.com", | ||
Path: "/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "git+ssh", | ||
url: "git+ssh://example.com/owner/repo.git", | ||
want: url{ | ||
Scheme: "ssh", | ||
User: "", | ||
Host: "example.com", | ||
Path: "/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "scp-like", | ||
url: "[email protected]:owner/repo.git", | ||
want: url{ | ||
Scheme: "ssh", | ||
User: "git", | ||
Host: "example.com", | ||
Path: "/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "scp-like, leading slash", | ||
url: "[email protected]:/owner/repo.git", | ||
want: url{ | ||
Scheme: "ssh", | ||
User: "git", | ||
Host: "example.com", | ||
Path: "/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "file protocol", | ||
url: "file:///example.com/owner/repo.git", | ||
want: url{ | ||
Scheme: "file", | ||
User: "", | ||
Host: "", | ||
Path: "/example.com/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "file path", | ||
url: "/example.com/owner/repo.git", | ||
want: url{ | ||
Scheme: "", | ||
User: "", | ||
Host: "", | ||
Path: "/example.com/owner/repo.git", | ||
}, | ||
}, | ||
{ | ||
name: "Windows file path", | ||
url: "C:\\example.com\\owner\\repo.git", | ||
want: url{ | ||
Scheme: "c", | ||
User: "", | ||
Host: "", | ||
Path: "", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
u, err := ParseURL(tt.url) | ||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("got error: %v", err) | ||
} | ||
if u.Scheme != tt.want.Scheme { | ||
t.Errorf("expected scheme %q, got %q", tt.want.Scheme, u.Scheme) | ||
} | ||
if u.User.Username() != tt.want.User { | ||
t.Errorf("expected user %q, got %q", tt.want.User, u.User.Username()) | ||
} | ||
if u.Host != tt.want.Host { | ||
t.Errorf("expected host %q, got %q", tt.want.Host, u.Host) | ||
} | ||
if u.Path != tt.want.Path { | ||
t.Errorf("expected path %q, got %q", tt.want.Path, u.Path) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.