github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/cf/net/uaa_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 failingUAARequest = func(writer http.ResponseWriter, request *http.Request) {
    20  	writer.WriteHeader(http.StatusBadRequest)
    21  	jsonResponse := `{ "error": "foo", "error_description": "The foo is wrong..." }`
    22  	fmt.Fprintln(writer, jsonResponse)
    23  }
    24  
    25  var _ = Describe("UAA Gateway", func() {
    26  	var gateway Gateway
    27  	var config coreconfig.Reader
    28  	var timeout string
    29  
    30  	BeforeEach(func() {
    31  		config = testconfig.NewRepository()
    32  		timeout = "1"
    33  	})
    34  
    35  	JustBeforeEach(func() {
    36  		gateway = NewUAAGateway(config, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), timeout)
    37  	})
    38  
    39  	It("parses error responses", func() {
    40  		ts := httptest.NewTLSServer(http.HandlerFunc(failingUAARequest))
    41  		defer ts.Close()
    42  		gateway.SetTrustedCerts(ts.TLS.Certificates)
    43  
    44  		request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil)
    45  		_, apiErr = gateway.PerformRequest(request)
    46  
    47  		Expect(apiErr).NotTo(BeNil())
    48  		Expect(apiErr.Error()).To(ContainSubstring("The foo is wrong"))
    49  		Expect(apiErr.(errors.HTTPError).ErrorCode()).To(ContainSubstring("foo"))
    50  	})
    51  
    52  	It("uses the set dial timeout", func() {
    53  		Expect(gateway.DialTimeout).To(Equal(1 * time.Second))
    54  	})
    55  
    56  	Context("with an invalid timeout", func() {
    57  		BeforeEach(func() {
    58  			timeout = ""
    59  		})
    60  
    61  		It("uses the default dial timeout", func() {
    62  			Expect(gateway.DialTimeout).To(Equal(5 * time.Second))
    63  		})
    64  	})
    65  })