-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): create responses from views (#3)
* wip: add view type * feat(core): create responses from views * chore(tests): AAA
- Loading branch information
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Context, Next } from 'koa'; | ||
|
||
export async function viewMiddleware(ctx: Context, next: Next) { | ||
const view = await next(); | ||
|
||
if (view) { | ||
if (view.status) { | ||
ctx.response.status = view.status; | ||
} | ||
|
||
if (view.headers) { | ||
ctx.response.headers = view.headers; | ||
} | ||
|
||
if (view.body) { | ||
ctx.response.body = view.body; | ||
} | ||
|
||
if (view.socket) { | ||
ctx.response.socket = view.socket; | ||
} | ||
|
||
if (view.redirect) { | ||
ctx.response.redirect(view.redirect); | ||
} | ||
|
||
if (view.attachment) { | ||
ctx.response.attachment(view.attachment); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
import { Context, Next } from 'koa'; | ||
import { viewMiddleware } from '../../src/core/view-middleware'; | ||
|
||
describe('View Middleware', () => { | ||
let ctx: Context; | ||
|
||
beforeEach(() => { | ||
ctx = { | ||
response: {}, | ||
} as Context; | ||
}); | ||
|
||
test('it should set response status', async () => { | ||
const next: Next = vi.fn().mockResolvedValue({ status: 200 }); | ||
|
||
await viewMiddleware(ctx, next); | ||
|
||
expect(ctx.response.status).toBe(200); | ||
}); | ||
|
||
test('it should set response headers', async () => { | ||
const next: Next = vi.fn().mockResolvedValue({ headers: { 'Content-Type': 'application/json' } }); | ||
|
||
await viewMiddleware(ctx, next); | ||
|
||
expect(ctx.response.headers).toEqual({ 'Content-Type': 'application/json' }); | ||
}); | ||
|
||
test('it should set response body', async () => { | ||
const next: Next = vi.fn().mockResolvedValue({ body: 'Hello, World!' }); | ||
|
||
await viewMiddleware(ctx, next); | ||
|
||
expect(ctx.response.body).toBe('Hello, World!'); | ||
}); | ||
|
||
test('it should set response socket', async () => { | ||
const next: Next = vi.fn().mockResolvedValue({ socket: 'socket' }); | ||
|
||
await viewMiddleware(ctx, next); | ||
|
||
expect(ctx.response.socket).toBe('socket'); | ||
}); | ||
|
||
test('it should redirect if view has redirect', async () => { | ||
const next: Next = vi.fn().mockResolvedValue({ redirect: '/home' }); | ||
ctx.response.redirect = vi.fn(); | ||
|
||
await viewMiddleware(ctx, next); | ||
|
||
expect(ctx.response.redirect).toHaveBeenCalledWith('/home'); | ||
}); | ||
|
||
test('it should attach if view has attachment', async () => { | ||
const next: Next = vi.fn().mockResolvedValue({ attachment: 'file.txt' }); | ||
ctx.response.attachment = vi.fn(); | ||
|
||
await viewMiddleware(ctx, next); | ||
|
||
expect(ctx.response.attachment).toHaveBeenCalledWith('file.txt'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters