github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/uaa/uaa_connection_test.go (about)

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