github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/cf/net/routing_api_gateway_test.go (about)

     1  package net_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"time"
     8  
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/errors"
    11  	. "code.cloudfoundry.org/cli/cf/net"
    12  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    13  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    14  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var failingRoutingAPIRequest = func(writer http.ResponseWriter, request *http.Request) {
    20  	writer.WriteHeader(http.StatusBadRequest)
    21  	jsonResponse := `{ "name": "some-error", "message": "The host is taken: test1" }`
    22  	fmt.Fprintln(writer, jsonResponse)
    23  }
    24  
    25  var invalidTokenRoutingAPIRequest = func(writer http.ResponseWriter, request *http.Request) {
    26  	writer.WriteHeader(http.StatusUnauthorized)
    27  	jsonResponse := `{ "name": "UnauthorizedError", "message": "bad token!" }`
    28  	fmt.Fprintln(writer, jsonResponse)
    29  }
    30  
    31  var _ = Describe("Routing Api Gateway", func() {
    32  	var gateway Gateway
    33  	var config coreconfig.Reader
    34  	var fakeLogger *tracefakes.FakePrinter
    35  	var timeout string
    36  
    37  	BeforeEach(func() {
    38  		fakeLogger = new(tracefakes.FakePrinter)
    39  		config = testconfig.NewRepository()
    40  		timeout = "1"
    41  	})
    42  
    43  	JustBeforeEach(func() {
    44  		gateway = NewRoutingAPIGateway(config, time.Now, new(terminalfakes.FakeUI), fakeLogger, timeout)
    45  	})
    46  
    47  	It("parses error responses", func() {
    48  		ts := httptest.NewTLSServer(http.HandlerFunc(failingRoutingAPIRequest))
    49  		defer ts.Close()
    50  		gateway.SetTrustedCerts(ts.TLS.Certificates)
    51  
    52  		request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil)
    53  		_, apiErr = gateway.PerformRequest(request)
    54  
    55  		Expect(apiErr).NotTo(BeNil())
    56  		Expect(apiErr.Error()).To(ContainSubstring("The host is taken: test1"))
    57  		Expect(apiErr.(errors.HTTPError).ErrorCode()).To(ContainSubstring("some-error"))
    58  	})
    59  
    60  	It("parses invalid token responses", func() {
    61  		ts := httptest.NewTLSServer(http.HandlerFunc(invalidTokenRoutingAPIRequest))
    62  		defer ts.Close()
    63  		gateway.SetTrustedCerts(ts.TLS.Certificates)
    64  
    65  		request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil)
    66  		_, apiErr = gateway.PerformRequest(request)
    67  
    68  		Expect(apiErr).NotTo(BeNil())
    69  		Expect(apiErr.Error()).To(ContainSubstring("bad token"))
    70  		Expect(apiErr.(errors.HTTPError)).To(HaveOccurred())
    71  	})
    72  
    73  	Context("when the Routing API returns a invalid JSON", func() {
    74  		var invalidJSONResponse = func(writer http.ResponseWriter, request *http.Request) {
    75  			writer.WriteHeader(http.StatusUnauthorized)
    76  			jsonResponse := `¯\_(ツ)_/¯`
    77  			fmt.Fprintln(writer, jsonResponse)
    78  		}
    79  
    80  		It("returns a 500 http error", func() {
    81  			ts := httptest.NewTLSServer(http.HandlerFunc(invalidJSONResponse))
    82  			defer ts.Close()
    83  			gateway.SetTrustedCerts(ts.TLS.Certificates)
    84  
    85  			request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil)
    86  			_, apiErr = gateway.PerformRequest(request)
    87  
    88  			Expect(apiErr).NotTo(BeNil())
    89  			Expect(apiErr.(errors.HTTPError)).To(HaveOccurred())
    90  			Expect(apiErr.(errors.HTTPError).StatusCode()).To(Equal(http.StatusInternalServerError))
    91  		})
    92  	})
    93  
    94  	It("uses the set dial timeout", func() {
    95  		Expect(gateway.DialTimeout).To(Equal(1 * time.Second))
    96  	})
    97  
    98  	Context("with an invalid timeout", func() {
    99  		BeforeEach(func() {
   100  			timeout = ""
   101  		})
   102  
   103  		It("uses the default dial timeout", func() {
   104  			Expect(gateway.DialTimeout).To(Equal(5 * time.Second))
   105  		})
   106  	})
   107  })