Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit ee675fd

Browse files
committed
Make client return error on JSON decoding error
If the response is not valid JSON, the request simply returns an empty body. If the user is expecting the result to be JSON and its not, we should signal that an error has occured. This patch also includes fixes to tests that fail due to the error that bubbles up.
1 parent 53c3b4c commit ee675fd

File tree

10 files changed

+23
-7
lines changed

10 files changed

+23
-7
lines changed

openstack/compute/v2/extensions/secgroups/fixtures.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func mockAddServerToGroupResponse(t *testing.T, serverID string) {
242242

243243
w.Header().Add("Content-Type", "application/json")
244244
w.WriteHeader(http.StatusAccepted)
245+
fmt.Fprintf(w, `{}`)
245246
})
246247
}
247248

@@ -261,5 +262,6 @@ func mockRemoveServerFromGroupResponse(t *testing.T, serverID string) {
261262

262263
w.Header().Add("Content-Type", "application/json")
263264
w.WriteHeader(http.StatusAccepted)
265+
fmt.Fprintf(w, `{}`)
264266
})
265267
}

openstack/compute/v2/servers/requests.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,7 @@ func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumRe
754754
// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
755755
func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
756756
var res DeleteMetadatumResult
757-
_, res.Err = client.Delete(metadatumURL(client, id, key), &gophercloud.RequestOpts{
758-
JSONResponse: &res.Body,
759-
})
757+
_, res.Err = client.Delete(metadatumURL(client, id, key), nil)
760758
return res
761759
}
762760

openstack/networking/v2/extensions/fwaas/firewalls/requests_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ func TestUpdate(t *testing.T) {
212212
"name": "fw",
213213
"admin_state_up": false,
214214
"tenant_id": "b4eedccc6fb74fa8a7ad6b08382b852b",
215-
"firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c"
215+
"firewall_policy_id": "19ab8c87-4a32-4e6a-a74e-b77fffb89a0c",
216216
"id": "ea5b5315-64f6-4ea3-8e58-981cc37c6576",
217-
"description": "OpenStack firewall",
217+
"description": "OpenStack firewall"
218218
}
219219
}
220220
`)

openstack/networking/v2/extensions/lbaas/pools/requests_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ func TestAssociateHealthMonitor(t *testing.T) {
296296

297297
w.Header().Add("Content-Type", "application/json")
298298
w.WriteHeader(http.StatusCreated)
299+
fmt.Fprintf(w, `{}`)
299300
})
300301

301302
_, err := AssociateMonitor(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", "b624decf-d5d3-4c66-9a3d-f047e7786181").Extract()

openstack/networking/v2/networks/requests_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func TestCreateWithOptionalFields(t *testing.T) {
204204
`)
205205

206206
w.WriteHeader(http.StatusCreated)
207+
fmt.Fprintf(w, `{}`)
207208
})
208209

209210
iTrue := true

provider_client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ func (client *ProviderClient) Request(method, url string, options RequestOpts) (
227227
// Parse the response body as JSON, if requested to do so.
228228
if options.JSONResponse != nil {
229229
defer resp.Body.Close()
230-
json.NewDecoder(resp.Body).Decode(options.JSONResponse)
230+
if err := json.NewDecoder(resp.Body).Decode(options.JSONResponse); err != nil {
231+
return nil, err
232+
}
231233
}
232234

233235
return resp, nil

rackspace/lb/v1/sessions/fixtures.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func mockEnableResponse(t *testing.T, lbID int) {
4646
`)
4747

4848
w.WriteHeader(http.StatusAccepted)
49+
fmt.Fprintf(w, `{}`)
4950
})
5051
}
5152

rackspace/lb/v1/ssl/fixtures.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func mockUpdateResponse(t *testing.T, lbID int) {
6363
`)
6464

6565
w.WriteHeader(http.StatusOK)
66+
fmt.Fprintf(w, `{}`)
6667
})
6768
}
6869

rackspace/lb/v1/throttle/fixtures.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func mockCreateResponse(t *testing.T, lbID int) {
4949
`)
5050

5151
w.WriteHeader(http.StatusAccepted)
52+
fmt.Fprintf(w, `{}`)
5253
})
5354
}
5455

rackspace/networking/v2/networks/delegate_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,17 @@ func TestCreateWithOptionalFields(t *testing.T) {
203203
}
204204
}
205205
`)
206-
207206
w.WriteHeader(http.StatusCreated)
207+
fmt.Fprintf(w, `
208+
{
209+
"network": {
210+
"name": "sample_network",
211+
"admin_state_up": true,
212+
"shared": true,
213+
"tenant_id": "12345"
214+
}
215+
}
216+
`)
208217
})
209218

210219
iTrue := true

0 commit comments

Comments
 (0)