Skip to content

Commit

Permalink
fix(transport-commons): Fix HTTP status precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Jul 9, 2024
1 parent 49b9f70 commit 6d55989
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/transport-commons/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ export function getStatusCode(context: HookContext, body: any, location: string
return http.status
}

if (context.method === 'create') {
return statusCodes.created
}

if (location !== undefined) {
return statusCodes.seeOther
}
Expand All @@ -74,6 +70,10 @@ export function getStatusCode(context: HookContext, body: any, location: string
return statusCodes.noContent
}

if (context.method === 'create') {
return statusCodes.created
}

return statusCodes.success
}

Expand Down
13 changes: 13 additions & 0 deletions packages/transport-commons/test/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,29 @@ describe('@feathersjs/transport-commons HTTP helpers', () => {
http: { status: 202 }
}
const createContext = {
method: 'create',
result: {}
}
const createEmptyContext = {
method: 'create'
}
const redirectContext = {
http: { location: '/' }
}
const redirectCreateContext = {
method: 'create',
http: { location: '/' }
}

assert.strictEqual(http.getResponse(statusContext as HookContext).status, 202)
assert.strictEqual(http.getResponse(createContext as HookContext).status, http.statusCodes.created)
assert.strictEqual(http.getResponse(redirectContext as HookContext).status, http.statusCodes.seeOther)
assert.strictEqual(
http.getResponse(redirectCreateContext as HookContext).status,
http.statusCodes.seeOther
)
assert.strictEqual(http.getResponse({} as HookContext).status, http.statusCodes.noContent)
assert.strictEqual(http.getResponse(createEmptyContext as HookContext).status, http.statusCodes.noContent)
assert.strictEqual(http.getResponse({ result: true } as HookContext).status, http.statusCodes.success)
})

Expand Down

0 comments on commit 6d55989

Please sign in to comment.