|
- import {describe, it, expect, beforeEach, afterEach} from 'vitest';
- import {fastify, FastifyInstance} from 'fastify';
- import { fastifyOatmeal } from '../src';
-
- describe('oatmeal-fastify', () => {
- let server: FastifyInstance;
-
- beforeEach(() => {
- server = fastify();
- server.route({
- url: '/',
- method: 'POST',
- handler: async (request, reply) => {
- return reply.sendX(request.headers['accept'] as string, request.body);
- },
- });
- });
-
- afterEach(async () => {
- await server.close();
- });
-
- describe('deserialize', () => {
- it('deserializes application/json', async () => {
- await server.register(fastifyOatmeal);
- const response = await server
- .inject()
- .post('/')
- .body(JSON.stringify({ hello: 'world' }))
- .headers({
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- });
-
- expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
- });
-
- it('deserializes text/json', async () => {
- await server.register(fastifyOatmeal);
- const response = await server
- .inject()
- .post('/')
- .body(JSON.stringify({ hello: 'world' }))
- .headers({
- 'Accept': 'application/json',
- 'Content-Type': 'text/json',
- });
-
- expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
- });
-
- it('deserializes application/xml', async () => {
- await server.register(fastifyOatmeal);
- const response = await server
- .inject()
- .post('/')
- .body(`<root type="object"><hello type="string">world</hello></root>`)
- .headers({
- 'Accept': 'application/json',
- 'Content-Type': 'application/xml',
- });
-
- expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
- });
-
- it('deserializes text/xml', async () => {
- await server.register(fastifyOatmeal);
- const response = await server
- .inject()
- .post('/')
- .body(`<root type="object"><hello type="string">world</hello></root>`)
- .headers({
- 'Accept': 'application/json',
- 'Content-Type': 'text/xml',
- });
-
- expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
- });
- });
-
- describe('serialize', () => {
- it('serializes application/json', async () => {
- await server.register(fastifyOatmeal);
- const response = await server
- .inject()
- .post('/')
- .body(JSON.stringify({ hello: 'world' }))
- .headers({
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- });
-
- expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
- });
-
- it('serializes text/json', async () => {
- await server.register(fastifyOatmeal);
- const response = await server
- .inject()
- .post('/')
- .body(JSON.stringify({ hello: 'world' }))
- .headers({
- 'Accept': 'text/json',
- 'Content-Type': 'application/json',
- });
-
- expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
- });
-
- it('deserializes application/xml', async () => {
- await server.register(fastifyOatmeal);
- const response = await server
- .inject()
- .post('/')
- .body(JSON.stringify({ hello: 'world' }))
- .headers({
- 'Accept': 'application/xml',
- 'Content-Type': 'application/json',
- });
-
- expect(response.body).toBe('<root type="object"><hello type="string">world</hello></root>');
- });
-
- it('deserializes application/xml', async () => {
- await server.register(fastifyOatmeal);
- const response = await server
- .inject()
- .post('/')
- .body(JSON.stringify({ hello: 'world' }))
- .headers({
- 'Accept': 'text/xml',
- 'Content-Type': 'application/json',
- });
-
- expect(response.body).toBe('<root type="object"><hello type="string">world</hello></root>');
- });
- });
- });
|