github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+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  
     8  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     9  	"github.com/cloudfoundry/cli/cf/errors"
    10  	. "github.com/cloudfoundry/cli/cf/net"
    11  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var failingUAARequest = func(writer http.ResponseWriter, request *http.Request) {
    18  	writer.WriteHeader(http.StatusBadRequest)
    19  	jsonResponse := `{ "error": "foo", "error_description": "The foo is wrong..." }`
    20  	fmt.Fprintln(writer, jsonResponse)
    21  }
    22  
    23  var _ = Describe("UAA Gateway", func() {
    24  	var gateway Gateway
    25  	var config core_config.Reader
    26  
    27  	BeforeEach(func() {
    28  		config = testconfig.NewRepository()
    29  		gateway = NewUAAGateway(config, &testterm.FakeUI{})
    30  	})
    31  
    32  	It("parses error responses", func() {
    33  		ts := httptest.NewTLSServer(http.HandlerFunc(failingUAARequest))
    34  		defer ts.Close()
    35  		gateway.SetTrustedCerts(ts.TLS.Certificates)
    36  
    37  		request, apiErr := gateway.NewRequest("GET", ts.URL, "TOKEN", nil)
    38  		_, apiErr = gateway.PerformRequest(request)
    39  
    40  		Expect(apiErr).NotTo(BeNil())
    41  		Expect(apiErr.Error()).To(ContainSubstring("The foo is wrong"))
    42  		Expect(apiErr.(errors.HttpError).ErrorCode()).To(ContainSubstring("foo"))
    43  	})
    44  })