|
5 | 5 | getWalletAddressUrlFromIncomingPayment,
|
6 | 6 | getWalletAddressUrlFromQuote,
|
7 | 7 | getWalletAddressUrlFromOutgoingPayment,
|
8 |
| - getWalletAddressUrlFromPath |
| 8 | + getWalletAddressUrlFromPath, |
| 9 | + redirectIfBrowserAcceptsHtml |
9 | 10 | } from './middleware'
|
10 | 11 | import { Config } from '../../config/app'
|
11 | 12 | import { IocContract } from '@adonisjs/fold'
|
@@ -382,4 +383,85 @@ describe('Wallet Address Middleware', (): void => {
|
382 | 383 | expect(ctx.walletAddress).toEqual(walletAddress)
|
383 | 384 | })
|
384 | 385 | })
|
| 386 | + |
| 387 | + describe('redirectWalletAddress', () => { |
| 388 | + let ctx: WalletAddressContext |
| 389 | + let next: jest.MockedFunction<() => Promise<void>> |
| 390 | + const walletAddressPath = 'ilp.wallet/test' |
| 391 | + const walletAddressUrl = `https://${walletAddressPath}` |
| 392 | + const walletAddressRedirectHtmlPage = 'https://ilp.dev' |
| 393 | + |
| 394 | + beforeEach((): void => { |
| 395 | + ctx = createContext({}, {}) |
| 396 | + |
| 397 | + next = jest.fn() |
| 398 | + }) |
| 399 | + |
| 400 | + test('redirects to wallet address url', async (): Promise<void> => { |
| 401 | + ctx.container = initIocContainer({ |
| 402 | + ...Config, |
| 403 | + walletAddressRedirectHtmlPage |
| 404 | + }) |
| 405 | + ctx.walletAddressUrl = walletAddressUrl |
| 406 | + ctx.request.headers.accept = 'text/html' |
| 407 | + |
| 408 | + await expect( |
| 409 | + redirectIfBrowserAcceptsHtml(ctx, next) |
| 410 | + ).resolves.toBeUndefined() |
| 411 | + |
| 412 | + expect(ctx.response.status).toBe(302) |
| 413 | + expect(ctx.response.get('Location')).toBe( |
| 414 | + `${walletAddressRedirectHtmlPage}/${walletAddressPath}` |
| 415 | + ) |
| 416 | + expect(next).not.toHaveBeenCalled() |
| 417 | + }) |
| 418 | + |
| 419 | + test('no redirect to wallet address url if env is not set', async (): Promise<void> => { |
| 420 | + ctx.container = initIocContainer({ |
| 421 | + ...Config, |
| 422 | + walletAddressRedirectHtmlPage: undefined |
| 423 | + }) |
| 424 | + ctx.walletAddressUrl = walletAddressUrl |
| 425 | + ctx.request.headers.accept = 'text/html' |
| 426 | + |
| 427 | + await expect( |
| 428 | + redirectIfBrowserAcceptsHtml(ctx, next) |
| 429 | + ).resolves.toBeUndefined() |
| 430 | + |
| 431 | + expect(next).toHaveBeenCalled() |
| 432 | + }) |
| 433 | + |
| 434 | + test('no redirect to wallet address url if accept is not text/html', async (): Promise<void> => { |
| 435 | + ctx.container = initIocContainer({ |
| 436 | + ...Config, |
| 437 | + walletAddressRedirectHtmlPage |
| 438 | + }) |
| 439 | + ctx.walletAddressUrl = walletAddressUrl |
| 440 | + |
| 441 | + await expect( |
| 442 | + redirectIfBrowserAcceptsHtml(ctx, next) |
| 443 | + ).resolves.toBeUndefined() |
| 444 | + |
| 445 | + expect(next).toHaveBeenCalled() |
| 446 | + }) |
| 447 | + |
| 448 | + it('should trim trailing slashes from redirectHtmlPage', async (): Promise<void> => { |
| 449 | + ctx.container = initIocContainer({ |
| 450 | + ...Config, |
| 451 | + walletAddressRedirectHtmlPage: 'https://ilp.dev/' |
| 452 | + }) |
| 453 | + ctx.walletAddressUrl = `${walletAddressUrl}` |
| 454 | + ctx.request.headers.accept = 'text/html' |
| 455 | + |
| 456 | + await expect( |
| 457 | + redirectIfBrowserAcceptsHtml(ctx, next) |
| 458 | + ).resolves.toBeUndefined() |
| 459 | + |
| 460 | + expect(ctx.response.status).toBe(302) |
| 461 | + expect(ctx.response.get('Location')).toBe( |
| 462 | + `${walletAddressRedirectHtmlPage}/${walletAddressPath}` |
| 463 | + ) |
| 464 | + expect(next).not.toHaveBeenCalled() |
| 465 | + }) |
| 466 | + }) |
385 | 467 | })
|
0 commit comments