github.com/openshift-online/ocm-sdk-go@v0.1.473/testing/transport.go (about) 1 /* 2 Copyright (c) 2021 Red Hat, Inc. 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 testing 18 19 import ( 20 "io" 21 "net/http" 22 "strings" 23 24 . "github.com/onsi/gomega" // nolint 25 ) 26 27 // TransportFunc is a function that implements the http.RoundTripper interface. This is intended to 28 // siplify writing tests that require custom round trippers because it doesn't require a new type. 29 // For example, to create a round tripper that always returns an error: 30 // 31 // transport := testing.TransportFunc(func (*http.Request) (*http.Response, error) { 32 // return nil, errors.New("my error") 33 // }) 34 type TransportFunc func(*http.Request) (*http.Response, error) 35 36 // RoundTrip is the implementation of the http.RoundTripper interface. 37 func (f TransportFunc) RoundTrip(request *http.Request) (response *http.Response, err error) { 38 response, err = f(request) 39 return 40 } 41 42 // ErrorTransport creates a transport that always returns the given error. 43 func ErrorTransport(err error) http.RoundTripper { 44 return TransportFunc(func(*http.Request) (*http.Response, error) { 45 return nil, err 46 }) 47 } 48 49 // TextTransport creates a transport that always returns the given status code and plan text body. 50 func TextTransport(code int, body string) http.RoundTripper { 51 return TransportFunc(func(request *http.Request) (response *http.Response, err error) { 52 response = &http.Response{ 53 StatusCode: code, 54 Body: io.NopCloser(strings.NewReader(body)), 55 Header: http.Header{ 56 "Content-Type": []string{"plain/text"}, 57 }, 58 } 59 return 60 }) 61 } 62 63 // JSONTransport creates a transport that always returns the given status code and JSON body. 64 func JSONTransport(code int, body string) http.RoundTripper { 65 return TransportFunc(func(request *http.Request) (response *http.Response, err error) { 66 response = &http.Response{ 67 StatusCode: code, 68 Body: io.NopCloser(strings.NewReader(body)), 69 Header: http.Header{ 70 "Content-Type": []string{"application/json"}, 71 }, 72 } 73 return 74 }) 75 } 76 77 // CombineTransports returns a transport that delegates to the given array of transports, in the 78 // given order. First request will go to the first transport, sceond request to the second transport, 79 // so on. 80 func CombineTransports(transports ...http.RoundTripper) http.RoundTripper { 81 i := 0 82 return TransportFunc(func(request *http.Request) (response *http.Response, err error) { 83 Expect(i).To(BeNumerically("<", len(transports))) 84 response, err = transports[i].RoundTrip(request) 85 i++ 86 return 87 }) 88 }