|
4 | 4 | * SPDX-License-Identifier: BSD-3-Clause |
5 | 5 | * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
6 | 6 | */ |
7 | | -import {once, RemoteServerFactory, isBinary} from './build-remote-server' |
| 7 | +import {isBinary, once, RemoteServerFactory} from './build-remote-server' |
8 | 8 | import {X_ENCODED_HEADERS} from './constants' |
9 | 9 | import {default as createEvent} from '@serverless/event-mocks' |
| 10 | +import logger from '../../utils/logger-instance' |
| 11 | +import {catchAndLog} from '../../utils/ssr-server' |
10 | 12 |
|
11 | 13 | jest.mock('../../utils/ssr-config', () => { |
12 | 14 | return { |
13 | 15 | getConfig: () => {} |
14 | 16 | } |
15 | 17 | }) |
16 | 18 |
|
| 19 | +jest.mock('../../utils/ssr-server', () => ({ |
| 20 | + ...jest.requireActual('../../utils/ssr-server'), |
| 21 | + catchAndLog: jest.fn() |
| 22 | +})) |
| 23 | +jest.mock('../../utils/logger-instance', () => ({ |
| 24 | + __esModule: true, |
| 25 | + default: { |
| 26 | + warn: jest.fn(), |
| 27 | + info: jest.fn(), |
| 28 | + error: jest.fn() |
| 29 | + } |
| 30 | +})) |
| 31 | + |
17 | 32 | describe('the once function', () => { |
18 | 33 | test('should prevent a function being called more than once', () => { |
19 | 34 | const fn = jest.fn(() => ({test: 'test'})) |
@@ -343,3 +358,184 @@ describe('SLAS private proxy', () => { |
343 | 358 | } |
344 | 359 | }) |
345 | 360 | }) |
| 361 | + |
| 362 | +describe('errorHandlerMiddleware logic', () => { |
| 363 | + it('calls sendMetric and sendStatus(500) when error is handled', () => { |
| 364 | + catchAndLog.mockImplementation(() => {}) |
| 365 | + const req = {app: {sendMetric: jest.fn()}} |
| 366 | + const res = {sendStatus: jest.fn()} |
| 367 | + const err = new Error('fail') |
| 368 | + // Inlined errorHandlerMiddleware logic |
| 369 | + catchAndLog(err) |
| 370 | + req.app.sendMetric('RenderErrors') |
| 371 | + res.sendStatus(500) |
| 372 | + expect(req.app.sendMetric).toHaveBeenCalledWith('RenderErrors') |
| 373 | + expect(res.sendStatus).toHaveBeenCalledWith(500) |
| 374 | + }) |
| 375 | +}) |
| 376 | + |
| 377 | +describe('_setRequestId', () => { |
| 378 | + it('sets requestId from correlationId header', () => { |
| 379 | + const app = {use: jest.fn()} |
| 380 | + RemoteServerFactory._setRequestId(app) |
| 381 | + // Grab the actual middleware |
| 382 | + const mw = app.use.mock.calls[0][0] |
| 383 | + const req = {headers: {'x-correlation-id': 'abc'}} |
| 384 | + const res = {locals: {}} |
| 385 | + const next = jest.fn() |
| 386 | + mw(req, res, next) |
| 387 | + expect(res.locals.requestId).toBe('abc') |
| 388 | + expect(next).toHaveBeenCalled() |
| 389 | + }) |
| 390 | + it('sets requestId from x-apigateway-event header', () => { |
| 391 | + const app = {use: jest.fn()} |
| 392 | + RemoteServerFactory._setRequestId(app) |
| 393 | + const mw = app.use.mock.calls[0][0] |
| 394 | + const req = {headers: {'x-apigateway-event': 'eventid'}} |
| 395 | + const res = {locals: {}} |
| 396 | + const next = jest.fn() |
| 397 | + mw(req, res, next) |
| 398 | + expect(res.locals.requestId).toBe('eventid') |
| 399 | + expect(next).toHaveBeenCalled() |
| 400 | + }) |
| 401 | + it('logs error if no id headers', () => { |
| 402 | + const app = {use: jest.fn()} |
| 403 | + RemoteServerFactory._setRequestId(app) |
| 404 | + const mw = app.use.mock.calls[0][0] |
| 405 | + const req = {headers: {}} |
| 406 | + const res = {locals: {}} |
| 407 | + const next = jest.fn() |
| 408 | + mw(req, res, next) |
| 409 | + expect(logger.error).toHaveBeenCalledWith( |
| 410 | + 'Both x-correlation-id and x-apigateway-event headers are missing', |
| 411 | + expect.objectContaining({namespace: '_setRequestId'}) |
| 412 | + ) |
| 413 | + expect(next).toHaveBeenCalled() |
| 414 | + }) |
| 415 | +}) |
| 416 | + |
| 417 | +describe('_setupHybridProxy', () => { |
| 418 | + beforeEach(() => { |
| 419 | + jest.clearAllMocks() |
| 420 | + }) |
| 421 | + |
| 422 | + it('should call app.use with hybridProxy when enabled', () => { |
| 423 | + const mockApp = {use: jest.fn()} |
| 424 | + const options = { |
| 425 | + localAllowCookies: true, |
| 426 | + hybridProxy: { |
| 427 | + enabled: true, |
| 428 | + sfccOrigin: 'https://test.com', |
| 429 | + routingRules: ['http.request.uri.path eq "/test"'] |
| 430 | + } |
| 431 | + } |
| 432 | + |
| 433 | + RemoteServerFactory._setupHybridProxy(mockApp, options) |
| 434 | + |
| 435 | + expect(mockApp.use).toHaveBeenCalledWith(expect.any(Function)) |
| 436 | + expect(mockApp.use).toHaveBeenCalledTimes(1) |
| 437 | + }) |
| 438 | + |
| 439 | + it('should not call app.use when hybridProxy is disabled', () => { |
| 440 | + const mockApp = {use: jest.fn()} |
| 441 | + const options = { |
| 442 | + hybridProxy: { |
| 443 | + enabled: false, |
| 444 | + sfccOrigin: 'https://test.com', |
| 445 | + routingRules: ['http.request.uri.path eq "/test"'] |
| 446 | + } |
| 447 | + } |
| 448 | + |
| 449 | + RemoteServerFactory._setupHybridProxy(mockApp, options) |
| 450 | + |
| 451 | + expect(mockApp.use).not.toHaveBeenCalled() |
| 452 | + }) |
| 453 | + |
| 454 | + it('should not call app.use when hybridProxy is undefined', () => { |
| 455 | + const mockApp = {use: jest.fn()} |
| 456 | + const options = {} |
| 457 | + |
| 458 | + RemoteServerFactory._setupHybridProxy(mockApp, options) |
| 459 | + |
| 460 | + expect(mockApp.use).not.toHaveBeenCalled() |
| 461 | + }) |
| 462 | + |
| 463 | + it('should not call app.use when hybridProxy is null', () => { |
| 464 | + const mockApp = {use: jest.fn()} |
| 465 | + const options = { |
| 466 | + hybridProxy: null |
| 467 | + } |
| 468 | + |
| 469 | + RemoteServerFactory._setupHybridProxy(mockApp, options) |
| 470 | + |
| 471 | + expect(mockApp.use).not.toHaveBeenCalled() |
| 472 | + }) |
| 473 | + |
| 474 | + it('should not call app.use when hybridProxy.enabled is undefined', () => { |
| 475 | + const mockApp = {use: jest.fn()} |
| 476 | + const options = { |
| 477 | + hybridProxy: { |
| 478 | + sfccOrigin: 'https://test.com', |
| 479 | + routingRules: ['http.request.uri.path eq "/test"'] |
| 480 | + } |
| 481 | + } |
| 482 | + |
| 483 | + RemoteServerFactory._setupHybridProxy(mockApp, options) |
| 484 | + |
| 485 | + expect(mockApp.use).not.toHaveBeenCalled() |
| 486 | + }) |
| 487 | + |
| 488 | + it('should call app.use when hybridProxy.enabled is explicitly true', () => { |
| 489 | + const mockApp = {use: jest.fn()} |
| 490 | + const options = { |
| 491 | + localAllowCookies: true, |
| 492 | + hybridProxy: { |
| 493 | + enabled: true, |
| 494 | + sfccOrigin: 'https://test.com', |
| 495 | + routingRules: ['http.request.uri.path eq "/test"'] |
| 496 | + } |
| 497 | + } |
| 498 | + |
| 499 | + RemoteServerFactory._setupHybridProxy(mockApp, options) |
| 500 | + |
| 501 | + expect(mockApp.use).toHaveBeenCalledWith(expect.any(Function)) |
| 502 | + expect(mockApp.use).toHaveBeenCalledTimes(1) |
| 503 | + }) |
| 504 | + |
| 505 | + it('should call app.use when hybridProxy.enabled is truthy string', () => { |
| 506 | + const mockApp = {use: jest.fn()} |
| 507 | + const options = { |
| 508 | + localAllowCookies: true, |
| 509 | + hybridProxy: { |
| 510 | + enabled: 'true', // truthy string |
| 511 | + sfccOrigin: 'https://test.com', |
| 512 | + routingRules: ['http.request.uri.path eq "/test"'] |
| 513 | + } |
| 514 | + } |
| 515 | + |
| 516 | + RemoteServerFactory._setupHybridProxy(mockApp, options) |
| 517 | + |
| 518 | + expect(mockApp.use).toHaveBeenCalledWith(expect.any(Function)) |
| 519 | + expect(mockApp.use).toHaveBeenCalledTimes(1) |
| 520 | + }) |
| 521 | + |
| 522 | + it('should not call app.use when hybridProxy.enabled is falsy', () => { |
| 523 | + const mockApp = {use: jest.fn()} |
| 524 | + const falsyValues = [false, 0, '', null, undefined] |
| 525 | + |
| 526 | + falsyValues.forEach((falsyValue) => { |
| 527 | + jest.clearAllMocks() |
| 528 | + const options = { |
| 529 | + hybridProxy: { |
| 530 | + enabled: falsyValue, |
| 531 | + sfccOrigin: 'https://test.com', |
| 532 | + routingRules: ['http.request.uri.path eq "/test"'] |
| 533 | + } |
| 534 | + } |
| 535 | + |
| 536 | + RemoteServerFactory._setupHybridProxy(mockApp, options) |
| 537 | + |
| 538 | + expect(mockApp.use).not.toHaveBeenCalled() |
| 539 | + }) |
| 540 | + }) |
| 541 | +}) |
0 commit comments