|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { IncomingMessage, ServerResponse } from 'node:http'; |
| 10 | +import { ManualRebuildController } from '../../../utils/manual-rebuild'; |
| 11 | +import { createAngularRebuildMiddleware } from './rebuild-middleware'; |
| 12 | + |
| 13 | +interface MockResponse { |
| 14 | + statusCode: number; |
| 15 | + headers: Record<string, string>; |
| 16 | + body: string | undefined; |
| 17 | + ended: Promise<void>; |
| 18 | +} |
| 19 | + |
| 20 | +function createRequest( |
| 21 | + url: string, |
| 22 | + method: string, |
| 23 | + headers: Record<string, string> = {}, |
| 24 | +): IncomingMessage { |
| 25 | + return { url, method, headers } as unknown as IncomingMessage; |
| 26 | +} |
| 27 | + |
| 28 | +function createResponse(): [ServerResponse, MockResponse] { |
| 29 | + let resolveEnded: () => void; |
| 30 | + const mock: MockResponse = { |
| 31 | + statusCode: 200, |
| 32 | + headers: {}, |
| 33 | + body: undefined, |
| 34 | + ended: new Promise((resolve) => (resolveEnded = resolve)), |
| 35 | + }; |
| 36 | + const res = { |
| 37 | + set statusCode(value: number) { |
| 38 | + mock.statusCode = value; |
| 39 | + }, |
| 40 | + setHeader(name: string, value: string) { |
| 41 | + mock.headers[name.toLowerCase()] = value; |
| 42 | + }, |
| 43 | + end(body?: string) { |
| 44 | + mock.body = body; |
| 45 | + resolveEnded(); |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + return [res as unknown as ServerResponse, mock]; |
| 50 | +} |
| 51 | + |
| 52 | +describe('createAngularRebuildMiddleware', () => { |
| 53 | + let controller: ManualRebuildController; |
| 54 | + let middleware: ReturnType<typeof createAngularRebuildMiddleware>; |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + controller = new ManualRebuildController(); |
| 58 | + spyOn(controller, 'requestRebuild').and.resolveTo({ status: 'success' }); |
| 59 | + middleware = createAngularRebuildMiddleware(controller); |
| 60 | + }); |
| 61 | + |
| 62 | + it('passes through requests for other routes', () => { |
| 63 | + const next = jasmine.createSpy('next'); |
| 64 | + const [res] = createResponse(); |
| 65 | + middleware(createRequest('/main.js', 'POST'), res, next); |
| 66 | + |
| 67 | + expect(next).toHaveBeenCalled(); |
| 68 | + expect(controller.requestRebuild).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('requests a rebuild and responds with the result', async () => { |
| 72 | + const [res, mock] = createResponse(); |
| 73 | + middleware(createRequest('/__ng_rebuild?x=1', 'POST'), res, jasmine.createSpy('next')); |
| 74 | + await mock.ended; |
| 75 | + |
| 76 | + expect(mock.statusCode).toBe(200); |
| 77 | + expect(mock.headers['content-type']).toBe('application/json'); |
| 78 | + expect(JSON.parse(mock.body ?? '')).toEqual({ status: 'success' }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('responds with 405 for non-POST requests', async () => { |
| 82 | + const [res, mock] = createResponse(); |
| 83 | + middleware(createRequest('/__ng_rebuild', 'GET'), res, jasmine.createSpy('next')); |
| 84 | + await mock.ended; |
| 85 | + |
| 86 | + expect(mock.statusCode).toBe(405); |
| 87 | + expect(controller.requestRebuild).not.toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('rejects cross-origin requests', async () => { |
| 91 | + const [res, mock] = createResponse(); |
| 92 | + middleware( |
| 93 | + createRequest('/__ng_rebuild', 'POST', { |
| 94 | + host: 'localhost:4200', |
| 95 | + origin: 'http://example.com', |
| 96 | + }), |
| 97 | + res, |
| 98 | + jasmine.createSpy('next'), |
| 99 | + ); |
| 100 | + await mock.ended; |
| 101 | + |
| 102 | + expect(mock.statusCode).toBe(403); |
| 103 | + expect(controller.requestRebuild).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('accepts same-origin requests', async () => { |
| 107 | + const [res, mock] = createResponse(); |
| 108 | + middleware( |
| 109 | + createRequest('/__ng_rebuild', 'POST', { |
| 110 | + host: 'localhost:4200', |
| 111 | + origin: 'http://localhost:4200', |
| 112 | + }), |
| 113 | + res, |
| 114 | + jasmine.createSpy('next'), |
| 115 | + ); |
| 116 | + await mock.ended; |
| 117 | + |
| 118 | + expect(mock.statusCode).toBe(200); |
| 119 | + }); |
| 120 | + |
| 121 | + it('accepts same-origin HTTP/2 requests using the ":authority" header', async () => { |
| 122 | + const [res, mock] = createResponse(); |
| 123 | + middleware( |
| 124 | + createRequest('/__ng_rebuild', 'POST', { |
| 125 | + ':authority': 'localhost:4200', |
| 126 | + origin: 'https://localhost:4200', |
| 127 | + }), |
| 128 | + res, |
| 129 | + jasmine.createSpy('next'), |
| 130 | + ); |
| 131 | + await mock.ended; |
| 132 | + |
| 133 | + expect(mock.statusCode).toBe(200); |
| 134 | + }); |
| 135 | +}); |
0 commit comments