sigs.k8s.io/cluster-api@v1.7.1/internal/goproxy/test/test_utils.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 contains test util functions for goproxy. 18 package test 19 20 import ( 21 "net/http" 22 "net/http/httptest" 23 "net/url" 24 "testing" 25 ) 26 27 // NewFakeGoproxy sets up a test HTTP server along with a github.Client that is 28 // configured to talk to that test server. Tests should register handlers on 29 // mux which provide mock responses for the API method being tested. 30 func NewFakeGoproxy() (scheme string, host string, mux *http.ServeMux, teardown func()) { 31 // mux is the HTTP request multiplexer used with the test server. 32 mux = http.NewServeMux() 33 34 apiHandler := http.NewServeMux() 35 apiHandler.Handle("/", mux) 36 37 // server is a test HTTP server used to provide mock API responses. 38 server := httptest.NewServer(apiHandler) 39 40 // client is the GitHub client being tested and is configured to use test server. 41 url, _ := url.Parse(server.URL + "/") 42 return url.Scheme, url.Host, mux, server.Close 43 } 44 45 // HTTPTestMethod reports error http.Request does not return want string. 46 func HTTPTestMethod(t *testing.T, r *http.Request, want string) { 47 t.Helper() 48 49 if got := r.Method; got != want { 50 t.Errorf("Request method: %v, want %v", got, want) 51 } 52 }