github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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, 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 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 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 VerifyHeaderKV("Connection", "close"), 81 RespondWith(http.StatusOK, response), 82 ), 83 ) 84 85 var err error 86 request, err = http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/foo", server.URL()), nil) 87 Expect(err).ToNot(HaveOccurred()) 88 }) 89 90 It("returns the status", func() { 91 response := Response{} 92 93 err := connection.Make(request, &response) 94 Expect(err).NotTo(HaveOccurred()) 95 96 Expect(response.HTTPResponse.Status).To(Equal("200 OK")) 97 }) 98 }) 99 100 Describe("Errors", func() { 101 When("the server does not exist", func() { 102 BeforeEach(func() { 103 connection = NewConnection(false, true, 0) 104 }) 105 106 It("returns a RequestError", func() { 107 request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/foo", "http://i.hope.this.doesnt.exist.com"), nil) 108 Expect(err).ToNot(HaveOccurred()) 109 110 var response Response 111 err = connection.Make(request, &response) 112 Expect(err).To(HaveOccurred()) 113 114 requestErr, ok := err.(RequestError) 115 Expect(ok).To(BeTrue()) 116 Expect(requestErr.Error()).To(MatchRegexp(".*http://i.hope.this.doesnt.exist.com/v2/foo.*")) 117 }) 118 }) 119 120 When("the server does not have a verified certificate", func() { 121 Context("skipSSLValidation is false", func() { 122 BeforeEach(func() { 123 server.AppendHandlers( 124 CombineHandlers( 125 VerifyRequest(http.MethodGet, "/v2/foo"), 126 ), 127 ) 128 129 connection = NewConnection(false, true, 0) 130 }) 131 132 It("returns a UnverifiedServerError", func() { 133 request, err := http.NewRequest(http.MethodGet, server.URL(), nil) 134 Expect(err).ToNot(HaveOccurred()) 135 136 var response Response 137 err = connection.Make(request, &response) 138 Expect(err).To(MatchError(UnverifiedServerError{URL: server.URL()})) 139 }) 140 }) 141 }) 142 143 Describe("RawHTTPStatusError", func() { 144 var uaaResponse string 145 146 BeforeEach(func() { 147 uaaResponse = `{ 148 "error":"unauthorized", 149 "error_description":"Bad credentials" 150 }` 151 152 server.AppendHandlers( 153 CombineHandlers( 154 VerifyRequest(http.MethodGet, "/v2/foo"), 155 RespondWith(http.StatusUnauthorized, uaaResponse), 156 ), 157 ) 158 }) 159 160 It("returns a RawHTTPStatusError", func() { 161 request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/foo", server.URL()), nil) 162 Expect(err).ToNot(HaveOccurred()) 163 164 var response Response 165 err = connection.Make(request, &response) 166 Expect(err).To(MatchError(RawHTTPStatusError{ 167 StatusCode: http.StatusUnauthorized, 168 RawResponse: []byte(uaaResponse), 169 })) 170 171 Expect(server.ReceivedRequests()).To(HaveLen(1)) 172 }) 173 }) 174 }) 175 }) 176 })