|
| 1 | +import * as net from 'net'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { DestroyableServer, makeDestroyable } from 'destroyable-server'; |
| 4 | + |
| 5 | +import * as Cookie from 'cookie'; |
| 6 | + |
| 7 | +import { createServer } from '../src/server.js'; |
| 8 | + |
| 9 | +describe("Cookie endpoints", () => { |
| 10 | + |
| 11 | + let server: DestroyableServer; |
| 12 | + let serverPort: number; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + server = makeDestroyable(await createServer()); |
| 16 | + await new Promise<void>((resolve) => server.listen(resolve)); |
| 17 | + serverPort = (server.address() as net.AddressInfo).port; |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(async () => { |
| 21 | + await server.destroy(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("returns cookies at /cookies", async () => { |
| 25 | + const cookieValue = 'test=value; another=cookie'; |
| 26 | + const address = `http://localhost:${serverPort}/cookies`; |
| 27 | + const response = await fetch(address, { |
| 28 | + headers: { |
| 29 | + 'Cookie': cookieValue |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + expect(response.status).to.equal(200); |
| 34 | + |
| 35 | + const body = await response.json(); |
| 36 | + expect(body.cookies).to.deep.equal(Cookie.parse(cookieValue)); |
| 37 | + }); |
| 38 | + |
| 39 | + it("sets cookies using path parameters at /cookies/set/name/value", async () => { |
| 40 | + const address = `http://localhost:${serverPort}/cookies/set/test/value`; |
| 41 | + const response = await fetch(address, { |
| 42 | + redirect: 'manual' // Prevent auto-following redirects |
| 43 | + }); |
| 44 | + |
| 45 | + expect(response.status).to.equal(302); |
| 46 | + expect(response.headers.get('Location')).to.equal('/cookies'); |
| 47 | + expect(response.headers.get('Set-Cookie')).to.equal('test=value; Path=/'); |
| 48 | + }); |
| 49 | + |
| 50 | + it("sets cookies using query parameters at /cookies/set", async () => { |
| 51 | + const address = `http://localhost:${serverPort}/cookies/set?test=value&test=value2&another=cookie`; |
| 52 | + const response = await fetch(address, { |
| 53 | + redirect: 'manual' // Prevent auto-following redirects |
| 54 | + }); |
| 55 | + |
| 56 | + expect(response.status).to.equal(302); |
| 57 | + expect(response.headers.get('Location')).to.equal('/cookies'); |
| 58 | + |
| 59 | + const setCookies = response.headers.getSetCookie(); |
| 60 | + const parsedCookies = setCookies.map((c) => Cookie.parse(c)); |
| 61 | + expect(parsedCookies).to.deep.equal([ |
| 62 | + { test: 'value', Path: '/' }, |
| 63 | + { another: 'cookie', Path: '/' } |
| 64 | + ]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('deletes cookies at /cookies/delete?cookie', async () => { |
| 68 | + const address = `http://localhost:${serverPort}/cookies/delete?hello=world&test`; |
| 69 | + const response = await fetch(address, { |
| 70 | + redirect: 'manual' // Prevent auto-following redirects |
| 71 | + }); |
| 72 | + |
| 73 | + expect(response.status).to.equal(302); |
| 74 | + expect(response.headers.get('Location')).to.equal('/cookies'); |
| 75 | + |
| 76 | + expect(response.headers.getSetCookie()).to.deep.equal([ |
| 77 | + [ |
| 78 | + 'hello=', |
| 79 | + 'Expires=Thu, 01-Jan-1970 00:00:00 GMT', |
| 80 | + 'Max-Age=0', |
| 81 | + 'Path=/' |
| 82 | + ].join('; '), |
| 83 | + [ |
| 84 | + 'test=', |
| 85 | + 'Expires=Thu, 01-Jan-1970 00:00:00 GMT', |
| 86 | + 'Max-Age=0', |
| 87 | + 'Path=/' |
| 88 | + ].join('; ') |
| 89 | + ]); |
| 90 | + }) |
| 91 | +}); |
0 commit comments