Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: aliculPix4D <[email protected]>
  • Loading branch information
aliculPix4D committed Oct 4, 2024
1 parent 59acf05 commit 424e986
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
32 changes: 15 additions & 17 deletions go-concourse/concourse/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,34 @@ func (team *team) CreateOrUpdatePipelineConfig(pipelineRef atc.PipelineRef, conf

switch response.StatusCode {
case http.StatusOK, http.StatusCreated:
if response.Header.Get("Content-Type") == "application/json" {
configResponse := setConfigResponse{}
err = json.Unmarshal(body, &configResponse)
if err != nil {
return false, false, []ConfigWarning{}, fmt.Errorf("client: unable to JSON parse the response body: %w", err)
}
created := response.StatusCode == http.StatusCreated
return created, !created, configResponse.Warnings, nil
} else {
if response.Header.Get("Content-Type") != "application/json" {
return false, false, []ConfigWarning{}, internal.UnexpectedResponseError{
StatusCode: response.StatusCode,
Status: response.Status,
Body: string(body),
}
}
configResponse := setConfigResponse{}
err = json.Unmarshal(body, &configResponse)
if err != nil {
return false, false, []ConfigWarning{}, fmt.Errorf("parsing JSON: %w", err)
}
created := response.StatusCode == http.StatusCreated
return created, !created, configResponse.Warnings, nil
case http.StatusBadRequest:
if response.Header.Get("Content-Type") == "application/json" {
var validationErr atc.SaveConfigResponse
err = json.Unmarshal(body, &validationErr)
if err != nil {
return false, false, []ConfigWarning{}, fmt.Errorf("client: unable to JSON parse the response body: %w", err)
}
return false, false, []ConfigWarning{}, InvalidConfigError{Errors: validationErr.Errors}
} else {
if response.Header.Get("Content-Type") != "application/json" {
return false, false, []ConfigWarning{}, internal.UnexpectedResponseError{
StatusCode: response.StatusCode,
Status: response.Status,
Body: string(body),
}
}
var validationErr atc.SaveConfigResponse
err = json.Unmarshal(body, &validationErr)
if err != nil {
return false, false, []ConfigWarning{}, fmt.Errorf("parsing JSON: %w", err)
}
return false, false, []ConfigWarning{}, InvalidConfigError{Errors: validationErr.Errors}
case http.StatusForbidden:
return false, false, []ConfigWarning{}, internal.ForbiddenError{
Reason: string(body),
Expand Down
35 changes: 6 additions & 29 deletions go-concourse/concourse/configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,9 @@ var _ = Describe("ATC Handler Configs", func() {
}))
})

Context("when response contains bad JSON", func() {
BeforeEach(func() {
returnBody = []byte(`bad-json`)
})

It("returns an error", func() {
_, _, _, err := team.CreateOrUpdatePipelineConfig(pipelineRef, expectedVersion, expectedConfig, checkCredentials)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("client: unable to JSON parse the response body"))
})
})

Context("when response does not contain application/json header", func() {
BeforeEach(func() {
contentType = "text/plain"
contentType = ""
returnBody = []byte(`server error`)
})

Expand Down Expand Up @@ -331,15 +319,16 @@ var _ = Describe("ATC Handler Configs", func() {
}))
})

Context("when response contains bad JSON", func() {
Context("when response does not contain application/json header", func() {
BeforeEach(func() {
returnBody = []byte(`bad-json`)
contentType = ""
returnBody = []byte(`server error`)
})

It("returns an error", func() {
_, _, _, err := team.CreateOrUpdatePipelineConfig(pipelineRef, expectedVersion, expectedConfig, checkCredentials)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("client: unable to JSON parse the response body"))
Expect(err.Error()).To(ContainSubstring("server error"))
})
})

Expand Down Expand Up @@ -393,21 +382,9 @@ var _ = Describe("ATC Handler Configs", func() {
Expect(err.Error()).To(ContainSubstring("fake-error1\nfake-error2"))
})

Context("when response contains bad JSON", func() {
BeforeEach(func() {
returnBody = []byte(`bad-json`)
})

It("returns an error", func() {
_, _, _, err := team.CreateOrUpdatePipelineConfig(pipelineRef, expectedVersion, expectedConfig, checkCredentials)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("client: unable to JSON parse the response body"))
})
})

Context("when response does not contain application/json header", func() {
BeforeEach(func() {
contentType = "text/plain"
contentType = ""
returnBody = []byte(`server error`)
})

Expand Down

0 comments on commit 424e986

Please sign in to comment.