github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/net/cloud_controller_gateway_test.go (about)

     1  package net_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"time"
    10  
    11  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    12  	"code.cloudfoundry.org/cli/cf/errors"
    13  	. "code.cloudfoundry.org/cli/cf/net"
    14  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    15  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    16  
    17  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var failingCloudControllerRequest = func(writer http.ResponseWriter, request *http.Request) {
    23  	writer.WriteHeader(http.StatusBadRequest)
    24  	jsonResponse := `{ "code": 210003, "description": "The host is taken: test1" }`
    25  	fmt.Fprintln(writer, jsonResponse)
    26  }
    27  
    28  var invalidTokenCloudControllerRequest = func(writer http.ResponseWriter, request *http.Request) {
    29  	writer.WriteHeader(http.StatusBadRequest)
    30  	jsonResponse := `{ "code": 1000, "description": "The token is invalid" }`
    31  	fmt.Fprintln(writer, jsonResponse)
    32  }
    33  
    34  var _ = Describe("Cloud Controller Gateway", func() {
    35  	var gateway Gateway
    36  	var config coreconfig.Reader
    37  	var timeout string
    38  
    39  	BeforeEach(func() {
    40  		timeout = "1"
    41  	})
    42  
    43  	JustBeforeEach(func() {
    44  		config = testconfig.NewRepository()
    45  		gateway = NewCloudControllerGateway(config, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), timeout)
    46  	})
    47  
    48  	It("parses error responses", func() {
    49  		ts := httptest.NewTLSServer(http.HandlerFunc(failingCloudControllerRequest))
    50  		ts.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
    51  		defer ts.Close()
    52  		gateway.SetTrustedCerts(ts.TLS.Certificates)
    53  
    54  		request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil)
    55  		_, apiErr = gateway.PerformRequest(request)
    56  
    57  		Expect(apiErr).NotTo(BeNil())
    58  		Expect(apiErr.Error()).To(ContainSubstring("The host is taken: test1"))
    59  		Expect(apiErr.(errors.HTTPError).ErrorCode()).To(ContainSubstring("210003"))
    60  	})
    61  
    62  	It("parses invalid token responses", func() {
    63  		ts := httptest.NewTLSServer(http.HandlerFunc(invalidTokenCloudControllerRequest))
    64  		ts.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
    65  		defer ts.Close()
    66  		gateway.SetTrustedCerts(ts.TLS.Certificates)
    67  
    68  		request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil)
    69  		_, apiErr = gateway.PerformRequest(request)
    70  
    71  		Expect(apiErr).NotTo(BeNil())
    72  		Expect(apiErr.Error()).To(ContainSubstring("The token is invalid"))
    73  		Expect(apiErr.(*errors.InvalidTokenError)).To(HaveOccurred())
    74  	})
    75  
    76  	It("uses the set dial timeout", func() {
    77  		Expect(gateway.DialTimeout).To(Equal(1 * time.Second))
    78  	})
    79  
    80  	Context("with an invalid timeout", func() {
    81  		BeforeEach(func() {
    82  			timeout = ""
    83  		})
    84  
    85  		It("uses the default dial timeout", func() {
    86  			Expect(gateway.DialTimeout).To(Equal(5 * time.Second))
    87  		})
    88  	})
    89  })