Serialize and deserialize data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.9 KiB

  1. import {describe, it, expect, beforeEach, afterEach} from 'vitest';
  2. import {fastify, FastifyInstance} from 'fastify';
  3. import { fastifyOatmeal } from '../src';
  4. describe('oatmeal-fastify', () => {
  5. let server: FastifyInstance;
  6. beforeEach(() => {
  7. server = fastify();
  8. server.route({
  9. url: '/',
  10. method: 'POST',
  11. handler: async (request, reply) => {
  12. return reply.sendX(request.headers['accept'] as string, request.body);
  13. },
  14. });
  15. });
  16. afterEach(async () => {
  17. await server.close();
  18. });
  19. describe('deserialize', () => {
  20. it('deserializes application/json', async () => {
  21. await server.register(fastifyOatmeal);
  22. const response = await server
  23. .inject()
  24. .post('/')
  25. .body(JSON.stringify({ hello: 'world' }))
  26. .headers({
  27. 'Accept': 'application/json',
  28. 'Content-Type': 'application/json',
  29. });
  30. expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
  31. });
  32. it('deserializes text/json', async () => {
  33. await server.register(fastifyOatmeal);
  34. const response = await server
  35. .inject()
  36. .post('/')
  37. .body(JSON.stringify({ hello: 'world' }))
  38. .headers({
  39. 'Accept': 'application/json',
  40. 'Content-Type': 'text/json',
  41. });
  42. expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
  43. });
  44. it('deserializes application/xml', async () => {
  45. await server.register(fastifyOatmeal);
  46. const response = await server
  47. .inject()
  48. .post('/')
  49. .body(`<root type="object"><hello type="string">world</hello></root>`)
  50. .headers({
  51. 'Accept': 'application/json',
  52. 'Content-Type': 'application/xml',
  53. });
  54. expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
  55. });
  56. it('deserializes text/xml', async () => {
  57. await server.register(fastifyOatmeal);
  58. const response = await server
  59. .inject()
  60. .post('/')
  61. .body(`<root type="object"><hello type="string">world</hello></root>`)
  62. .headers({
  63. 'Accept': 'application/json',
  64. 'Content-Type': 'text/xml',
  65. });
  66. expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
  67. });
  68. });
  69. describe('serialize', () => {
  70. it('serializes application/json', async () => {
  71. await server.register(fastifyOatmeal);
  72. const response = await server
  73. .inject()
  74. .post('/')
  75. .body(JSON.stringify({ hello: 'world' }))
  76. .headers({
  77. 'Accept': 'application/json',
  78. 'Content-Type': 'application/json',
  79. });
  80. expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
  81. });
  82. it('serializes text/json', async () => {
  83. await server.register(fastifyOatmeal);
  84. const response = await server
  85. .inject()
  86. .post('/')
  87. .body(JSON.stringify({ hello: 'world' }))
  88. .headers({
  89. 'Accept': 'text/json',
  90. 'Content-Type': 'application/json',
  91. });
  92. expect(response.body).toBe(JSON.stringify({ hello: 'world' }));
  93. });
  94. it('deserializes application/xml', async () => {
  95. await server.register(fastifyOatmeal);
  96. const response = await server
  97. .inject()
  98. .post('/')
  99. .body(JSON.stringify({ hello: 'world' }))
  100. .headers({
  101. 'Accept': 'application/xml',
  102. 'Content-Type': 'application/json',
  103. });
  104. expect(response.body).toBe('<root type="object"><hello type="string">world</hello></root>');
  105. });
  106. it('deserializes application/xml', async () => {
  107. await server.register(fastifyOatmeal);
  108. const response = await server
  109. .inject()
  110. .post('/')
  111. .body(JSON.stringify({ hello: 'world' }))
  112. .headers({
  113. 'Accept': 'text/xml',
  114. 'Content-Type': 'application/json',
  115. });
  116. expect(response.body).toBe('<root type="object"><hello type="string">world</hello></root>');
  117. });
  118. });
  119. });