Skip to content

Commit

Permalink
Merge pull request #52 from nobitagit/fix-204-response
Browse files Browse the repository at this point in the history
Fix 204 response
  • Loading branch information
wadetandy authored Nov 18, 2019
2 parents b684fd4 + 87d1c57 commit 684a687
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ export class SpraypaintBase {
throw err
}

if (response.status === 202) {
if (response.status === 202 || response.status === 204) {
return await this._handleAcceptedResponse(response, this.onDeferredUpdate)
}

Expand Down
5 changes: 3 additions & 2 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Request {
response: Response,
requestOptions: RequestInit
) {
let wasDelete =
const wasDelete =
requestOptions.method === "DELETE" &&
[204, 200].indexOf(response.status) > -1
if (wasDelete) return
Expand All @@ -127,7 +127,8 @@ export class Request {
try {
json = await response.clone().json()
} catch (e) {
if (response.status === 202) return
const isEmptyResponse = [202, 204].indexOf(response.status) > -1
if (isEmptyResponse) return
throw new ResponseError(response, "invalid json", e)
}

Expand Down
37 changes: 31 additions & 6 deletions test/integration/persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,34 @@ describe("Model persistence", () => {
expect(job.klass).to.eq(BackgroundJob)
})
})

describe("when the server returns 204 (no content)", () => {
const lastName = "Richards"
beforeEach(() => {
fetchMock.restore()

fetchMock.mock("http://example.com/api/v1/people", {
status: 204,
body: null
})
})

afterEach(resetMocks)

it("doesn't blow up when posting", async () => {
instance.isPersisted = false
instance.lastName = lastName
await instance.save()
expect(instance.lastName).to.eq(lastName)
})

it("doesn't blow up when patching", async () => {
instance.isPersisted = true
instance.lastName = lastName
await instance.save()
expect(instance.lastName).to.eq(lastName)
})
})
})

describe("#destroy", () => {
Expand All @@ -347,15 +375,12 @@ describe("Model persistence", () => {
beforeEach(() => {
fetchMock.restore()

fetchMock.mock({
matcher: "http://example.com/api/v1/people/1",
response: new Response({ status: 204 } as any)
fetchMock.mock("http://example.com/api/v1/people/1", {
status: 204
})
})

afterEach(() => {
resetMocks()
})
afterEach(resetMocks)

it("does not blow up", async () => {
expect(instance.isPersisted).to.eq(true)
Expand Down
1 change: 0 additions & 1 deletion test/unit/associations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe("Associations", () => {
const jsonapiTypeAssoc = assoc({ type: "type_strings" })

expect(jsonapiTypeAssoc.jsonapiType).to.eq("type_strings")
it
})

it("accepts a bare spraypaint type string", () => {
Expand Down

0 comments on commit 684a687

Please sign in to comment.