|
| 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 | +) |
0 commit comments