-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathapi_version_test.go
More file actions
92 lines (81 loc) · 2.76 KB
/
Copy pathapi_version_test.go
File metadata and controls
92 lines (81 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package transport
import (
"net/http"
"testing"
"github.com/github/github-mcp-server/pkg/http/headers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func TestAPIVersionTransport(t *testing.T) {
t.Parallel()
tests := []struct {
name string
url string
existingVersion string
wantVersion string
}{
{
name: "GitHub.com overrides the default version",
url: "https://api.github.com/repos/octo-org/octo-repo",
existingVersion: headers.GitHubEnterpriseServerAPIVersion,
wantVersion: headers.GitHubAPIVersion,
},
{
name: "GitHub Enterprise Cloud sets the new version",
url: "https://api.example.ghe.com/repos/octo-org/octo-repo",
wantVersion: headers.GitHubAPIVersion,
},
{
name: "GitHub Enterprise Server pins the compatibility version",
url: "https://github.example.com/api/v3/repos/octo-org/octo-repo",
existingVersion: headers.GitHubAPIVersion,
wantVersion: headers.GitHubEnterpriseServerAPIVersion,
},
{
name: "GitHub Enterprise Server sets the compatibility version",
url: "https://github.example.com/api/v3/repos/octo-org/octo-repo",
wantVersion: headers.GitHubEnterpriseServerAPIVersion,
},
{
name: "host classification is case insensitive",
url: "https://API.GITHUB.COM/repos/octo-org/octo-repo",
wantVersion: headers.GitHubAPIVersion,
},
{
name: "lookalike domain is treated as GitHub Enterprise Server",
url: "https://api.github.com.example.org/api/v3/",
wantVersion: headers.GitHubEnterpriseServerAPIVersion,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var gotVersion string
underlying := roundTripFunc(func(req *http.Request) (*http.Response, error) {
gotVersion = req.Header.Get(headers.GitHubAPIVersionHeader)
return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
Body: http.NoBody,
Request: req,
}, nil
})
req, err := http.NewRequest(http.MethodGet, tt.url, nil)
require.NoError(t, err)
if tt.existingVersion != "" {
req.Header.Set(headers.GitHubAPIVersionHeader, tt.existingVersion)
} else {
req.Header = nil
}
resp, err := (&APIVersionTransport{Transport: underlying}).RoundTrip(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, tt.wantVersion, gotVersion)
assert.Equal(t, tt.existingVersion, req.Header.Get(headers.GitHubAPIVersionHeader), "the original request must not be mutated")
})
}
}