github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/runtime/provider_fake.go (about)

     1  package runtime
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"path"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  type HTTPFakeClient struct {
    16  	t                 *testing.T
    17  	installerContent  string
    18  	componentsContent string
    19  	code              int
    20  
    21  	RequestURL string
    22  }
    23  
    24  func (f *HTTPFakeClient) Do(req *http.Request) (*http.Response, error) {
    25  	f.RequestURL = req.URL.String()
    26  
    27  	var body io.ReadCloser
    28  	if strings.Contains(f.RequestURL, "kyma-components.yaml") {
    29  		body = ioutil.NopCloser(bytes.NewReader([]byte(f.componentsContent)))
    30  	} else {
    31  		body = ioutil.NopCloser(bytes.NewReader([]byte(f.installerContent)))
    32  	}
    33  
    34  	return &http.Response{
    35  		StatusCode: f.code,
    36  		Body:       body,
    37  		Request:    req,
    38  	}, nil
    39  }
    40  
    41  func NewTestClient(t *testing.T, installerContent, componentsContent string, code int) *HTTPFakeClient {
    42  	return &HTTPFakeClient{
    43  		t:                 t,
    44  		code:              code,
    45  		installerContent:  installerContent,
    46  		componentsContent: componentsContent,
    47  	}
    48  }
    49  
    50  // WithHTTPClient is a helper method to use ONLY in tests
    51  func (r *ComponentsListProvider) WithHTTPClient(doer HTTPDoer) *ComponentsListProvider {
    52  	r.httpClient = doer
    53  
    54  	return r
    55  }
    56  
    57  // ReadYAMLFromFile is a helper method to use ONLY in tests
    58  func ReadYAMLFromFile(t *testing.T, yamlFileName string) string {
    59  	t.Helper()
    60  
    61  	filename := path.Join("testdata", yamlFileName)
    62  	yamlFile, err := ioutil.ReadFile(filename)
    63  	require.NoError(t, err)
    64  
    65  	return string(yamlFile)
    66  }