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

     1  package runtime
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"os"
     8  	"path"
     9  	"strings"
    10  )
    11  
    12  const kymaComponentsYAMLFileName = "kyma-components.yaml"
    13  
    14  type fakeHTTPDoer struct{}
    15  
    16  func (f *fakeHTTPDoer) Do(req *http.Request) (*http.Response, error) {
    17  	urlPathSplit := strings.Split(req.URL.Path, "/")
    18  	if !isSupportedVersion(urlPathSplit[5]) {
    19  		return &http.Response{
    20  			Status:     "404 Not Found",
    21  			StatusCode: 404,
    22  			Body:       http.NoBody,
    23  			Request:    req,
    24  		}, nil
    25  	}
    26  
    27  	yamlFilePath := path.Join("testdata", kymaComponentsYAMLFileName)
    28  	contents, err := os.ReadFile(yamlFilePath)
    29  	if err != nil {
    30  		return &http.Response{
    31  			Status:     "500 Internal Server Error",
    32  			StatusCode: 500,
    33  			Body:       http.NoBody,
    34  			Request:    req,
    35  		}, nil
    36  	}
    37  
    38  	body := io.NopCloser(bytes.NewReader(contents))
    39  
    40  	return &http.Response{
    41  		Status:     "200 OK",
    42  		StatusCode: 200,
    43  		Body:       body,
    44  		Request:    req,
    45  	}, nil
    46  }
    47  
    48  func isSupportedVersion(version string) bool {
    49  	return strings.HasPrefix(version, "PR-") ||
    50  		strings.HasPrefix(version, "main-") ||
    51  		strings.Split(version, ".")[0] == "2"
    52  }
    53  
    54  func NewFakeComponentsProvider() *ComponentsProvider {
    55  	return &ComponentsProvider{
    56  		httpClient: &fakeHTTPDoer{},
    57  	}
    58  }