github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/uaa/uaa_connection_test.go (about)

     1  package uaa_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	. "code.cloudfoundry.org/cli/api/uaa"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/ghttp"
    11  )
    12  
    13  type DummyResponse struct {
    14  	Val1 string `json:"val1"`
    15  	Val2 int    `json:"val2"`
    16  }
    17  
    18  var _ = Describe("UAA Connection", func() {
    19  	var (
    20  		connection *UAAConnection
    21  		request    *http.Request
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		connection = NewConnection(true, 0)
    26  	})
    27  
    28  	Describe("Make", func() {
    29  		Describe("Data Unmarshalling", func() {
    30  			BeforeEach(func() {
    31  				response := `{
    32  					"val1":"2.59.0",
    33  					"val2":2
    34  				}`
    35  				server.AppendHandlers(
    36  					CombineHandlers(
    37  						VerifyRequest(http.MethodGet, "/v2/foo", ""),
    38  						RespondWith(http.StatusOK, response),
    39  					),
    40  				)
    41  
    42  				var err error
    43  				request, err = http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/foo", server.URL()), nil)
    44  				Expect(err).ToNot(HaveOccurred())
    45  			})
    46  
    47  			Context("when passed a response with a result set", func() {
    48  				It("unmarshals the data into a struct", func() {
    49  					var body DummyResponse
    50  					response := Response{
    51  						Result: &body,
    52  					}
    53  
    54  					err := connection.Make(request, &response)
    55  					Expect(err).NotTo(HaveOccurred())
    56  
    57  					Expect(body.Val1).To(Equal("2.59.0"))
    58  					Expect(body.Val2).To(Equal(2))
    59  				})
    60  			})
    61  
    62  			Context("when passed an empty response", func() {
    63  				It("skips the unmarshalling step", func() {
    64  					var response Response
    65  					err := connection.Make(request, &response)
    66  					Expect(err).NotTo(HaveOccurred())
    67  					Expect(response.Result).To(BeNil())
    68  				})
    69  			})
    70  		})
    71  
    72  		Describe("HTTP Response", func() {
    73  			var request *http.Request
    74  
    75  			BeforeEach(func() {
    76  				response := `{}`
    77  				server.AppendHandlers(
    78  					CombineHandlers(
    79  						VerifyRequest(http.MethodGet, "/v2/foo", ""),
    80  						RespondWith(http.StatusOK, response),
    81  					),
    82  				)
    83  
    84  				var err error
    85  				request, err = http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/foo", server.URL()), nil)
    86  				Expect(err).ToNot(HaveOccurred())
    87  			})
    88  
    89  			It("returns the status", func() {
    90  				response := Response{}
    91  
    92  				err := connection.Make(request, &response)
    93  				Expect(err).NotTo(HaveOccurred())
    94  
    95  				Expect(response.HTTPResponse.Status).To(Equal("200 OK"))
    96  			})
    97  		})
    98  
    99  		Describe("Errors", func() {
   100  			Context("when the server does not exist", func() {
   101  				BeforeEach(func() {
   102  					connection = NewConnection(false, 0)
   103  				})
   104  
   105  				It("returns a RequestError", func() {
   106  					request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/foo", "http://i.hope.this.doesnt.exist.com"), nil)
   107  					Expect(err).ToNot(HaveOccurred())
   108  
   109  					var response Response
   110  					err = connection.Make(request, &response)
   111  					Expect(err).To(HaveOccurred())
   112  
   113  					requestErr, ok := err.(RequestError)
   114  					Expect(ok).To(BeTrue())
   115  					Expect(requestErr.Error()).To(MatchRegexp(".*http://i.hope.this.doesnt.exist.com/v2/foo.*"))
   116  				})
   117  			})
   118  
   119  			Context("when the server does not have a verified certificate", func() {
   120  				Context("skipSSLValidation is false", func() {
   121  					BeforeEach(func() {
   122  						server.AppendHandlers(
   123  							CombineHandlers(
   124  								VerifyRequest(http.MethodGet, "/v2/foo"),
   125  							),
   126  						)
   127  
   128  						connection = NewConnection(false, 0)
   129  					})
   130  
   131  					It("returns a UnverifiedServerError", func() {
   132  						request, err := http.NewRequest(http.MethodGet, server.URL(), nil)
   133  						Expect(err).ToNot(HaveOccurred())
   134  
   135  						var response Response
   136  						err = connection.Make(request, &response)
   137  						Expect(err).To(MatchError(UnverifiedServerError{URL: server.URL()}))
   138  					})
   139  				})
   140  			})
   141  
   142  			Describe("RawHTTPStatusError", func() {
   143  				var uaaResponse string
   144  
   145  				BeforeEach(func() {
   146  					uaaResponse = `{
   147  						"error":"unauthorized",
   148  						"error_description":"Bad credentials"
   149  					}`
   150  
   151  					server.AppendHandlers(
   152  						CombineHandlers(
   153  							VerifyRequest(http.MethodGet, "/v2/foo"),
   154  							RespondWith(http.StatusUnauthorized, uaaResponse),
   155  						),
   156  					)
   157  				})
   158  
   159  				It("returns a RawHTTPStatusError", func() {
   160  					request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/foo", server.URL()), nil)
   161  					Expect(err).ToNot(HaveOccurred())
   162  
   163  					var response Response
   164  					err = connection.Make(request, &response)
   165  					Expect(err).To(MatchError(RawHTTPStatusError{
   166  						StatusCode:  http.StatusUnauthorized,
   167  						RawResponse: []byte(uaaResponse),
   168  					}))
   169  
   170  					Expect(server.ReceivedRequests()).To(HaveLen(1))
   171  				})
   172  			})
   173  		})
   174  	})
   175  })