Add redirect base setup and tests.
This commit is contained in:
		
							parent
							
								
									b6fb952580
								
							
						
					
					
						commit
						8e96a55661
					
				|  | @ -0,0 +1,9 @@ | |||
| import {HostnameParameters} from './hostname.js'; | ||||
| 
 | ||||
| export type RedirectParameters = HostnameParameters; | ||||
| 
 | ||||
| export abstract class Redirect<P extends RedirectParameters> { | ||||
|   constructor(public parameters: P) {} | ||||
| 
 | ||||
|   public abstract redirect(url: URL | string): URL; | ||||
| } | ||||
|  | @ -0,0 +1,15 @@ | |||
| import {RedirectParameters} from './base.js'; | ||||
| import {HostnameRedirect} from './hostname.js'; | ||||
| 
 | ||||
| export * from './base.js'; | ||||
| export * from './hostname.js'; | ||||
| 
 | ||||
| export type Redirects = HostnameRedirect; | ||||
| 
 | ||||
| export function parseRedirect<P extends RedirectParameters>( | ||||
|   parameters: P, | ||||
| ): Redirects | undefined { | ||||
|   if (parameters.type === 'hostname') { | ||||
|     return new HostnameRedirect(parameters); | ||||
|   } | ||||
| } | ||||
|  | @ -0,0 +1,14 @@ | |||
| import {Redirect} from './base.js'; | ||||
| 
 | ||||
| export type HostnameParameters = { | ||||
|   hostname: string; | ||||
|   type: 'hostname'; | ||||
| }; | ||||
| 
 | ||||
| export class HostnameRedirect extends Redirect<HostnameParameters> { | ||||
|   public redirect(url: URL | string): URL { | ||||
|     const redirected = new URL(url); | ||||
|     redirected.hostname = this.parameters.hostname; | ||||
|     return redirected; | ||||
|   } | ||||
| } | ||||
|  | @ -0,0 +1,54 @@ | |||
| /// <reference path="../source/types.d.ts" />
 | ||||
| 
 | ||||
| import test from 'ava'; | ||||
| 
 | ||||
| import { | ||||
|   parseRedirect, | ||||
|   HostnameRedirect, | ||||
|   Redirect, | ||||
|   RedirectParameters, | ||||
| } from '../source/redirect/exports.js'; | ||||
| 
 | ||||
| test('parseRedirect', (t) => { | ||||
|   const samples: RedirectParameters[] = [ | ||||
|     { | ||||
|       test: 'Invalid parameters', | ||||
|     } as unknown as RedirectParameters, | ||||
|     { | ||||
|       hostname: 'example.org', | ||||
|       type: 'hostname', | ||||
|     }, | ||||
|   ]; | ||||
| 
 | ||||
|   for (const sample of samples) { | ||||
|     const redirect = parseRedirect(sample); | ||||
| 
 | ||||
|     if (redirect === undefined) { | ||||
|       t.pass('parseRedirect returned undefined'); | ||||
|     } else { | ||||
|       t.snapshot(redirect, `Class ${redirect.constructor.name}`); | ||||
|     } | ||||
|   } | ||||
| }); | ||||
| 
 | ||||
| test('Redirect.redirect', (t) => { | ||||
|   const hostnameRedirect = new HostnameRedirect({ | ||||
|     hostname: 'example.org', | ||||
|     type: 'hostname', | ||||
|   }); | ||||
| 
 | ||||
|   const samples: Array<[string, Redirect<RedirectParameters>]> = [ | ||||
|     ['https://example.com', hostnameRedirect], | ||||
|     ['https://example.com/path#hash?query=test', hostnameRedirect], | ||||
|   ]; | ||||
| 
 | ||||
|   for (const [index, [url, redirect]] of samples.entries()) { | ||||
|     t.snapshot( | ||||
|       { | ||||
|         original: url, | ||||
|         redirected: redirect.redirect(url).href, | ||||
|       }, | ||||
|       `${index} ${redirect.constructor.name}`, | ||||
|     ); | ||||
|   } | ||||
| }); | ||||
|  | @ -0,0 +1,32 @@ | |||
| # Snapshot report for `tests/redirect.test.ts` | ||||
| 
 | ||||
| The actual snapshot is saved in `redirect.test.ts.snap`. | ||||
| 
 | ||||
| Generated by [AVA](https://avajs.dev). | ||||
| 
 | ||||
| ## parseRedirect | ||||
| 
 | ||||
| > Class HostnameRedirect | ||||
| 
 | ||||
|     HostnameRedirect { | ||||
|       parameters: { | ||||
|         hostname: 'example.org', | ||||
|         type: 'hostname', | ||||
|       }, | ||||
|     } | ||||
| 
 | ||||
| ## Redirect.redirect | ||||
| 
 | ||||
| > 0 HostnameRedirect | ||||
| 
 | ||||
|     { | ||||
|       original: 'https://example.com', | ||||
|       redirected: 'https://example.org/', | ||||
|     } | ||||
| 
 | ||||
| > 1 HostnameRedirect | ||||
| 
 | ||||
|     { | ||||
|       original: 'https://example.com/path#hash?query=test', | ||||
|       redirected: 'https://example.org/path#hash?query=test', | ||||
|     } | ||||
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue