Skip to content

Commit

Permalink
fix sm response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TalShorSap committed Jul 27, 2023
1 parent 63243b5 commit 6fcbae7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
7 changes: 0 additions & 7 deletions client/sm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,19 +534,12 @@ func handleResponseError(response *http.Response) error {
body = []byte(fmt.Sprintf("error reading response body: %s", err))
}

err = fmt.Errorf("StatusCode: %d Body: %s", response.StatusCode, body)
if response.Request != nil {
err = fmt.Errorf("request %s %s failed ", response.Request.Method, response.Request.URL)
}

smError := &ServiceManagerError{
StatusCode: response.StatusCode,
Description: "",
}
_ = json.Unmarshal(body, &smError)

smError.Description = err.Error() + smError.Description

return smError
}

Expand Down
49 changes: 24 additions & 25 deletions client/sm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var _ = Describe("Client test", func() {
})
It("should handle status code != 200", func() {
_, err := client.ListInstances(params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusCreated)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusCreated)
})
})

Expand All @@ -85,7 +85,7 @@ var _ = Describe("Client test", func() {
})
It("should handle status code > 299", func() {
_, err := client.ListInstances(params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusBadRequest)
})
})
})
Expand Down Expand Up @@ -113,7 +113,7 @@ var _ = Describe("Client test", func() {
})
It("should return 404", func() {
_, err := client.GetInstanceByID(instance.ID, params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusNotFound)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusNotFound)
})
})

Expand All @@ -126,8 +126,7 @@ var _ = Describe("Client test", func() {
It("should handle status code != 200", func() {
_, err := client.GetInstanceByID(instance.ID, params)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(types.ServiceInstancesURL))
Expect(err.(*ServiceManagerError).StatusCode).To(Equal(http.StatusCreated))
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusCreated)
})
})

Expand All @@ -140,7 +139,7 @@ var _ = Describe("Client test", func() {

It("should handle status code > 299", func() {
_, err := client.GetInstanceByID(instance.ID, params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusBadRequest)
})
})
})
Expand Down Expand Up @@ -305,33 +304,33 @@ var _ = Describe("Client test", func() {
})
It("should return error with status code", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusOK)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusOK)
Expect(res).To(BeNil())
})
})

Context("And status code is unsuccessful", func() {
BeforeEach(func() {
responseBody := []byte(`{ "description": "description", }`)
responseBody := []byte(`{ "description": "description"}`)
handlerDetails[0] = HandlerDetails{Method: http.MethodPost, Path: types.ServiceInstancesURL, ResponseBody: responseBody, ResponseStatusCode: http.StatusBadRequest}

})
It("should return error with url and description", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "description", http.StatusBadRequest)
Expect(res).To(BeNil())
})
})

Context("And invalid response body", func() {
BeforeEach(func() {
responseBody := []byte(`{ "description": description", }`)
responseBody := []byte(`{ "description": description" }`)
handlerDetails[0] = HandlerDetails{Method: http.MethodPost, Path: types.ServiceInstancesURL, ResponseBody: responseBody, ResponseStatusCode: http.StatusBadRequest}

})
It("should return error without url and description if invalid response body", func() {
res, err := client.Provision(instance, serviceName, planName, params, "test-user", "")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusBadRequest)
Expect(res).To(BeNil())
})
})
Expand Down Expand Up @@ -380,7 +379,7 @@ var _ = Describe("Client test", func() {
})
It("should handle error", func() {
location, err := client.Deprovision(instance.ID, params, "test-user")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusCreated)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusCreated)
Expect(location).Should(BeEmpty())
})
})
Expand Down Expand Up @@ -485,7 +484,7 @@ var _ = Describe("Client test", func() {
})
It("should return error with status code", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusTeapot)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusTeapot)
Expect(location).Should(BeEmpty())
Expect(responseInstance).To(BeNil())
})
Expand All @@ -499,7 +498,7 @@ var _ = Describe("Client test", func() {
})
It("should return error with url and description", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "description", http.StatusBadRequest)
Expect(location).Should(BeEmpty())
Expect(responseInstance).To(BeNil())
})
Expand All @@ -513,7 +512,7 @@ var _ = Describe("Client test", func() {
})
It("should return error without url and description if invalid response body", func() {
responseInstance, location, err := client.UpdateInstance(instance.ID, instance, serviceName, planName, params, "test-user", "")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceInstancesURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "description", http.StatusBadRequest)
Expect(responseInstance).To(BeNil())
Expect(location).Should(BeEmpty())
})
Expand Down Expand Up @@ -601,7 +600,7 @@ var _ = Describe("Client test", func() {
})
It("should handle status code != 200", func() {
_, err := client.ListBindings(params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusCreated)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusCreated)
})
})

Expand All @@ -613,7 +612,7 @@ var _ = Describe("Client test", func() {
})
It("should handle status code > 299", func() {
_, err := client.ListBindings(params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusBadRequest)
})
})
})
Expand Down Expand Up @@ -641,7 +640,7 @@ var _ = Describe("Client test", func() {
})
It("should return 404", func() {
_, err := client.GetBindingByID(binding.ID, params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusNotFound)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusNotFound)
})
})

Expand All @@ -653,7 +652,7 @@ var _ = Describe("Client test", func() {
})
It("should handle status code != 200", func() {
_, err := client.GetBindingByID(binding.ID, params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusCreated)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusCreated)
})
})

Expand All @@ -665,7 +664,7 @@ var _ = Describe("Client test", func() {
})
It("should handle status code > 299", func() {
_, err := client.GetBindingByID(binding.ID, params)
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusBadRequest)

})
})
Expand Down Expand Up @@ -735,7 +734,7 @@ var _ = Describe("Client test", func() {
})
It("should return error with status code", func() {
responseBinding, location, err := client.Bind(binding, params, "test-user")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusOK)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusOK)
Expect(responseBinding).To(BeNil())
Expect(location).Should(BeEmpty())
})
Expand All @@ -750,22 +749,22 @@ var _ = Describe("Client test", func() {
})
It("should return error with url and description", func() {
responseBinding, location, err := client.Bind(binding, params, "test-user")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "description", http.StatusBadRequest)
Expect(responseBinding).To(BeNil())
Expect(location).Should(BeEmpty())
})
})

Context("And invalid response body", func() {
BeforeEach(func() {
responseBody := []byte(`{ "description": description", }`)
responseBody := []byte(`{ "description": description" }`)
handlerDetails = []HandlerDetails{
{Method: http.MethodPost, Path: types.ServiceBindingsURL, ResponseBody: responseBody, ResponseStatusCode: http.StatusBadRequest},
}
})
It("should return error without url and description if invalid response body", func() {
responseBinding, location, err := client.Bind(binding, params, "test-user")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusBadRequest)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusBadRequest)
Expect(responseBinding).To(BeNil())
Expect(location).Should(BeEmpty())
})
Expand Down Expand Up @@ -910,7 +909,7 @@ TSTAhYWEQVZqRKYQMYGHpNlU
})
It("should handle error", func() {
location, err := client.Unbind(binding.ID, params, "test-user")
expectErrorToContainSubstringAndStatusCode(err, types.ServiceBindingsURL, http.StatusCreated)
expectErrorToContainSubstringAndStatusCode(err, "", http.StatusCreated)
Expect(location).Should(BeEmpty())
})
})
Expand Down

0 comments on commit 6fcbae7

Please sign in to comment.