Skip to content

Commit f6ddbd7

Browse files
committed
chore: Replace external GitHub mock with local implementation
Removed dependency on github.com/migueleliasweb/go-github-mock and replaced its usage in tests with a local mock implementation in github_server_test.go. Updated auth_test.go to use the new local mock, and cleaned up go.mod and go.sum to remove unused dependencies.
1 parent 96cdd66 commit f6ddbd7

File tree

4 files changed

+114
-25
lines changed

4 files changed

+114
-25
lines changed

auth_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
jwt "github.com/golang-jwt/jwt/v5"
1515
"github.com/google/go-github/v74/github"
16-
"github.com/migueleliasweb/go-github-mock/src/mock"
1716
"golang.org/x/oauth2"
1817
)
1918

@@ -159,9 +158,9 @@ func Test_installationTokenSource_Token(t *testing.T) {
159158
now := time.Now().UTC()
160159
expiration := now.Add(10 * time.Minute)
161160

162-
mockedHTTPClient := mock.NewMockedHTTPClient(
163-
mock.WithRequestMatch(
164-
mock.PostAppInstallationsAccessTokensByInstallationId,
161+
mockedHTTPClient, cleanupSuccess := newMockedHTTPClient(
162+
withRequestMatch(
163+
postAppInstallationsAccessTokensByInstallationID,
165164
github.InstallationToken{
166165
Token: github.Ptr("mocked-installation-token"),
167166
ExpiresAt: &github.Timestamp{
@@ -179,15 +178,17 @@ func Test_installationTokenSource_Token(t *testing.T) {
179178
},
180179
),
181180
)
181+
defer cleanupSuccess()
182182

183-
errMockedHTTPClient := mock.NewMockedHTTPClient(
184-
mock.WithRequestMatchHandler(
185-
mock.PostAppInstallationsAccessTokensByInstallationId,
183+
errMockedHTTPClient, cleanupError := newMockedHTTPClient(
184+
withRequestMatchHandler(
185+
postAppInstallationsAccessTokensByInstallationID,
186186
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
187187
w.WriteHeader(http.StatusInternalServerError)
188188
_, _ = w.Write([]byte(`{"message":"Internal Server Error"}`))
189189
}),
190190
))
191+
defer cleanupError()
191192

192193
privateKey, err := generatePrivateKey()
193194
if err != nil {

github_server_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Package githubauth provides utilities for GitHub authentication,
2+
// including generating and using GitHub App tokens and installation tokens.
3+
//
4+
// This file contains local mock implementations for testing purposes.
5+
package githubauth
6+
7+
import (
8+
"encoding/json"
9+
"net/http"
10+
"net/http/httptest"
11+
"strings"
12+
)
13+
14+
// mockedHTTPClient represents a mock HTTP client for testing GitHub API interactions.
15+
type mockedHTTPClient struct {
16+
server *httptest.Server
17+
handlers map[string]http.HandlerFunc
18+
}
19+
20+
// mockOption is a functional option for configuring mockedHTTPClient.
21+
type mockOption func(*mockedHTTPClient)
22+
23+
// withRequestMatch configures the mock to return a specific response for a given endpoint pattern.
24+
func withRequestMatch(endpoint string, response any) mockOption {
25+
return func(m *mockedHTTPClient) {
26+
m.handlers[endpoint] = func(w http.ResponseWriter, _ *http.Request) {
27+
w.Header().Set("Content-Type", "application/json")
28+
json.NewEncoder(w).Encode(response)
29+
}
30+
}
31+
}
32+
33+
// withRequestMatchHandler configures the mock to use a custom handler for a given endpoint pattern.
34+
func withRequestMatchHandler(endpoint string, handler http.HandlerFunc) mockOption {
35+
return func(m *mockedHTTPClient) {
36+
m.handlers[endpoint] = handler
37+
}
38+
}
39+
40+
// newMockedHTTPClient creates a new mock HTTP client with the provided options.
41+
// Returns the HTTP client and a cleanup function that should be called to close the test server.
42+
func newMockedHTTPClient(opts ...mockOption) (*http.Client, func()) {
43+
m := &mockedHTTPClient{
44+
handlers: make(map[string]http.HandlerFunc),
45+
}
46+
47+
for _, opt := range opts {
48+
opt(m)
49+
}
50+
51+
m.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52+
key := r.Method + " " + r.URL.Path
53+
54+
if handler, exists := m.handlers[key]; exists {
55+
handler(w, r)
56+
return
57+
}
58+
59+
for pattern, handler := range m.handlers {
60+
if matchesPattern(key, pattern) {
61+
handler(w, r)
62+
return
63+
}
64+
}
65+
66+
w.WriteHeader(http.StatusNotFound)
67+
w.Write([]byte(`{"message":"Not Found"}`))
68+
}))
69+
70+
client := &http.Client{
71+
Transport: &mockTransport{
72+
server: m.server,
73+
},
74+
}
75+
76+
return client, m.server.Close
77+
}
78+
79+
// mockTransport implements http.RoundTripper to redirect requests to our mock server.
80+
type mockTransport struct {
81+
server *httptest.Server
82+
}
83+
84+
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
85+
req.URL.Scheme = "http"
86+
req.URL.Host = t.server.URL[7:]
87+
return http.DefaultTransport.RoundTrip(req)
88+
}
89+
90+
// matchesPattern performs simple pattern matching for GitHub API endpoints.
91+
func matchesPattern(request, pattern string) bool {
92+
// Handle the specific case used in tests: POST /app/installations/{installation_id}/access_tokens
93+
if pattern == "POST /app/installations/{installation_id}/access_tokens" {
94+
return strings.HasPrefix(request, "POST /app/installations/") &&
95+
strings.HasSuffix(request, "/access_tokens")
96+
}
97+
98+
// For other patterns, use exact matching
99+
return request == pattern
100+
}
101+
102+
// Common GitHub API endpoint patterns used in tests
103+
const (
104+
postAppInstallationsAccessTokensByInstallationID = "POST /app/installations/{installation_id}/access_tokens"
105+
)

go.mod

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,4 @@ require (
88
golang.org/x/oauth2 v0.31.0
99
)
1010

11-
require (
12-
github.com/google/go-github/v73 v73.0.0 // indirect
13-
github.com/gorilla/mux v1.8.1 // indirect
14-
golang.org/x/time v0.12.0 // indirect
15-
)
16-
17-
require (
18-
github.com/google/go-querystring v1.1.0 // indirect
19-
github.com/migueleliasweb/go-github-mock v1.4.0
20-
)
11+
require github.com/google/go-querystring v1.1.0 // indirect

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@ github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArs
33
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
44
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
55
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
6-
github.com/google/go-github/v73 v73.0.0 h1:aR+Utnh+Y4mMkS+2qLQwcQ/cF9mOTpdwnzlaw//rG24=
7-
github.com/google/go-github/v73 v73.0.0/go.mod h1:fa6w8+/V+edSU0muqdhCVY7Beh1M8F1IlQPZIANKIYw=
86
github.com/google/go-github/v74 v74.0.0 h1:yZcddTUn8DPbj11GxnMrNiAnXH14gNs559AsUpNpPgM=
97
github.com/google/go-github/v74 v74.0.0/go.mod h1:ubn/YdyftV80VPSI26nSJvaEsTOnsjrxG3o9kJhcyak=
108
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
119
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
12-
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
13-
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
14-
github.com/migueleliasweb/go-github-mock v1.4.0 h1:pQ6K8r348m2q79A8Khb0PbEeNQV7t3h1xgECV+jNpXk=
15-
github.com/migueleliasweb/go-github-mock v1.4.0/go.mod h1:/DUmhXkxrgVlDOVBqGoUXkV4w0ms5n1jDQHotYm135o=
1610
golang.org/x/oauth2 v0.31.0 h1:8Fq0yVZLh4j4YA47vHKFTa9Ew5XIrCP8LC6UeNZnLxo=
1711
golang.org/x/oauth2 v0.31.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
18-
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
19-
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
2012
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)