Skip to content

Commit 810c100

Browse files
committed
Add processFileHandlers tests
1 parent a7a7008 commit 810c100

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2025 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {
18+
processFileHandlers,
19+
FileHandler,
20+
FileHandlerJson,
21+
} from '../../lib/types/FileHandler';
22+
23+
describe('FileHandler', () => {
24+
describe('#processFileHandlers', () => {
25+
it('Accepts valid file handlers', () => {
26+
const startUrl = new URL('https://test.com/app/start');
27+
const scopeUrl = new URL('https://test.com/app');
28+
const testHandlers: FileHandlerJson[] = [
29+
{
30+
'action': '/app',
31+
'accept': {
32+
'text/plain': ['.txt'],
33+
},
34+
},
35+
{
36+
'action': '/app?image',
37+
'accept': {
38+
'image/jpeg': ['.jpg', 'jpeg'],
39+
},
40+
},
41+
];
42+
const expectedHandlers: FileHandler[] = [
43+
{
44+
actionUrl: 'https://test.com/app',
45+
mimeTypes: ['text/plain'],
46+
},
47+
{
48+
actionUrl: 'https://test.com/app?image',
49+
mimeTypes: ['image/jpeg'],
50+
},
51+
];
52+
const processedHandlers = processFileHandlers(testHandlers, startUrl, scopeUrl);
53+
expect(processedHandlers).toEqual(expectedHandlers);
54+
});
55+
it('Rejects invalid file handlers', () => {
56+
const startUrl = new URL('https://test.com/app/start');
57+
const scopeUrl = new URL('https://test.com/app');
58+
const testHandlers: FileHandlerJson[] = [
59+
{
60+
'action': '/app',
61+
},
62+
{
63+
'accept': {
64+
'text/plain': ['.txt'],
65+
},
66+
},
67+
{
68+
'action': '/app?image',
69+
'accept': {},
70+
},
71+
];
72+
const processedHandlers = processFileHandlers(testHandlers, startUrl, scopeUrl);
73+
expect(processedHandlers).toEqual([]);
74+
});
75+
it('Rejects invalid action URLs', () => {
76+
const startUrl = new URL('https://test.com/app/start');
77+
const scopeUrl = new URL('https://test.com/app');
78+
const testHandlers: FileHandlerJson[] = [
79+
{
80+
'action': '/', // not withing the scope
81+
'accept': {
82+
'text/plain': ['.txt'],
83+
},
84+
},
85+
{
86+
'action': 'http://test.com/app', // invalid protocol
87+
'accept': {
88+
'text/plain': ['.txt'],
89+
},
90+
},
91+
{
92+
'action': 'http://a.test.com/app', // invalid origin
93+
'accept': {
94+
'text/plain': ['.txt'],
95+
},
96+
},
97+
];
98+
const processedHandlers = processFileHandlers(testHandlers, startUrl, scopeUrl);
99+
expect(processedHandlers).toEqual([]);
100+
});
101+
});
102+
});

packages/core/src/spec/lib/features/FileHandlingFeatureSpec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import {FileHandlingFeature} from '../../../lib/features/FileHandlingFeature';
1818

1919
describe('FileHandlingFeature', () => {
2020
describe('#constructor', () => {
21-
// variables is the only field dynamically generated.
22-
it('Generates correct variables for application', () => {
21+
it('Populates templates from the twaManifest correctly', () => {
2322
const fileHandlers = [
2423
{
2524
'actionUrl': 'https://pwa-directory.com/',

0 commit comments

Comments
 (0)