Skip to content

Commit

Permalink
🎉 feat: add testcase for redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Apr 26, 2024
1 parent 144d573 commit 3623be5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
@@ -1,4 +1,4 @@
# 1.0.15 - 25 Apr 2024
# 1.0.15 - 27 Apr 2024
Feature:
- add `redirect` function to `Context`

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "elysia",
"description": "Ergonomic Framework for Human",
"version": "1.0.14",
"version": "1.0.15",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -5343,6 +5343,7 @@ export {
mergeHook,
mergeObjectArray,
getResponseSchemaValidator,
redirect,
StatusMap,
InvertedStatusMap
} from './utils'
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Expand Up @@ -977,7 +977,7 @@ export const cloneInference = (inference: {
*/
export const redirect = (
url: string,
status: number = 302
status: number = 301
) =>
new Response(null, {
status,
Expand Down
49 changes: 49 additions & 0 deletions test/response/redirect.test.ts
@@ -0,0 +1,49 @@
import { Elysia } from '../../src'

import { describe, expect, it } from 'bun:test'
import { req } from '../utils'

describe('Response Headers', () => {
it('handle redirect', async () => {
const app = new Elysia().get('/', ({ redirect }) => redirect('/skadi'))

const { headers, status } = await app.handle(req('/'))

expect(status).toBe(301)
// @ts-expect-error
expect(headers.toJSON()).toEqual({
location: '/skadi'
})
})

it('handle redirect status', async () => {
const app = new Elysia().get('/', ({ redirect }) =>
redirect('/skadi', 302)
)

const { headers, status } = await app.handle(req('/'))

expect(status).toBe(302)
// @ts-expect-error
expect(headers.toJSON()).toEqual({
location: '/skadi'
})
})

it('add set.headers to redirect', async () => {
const app = new Elysia().get('/', ({ redirect, set }) => {
set.headers.alias = 'Abyssal Hunter'

return redirect('/skadi')
})

const { headers, status } = await app.handle(req('/'))

expect(status).toBe(301)
// @ts-expect-error
expect(headers.toJSON()).toEqual({
location: '/skadi',
alias: 'Abyssal Hunter'
})
})
})

0 comments on commit 3623be5

Please sign in to comment.