sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/internal/test/fake_github.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "net/url" 23 24 "github.com/google/go-github/v53/github" 25 ) 26 27 const baseURLPath = "/api-v3" 28 29 // NewFakeGitHub sets up a test HTTP server along with a github.Client that is 30 // configured to talk to that test server. Tests should register handlers on 31 // mux which provide mock responses for the API method being tested. 32 func NewFakeGitHub() (client *github.Client, mux *http.ServeMux, teardown func()) { 33 // mux is the HTTP request multiplexer used with the test server. 34 mux = http.NewServeMux() 35 36 apiHandler := http.NewServeMux() 37 apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux)) 38 39 // server is a test HTTP server used to provide mock API responses. 40 server := httptest.NewServer(apiHandler) 41 42 // client is the GitHub client being tested and is configured to use test server. 43 client = github.NewClient(nil) 44 url, _ := url.Parse(server.URL + baseURLPath + "/") 45 client.BaseURL = url 46 client.UploadURL = url 47 48 return client, mux, server.Close 49 }