| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {PrefetchAssetGroup} from '../src/assets'; | 
					
						
							|  |  |  | import {CacheDatabase} from '../src/db-cache'; | 
					
						
							|  |  |  | import {IdleScheduler} from '../src/idle'; | 
					
						
							| 
									
										
										
										
											2020-04-29 16:59:15 +02:00
										 |  |  | import {MockCache} from '../testing/cache'; | 
					
						
							|  |  |  | import {MockRequest} from '../testing/fetch'; | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTable, tmpManifestSingleAssetGroup} from '../testing/mock'; | 
					
						
							|  |  |  | import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 23:29:14 +02:00
										 |  |  | (function() { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | // Skip environments that don't support the minimum APIs needed to run the SW tests.
 | 
					
						
							|  |  |  | if (!SwTestHarness.envIsSupported()) { | 
					
						
							|  |  |  |   return; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | const dist = new MockFileSystemBuilder() | 
					
						
							| 
									
										
										
										
											2020-04-29 16:59:15 +02:00
										 |  |  |                  .addFile('/foo.txt', 'this is foo', {Vary: 'Accept'}) | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |                  .addFile('/bar.txt', 'this is bar') | 
					
						
							|  |  |  |                  .build(); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | const manifest = tmpManifestSingleAssetGroup(dist); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | const server = new MockServerStateBuilder().withStaticFiles(dist).withManifest(manifest).build(); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | const scope = new SwTestHarnessBuilder().withServerState(server).build(); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | const db = new CacheDatabase(scope, scope); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | describe('prefetch assets', () => { | 
					
						
							|  |  |  |   let group: PrefetchAssetGroup; | 
					
						
							|  |  |  |   let idle: IdleScheduler; | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2021-01-08 13:59:35 +02:00
										 |  |  |     idle = new IdleScheduler(null!, 3000, 30000, { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       log: (v, ctx = '') => console.error(v, ctx), | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     group = new PrefetchAssetGroup( | 
					
						
							|  |  |  |         scope, scope, idle, manifest.assetGroups![0], tmpHashTable(manifest), db, 'test'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   it('initializes without crashing', async () => { | 
					
						
							|  |  |  |     await group.initializeFully(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   it('fully caches the two files', async () => { | 
					
						
							|  |  |  |     await group.initializeFully(); | 
					
						
							|  |  |  |     scope.updateServerState(); | 
					
						
							|  |  |  |     const res1 = await group.handleFetch(scope.newRequest('/foo.txt'), scope); | 
					
						
							|  |  |  |     const res2 = await group.handleFetch(scope.newRequest('/bar.txt'), scope); | 
					
						
							|  |  |  |     expect(await res1!.text()).toEqual('this is foo'); | 
					
						
							|  |  |  |     expect(await res2!.text()).toEqual('this is bar'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   it('persists the cache across restarts', async () => { | 
					
						
							|  |  |  |     await group.initializeFully(); | 
					
						
							|  |  |  |     const freshScope = new SwTestHarnessBuilder().withCacheState(scope.caches.dehydrate()).build(); | 
					
						
							|  |  |  |     group = new PrefetchAssetGroup( | 
					
						
							|  |  |  |         freshScope, freshScope, idle, manifest.assetGroups![0], tmpHashTable(manifest), | 
					
						
							|  |  |  |         new CacheDatabase(freshScope, freshScope), 'test'); | 
					
						
							|  |  |  |     await group.initializeFully(); | 
					
						
							|  |  |  |     const res1 = await group.handleFetch(scope.newRequest('/foo.txt'), scope); | 
					
						
							|  |  |  |     const res2 = await group.handleFetch(scope.newRequest('/bar.txt'), scope); | 
					
						
							|  |  |  |     expect(await res1!.text()).toEqual('this is foo'); | 
					
						
							|  |  |  |     expect(await res2!.text()).toEqual('this is bar'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   it('caches properly if resources are requested before initialization', async () => { | 
					
						
							|  |  |  |     const res1 = await group.handleFetch(scope.newRequest('/foo.txt'), scope); | 
					
						
							|  |  |  |     const res2 = await group.handleFetch(scope.newRequest('/bar.txt'), scope); | 
					
						
							|  |  |  |     expect(await res1!.text()).toEqual('this is foo'); | 
					
						
							|  |  |  |     expect(await res2!.text()).toEqual('this is bar'); | 
					
						
							|  |  |  |     scope.updateServerState(); | 
					
						
							|  |  |  |     await group.initializeFully(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   it('throws if the server-side content does not match the manifest hash', async () => { | 
					
						
							|  |  |  |     const badHashFs = dist.extend().addFile('/foo.txt', 'corrupted file').build(); | 
					
						
							|  |  |  |     const badServer = | 
					
						
							|  |  |  |         new MockServerStateBuilder().withManifest(manifest).withStaticFiles(badHashFs).build(); | 
					
						
							|  |  |  |     const badScope = new SwTestHarnessBuilder().withServerState(badServer).build(); | 
					
						
							|  |  |  |     group = new PrefetchAssetGroup( | 
					
						
							|  |  |  |         badScope, badScope, idle, manifest.assetGroups![0], tmpHashTable(manifest), | 
					
						
							|  |  |  |         new CacheDatabase(badScope, badScope), 'test'); | 
					
						
							|  |  |  |     const err = await errorFrom(group.initializeFully()); | 
					
						
							|  |  |  |     expect(err.message).toContain('Hash mismatch'); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-04-29 16:59:15 +02:00
										 |  |  |   it('CacheQueryOptions are passed through', async () => { | 
					
						
							|  |  |  |     await group.initializeFully(); | 
					
						
							|  |  |  |     const matchSpy = spyOn(MockCache.prototype, 'match').and.callThrough(); | 
					
						
							|  |  |  |     await group.handleFetch(scope.newRequest('/foo.txt'), scope); | 
					
						
							|  |  |  |     expect(matchSpy).toHaveBeenCalledWith(new MockRequest('/foo.txt'), {ignoreVary: true}); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | })(); | 
					
						
							| 
									
										
										
										
											2017-09-28 16:18:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | function errorFrom(promise: Promise<any>): Promise<any> { | 
					
						
							|  |  |  |   return promise.catch(err => err); | 
					
						
							|  |  |  | } |