Skip to content

Commit 8763279

Browse files
committed
Add ghinstance tests
1 parent c1c836a commit 8763279

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

internal/ghinstance/host.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import (
77

88
const defaultHostname = "github.com"
99

10+
// Default returns the host name of the default GitHub instance
1011
func Default() string {
1112
return defaultHostname
1213
}
1314

15+
// IsEnterprise reports whether a non-normalized host name looks like a GHE instance
1416
func IsEnterprise(h string) bool {
1517
return NormalizeHostname(h) != defaultHostname
1618
}
1719

20+
// NormalizeHostname returns the canonical host name of a GitHub instance
1821
func NormalizeHostname(h string) string {
1922
hostname := strings.ToLower(h)
2023
if strings.HasSuffix(hostname, "."+defaultHostname) {

internal/ghinstance/host_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package ghinstance
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsEnterprise(t *testing.T) {
8+
tests := []struct {
9+
host string
10+
want bool
11+
}{
12+
{
13+
host: "github.com",
14+
want: false,
15+
},
16+
{
17+
host: "api.github.com",
18+
want: false,
19+
},
20+
{
21+
host: "ghe.io",
22+
want: true,
23+
},
24+
{
25+
host: "example.com",
26+
want: true,
27+
},
28+
}
29+
for _, tt := range tests {
30+
t.Run(tt.host, func(t *testing.T) {
31+
if got := IsEnterprise(tt.host); got != tt.want {
32+
t.Errorf("IsEnterprise() = %v, want %v", got, tt.want)
33+
}
34+
})
35+
}
36+
}
37+
38+
func TestNormalizeHostname(t *testing.T) {
39+
tests := []struct {
40+
host string
41+
want string
42+
}{
43+
{
44+
host: "GitHub.com",
45+
want: "github.com",
46+
},
47+
{
48+
host: "api.github.com",
49+
want: "github.com",
50+
},
51+
{
52+
host: "ssh.github.com",
53+
want: "github.com",
54+
},
55+
{
56+
host: "upload.github.com",
57+
want: "github.com",
58+
},
59+
{
60+
host: "GHE.IO",
61+
want: "ghe.io",
62+
},
63+
{
64+
host: "git.my.org",
65+
want: "git.my.org",
66+
},
67+
}
68+
for _, tt := range tests {
69+
t.Run(tt.host, func(t *testing.T) {
70+
if got := NormalizeHostname(tt.host); got != tt.want {
71+
t.Errorf("NormalizeHostname() = %v, want %v", got, tt.want)
72+
}
73+
})
74+
}
75+
}
76+
77+
func TestGraphQLEndpoint(t *testing.T) {
78+
tests := []struct {
79+
host string
80+
want string
81+
}{
82+
{
83+
host: "github.com",
84+
want: "https://api.github.com/graphql",
85+
},
86+
{
87+
host: "ghe.io",
88+
want: "https://ghe.io/api/graphql",
89+
},
90+
}
91+
for _, tt := range tests {
92+
t.Run(tt.host, func(t *testing.T) {
93+
if got := GraphQLEndpoint(tt.host); got != tt.want {
94+
t.Errorf("GraphQLEndpoint() = %v, want %v", got, tt.want)
95+
}
96+
})
97+
}
98+
}
99+
100+
func TestRESTPrefix(t *testing.T) {
101+
tests := []struct {
102+
host string
103+
want string
104+
}{
105+
{
106+
host: "github.com",
107+
want: "https://api.github.com/",
108+
},
109+
{
110+
host: "ghe.io",
111+
want: "https://ghe.io/api/v3/",
112+
},
113+
}
114+
for _, tt := range tests {
115+
t.Run(tt.host, func(t *testing.T) {
116+
if got := RESTPrefix(tt.host); got != tt.want {
117+
t.Errorf("RESTPrefix() = %v, want %v", got, tt.want)
118+
}
119+
})
120+
}
121+
}

0 commit comments

Comments
 (0)