headers.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import * as assert from 'node:assert/strict';
  2. import { describe, it, before } from 'node:test';
  3. import nodejs from '../dist/index.js';
  4. import { loadFixture, createRequestAndResponse } from './test-utils.js';
  5. describe('Node Adapter Headers', () => {
  6. /** @type {import('./test-utils').Fixture} */
  7. let fixture;
  8. before(async () => {
  9. fixture = await loadFixture({
  10. root: './fixtures/headers/',
  11. output: 'server',
  12. adapter: nodejs({ mode: 'middleware' }),
  13. });
  14. await fixture.build();
  15. });
  16. it('Endpoint Simple Headers', async () => {
  17. await runTest('/endpoints/simple', {
  18. 'content-type': 'text/plain;charset=utf-8',
  19. 'x-hello': 'world',
  20. });
  21. });
  22. it('Endpoint Astro Single Cookie Header', async () => {
  23. await runTest('/endpoints/astro-cookies-single', {
  24. 'content-type': 'text/plain;charset=utf-8',
  25. 'set-cookie': 'from1=astro1',
  26. });
  27. });
  28. it('Endpoint Astro Multi Cookie Header', async () => {
  29. await runTest('/endpoints/astro-cookies-multi', {
  30. 'content-type': 'text/plain;charset=utf-8',
  31. 'set-cookie': ['from1=astro1', 'from2=astro2'],
  32. });
  33. });
  34. it('Endpoint Response Single Cookie Header', async () => {
  35. await runTest('/endpoints/response-cookies-single', {
  36. 'content-type': 'text/plain;charset=utf-8',
  37. 'set-cookie': 'hello1=world1',
  38. });
  39. });
  40. it('Endpoint Response Multi Cookie Header', async () => {
  41. await runTest('/endpoints/response-cookies-multi', {
  42. 'content-type': 'text/plain;charset=utf-8',
  43. 'set-cookie': ['hello1=world1', 'hello2=world2'],
  44. });
  45. });
  46. it('Endpoint Complex Headers Kitchen Sink', async () => {
  47. await runTest('/endpoints/kitchen-sink', {
  48. 'content-type': 'text/plain;charset=utf-8',
  49. 'x-single': 'single',
  50. 'x-triple': 'one, two, three',
  51. 'set-cookie': ['hello1=world1', 'hello2=world2'],
  52. });
  53. });
  54. it('Endpoint Astro and Response Single Cookie Header', async () => {
  55. await runTest('/endpoints/astro-response-cookie-single', {
  56. 'content-type': 'text/plain;charset=utf-8',
  57. 'set-cookie': ['from1=response1', 'from1=astro1'],
  58. });
  59. });
  60. it('Endpoint Astro and Response Multi Cookie Header', async () => {
  61. await runTest('/endpoints/astro-response-cookie-multi', {
  62. 'content-type': 'text/plain;charset=utf-8',
  63. 'set-cookie': ['from1=response1', 'from2=response2', 'from3=astro1', 'from4=astro2'],
  64. });
  65. });
  66. it('Endpoint Response Empty Headers Object', async () => {
  67. await runTest('/endpoints/response-empty-headers-object', {
  68. 'content-type': 'text/plain;charset=UTF-8',
  69. });
  70. });
  71. it('Endpoint Response undefined Headers Object', async () => {
  72. await runTest('/endpoints/response-undefined-headers-object', {
  73. 'content-type': 'text/plain;charset=UTF-8',
  74. });
  75. });
  76. it('Component Astro Single Cookie Header', async () => {
  77. await runTest('/astro/component-astro-cookies-single', {
  78. 'content-type': 'text/html',
  79. 'set-cookie': 'from1=astro1',
  80. });
  81. });
  82. it('Component Astro Multi Cookie Header', async () => {
  83. await runTest('/astro/component-astro-cookies-multi', {
  84. 'content-type': 'text/html',
  85. 'set-cookie': ['from1=astro1', 'from2=astro2'],
  86. });
  87. });
  88. it('Component Response Single Cookie Header', async () => {
  89. await runTest('/astro/component-response-cookies-single', {
  90. 'content-type': 'text/html',
  91. 'set-cookie': 'from1=value1',
  92. });
  93. });
  94. it('Component Response Multi Cookie Header', async () => {
  95. await runTest('/astro/component-response-cookies-multi', {
  96. 'content-type': 'text/html',
  97. 'set-cookie': ['from1=value1', 'from2=value2'],
  98. });
  99. });
  100. it('Component Astro and Response Single Cookie Header', async () => {
  101. await runTest('/astro/component-astro-response-cookie-single', {
  102. 'content-type': 'text/html',
  103. 'set-cookie': ['from1=response1', 'from1=astro1'],
  104. });
  105. });
  106. it('Component Astro and Response Multi Cookie Header', async () => {
  107. await runTest('/astro/component-astro-response-cookie-multi', {
  108. 'content-type': 'text/html',
  109. 'set-cookie': ['from1=response1', 'from2=response2', 'from3=astro1', 'from4=astro2'],
  110. });
  111. });
  112. });
  113. async function runTest(url, expectedHeaders) {
  114. const { handler } = await import('./fixtures/headers/dist/server/entry.mjs');
  115. let { req, res, done } = createRequestAndResponse({
  116. method: 'GET',
  117. url,
  118. });
  119. handler(req, res);
  120. req.send();
  121. await done;
  122. const headers = res.getHeaders();
  123. assert.deepEqual(headers, expectedHeaders);
  124. }