github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/helpers/fake_server.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	. "github.com/onsi/gomega"
     8  	. "github.com/onsi/gomega/gexec"
     9  	. "github.com/onsi/gomega/ghttp"
    10  )
    11  
    12  const (
    13  	DefaultV2Version string = "2.100.0"
    14  	DefaultV3Version string = "3.33.3"
    15  )
    16  
    17  func StartAndTargetServerWithAPIVersions(v2Version string, v3Version string) *Server {
    18  	server := StartServerWithAPIVersions(v2Version, v3Version)
    19  	Eventually(CF("api", server.URL(), "--skip-ssl-validation")).Should(Exit(0))
    20  
    21  	return server
    22  }
    23  
    24  func StartServerWithMinimumCLIVersion(minCLIVersion string) *Server {
    25  	return startServerWithVersions(DefaultV2Version, DefaultV3Version, &minCLIVersion)
    26  }
    27  
    28  func StartServerWithAPIVersions(v2Version string, v3Version string) *Server {
    29  	return startServerWithVersions(v2Version, v3Version, nil)
    30  }
    31  
    32  func startServerWithVersions(v2Version string, v3Version string, minimumCLIVersion *string) *Server {
    33  	server := NewTLSServer()
    34  
    35  	rootResponse := fmt.Sprintf(`{
    36     "links": {
    37        "self": {
    38           "href": "%[1]s"
    39        },
    40        "cloud_controller_v2": {
    41           "href": "%[1]s/v2",
    42           "meta": {
    43              "version": "%[2]s"
    44           }
    45        },
    46        "cloud_controller_v3": {
    47           "href": "%[1]s/v3",
    48           "meta": {
    49              "version": "%[3]s"
    50           }
    51        },
    52        "network_policy_v0": {
    53           "href": "%[1]s/networking/v0/external"
    54        },
    55        "network_policy_v1": {
    56           "href": "%[1]s/networking/v1/external"
    57        },
    58        "uaa": {
    59           "href": "%[1]s"
    60        },
    61        "logging": {
    62           "href": "wss://unused:443"
    63        },
    64        "app_ssh": {
    65           "href": "unused:2222",
    66           "meta": {
    67              "host_key_fingerprint": "unused",
    68              "oauth_client": "ssh-proxy"
    69           }
    70        }
    71     }
    72   }`, server.URL(), v2Version, v3Version)
    73  
    74  	v2InfoResponse := struct {
    75  		APIVersion            string  `json:"api_version"`
    76  		AuthorizationEndpoint string  `json:"authorization_endpoint"`
    77  		MinCLIVersion         *string `json:"min_cli_version"`
    78  	}{
    79  		APIVersion:            v2Version,
    80  		AuthorizationEndpoint: server.URL(),
    81  		MinCLIVersion:         minimumCLIVersion}
    82  
    83  	server.RouteToHandler(http.MethodGet, "/v2/info", RespondWithJSONEncoded(http.StatusOK, v2InfoResponse))
    84  	server.RouteToHandler(http.MethodGet, "/v3", func(res http.ResponseWriter, req *http.Request) {
    85  		res.WriteHeader(http.StatusOK)
    86  		res.Write([]byte(`{"links":{}}`))
    87  	})
    88  	server.RouteToHandler(http.MethodGet, "/login", func(res http.ResponseWriter, req *http.Request) {
    89  		res.WriteHeader(http.StatusOK)
    90  		res.Write([]byte(`{"links":{}}`))
    91  	})
    92  	server.RouteToHandler(http.MethodGet, "/", func(res http.ResponseWriter, req *http.Request) {
    93  		res.WriteHeader(http.StatusOK)
    94  		res.Write([]byte(rootResponse))
    95  	})
    96  
    97  	return server
    98  }