Skip to content

Commit c66a807

Browse files
committed
test: add decompress-test fixture for real-world testing
Add comprehensive test fixture that demonstrates decompress functionality with real caching and httpbin backend. This fixture can be deployed by CI to real test environments. Features: - /gzip endpoint: Tests decompress: true (default) behavior - /gzip-compressed endpoint: Tests decompress: false behavior - /json endpoint: Tests JSON fetching with caching - /headers endpoint: Shows request headers and context Uses httpbin.org as backend for testing gzip decompression and caching behavior across Fastly and Cloudflare environments. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> Signed-off-by: Lars Trieloff <[email protected]>
1 parent e4a12c3 commit c66a807

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "decompress-test",
3+
"version": "1.0.0",
4+
"description": "Test Project for Decompress Functionality",
5+
"private": true,
6+
"license": "Apache-2.0",
7+
"main": "src/index.js",
8+
"type": "module",
9+
"wsk": {
10+
"name": "decompress-test",
11+
"webExport": true,
12+
"package": {
13+
"name": "decompress-package"
14+
}
15+
},
16+
"devDependencies": {
17+
"@adobe/fetch": "^4.1.8"
18+
}
19+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright 2024 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
import { Response, fetch } from '@adobe/fetch';
13+
14+
/**
15+
* Test action that demonstrates the decompress functionality with caching.
16+
*
17+
* Endpoints:
18+
* - /gzip - Fetches gzipped content from httpbin with decompress: true (default)
19+
* - /gzip-compressed - Fetches gzipped content with decompress: false
20+
* - /json - Fetches JSON content with caching
21+
* - /headers - Returns request headers
22+
*
23+
* @param {Request} req - The incoming request
24+
* @param {Object} context - The execution context
25+
* @returns {Response} The response
26+
*/
27+
export async function main(req, context) {
28+
const url = new URL(req.url);
29+
const path = url.pathname;
30+
31+
try {
32+
// Test different decompress scenarios
33+
if (path.includes('/gzip-compressed')) {
34+
// Fetch with decompress: false - should receive compressed data
35+
const response = await fetch('https://httpbin.org/gzip', {
36+
backend: 'httpbin.org',
37+
decompress: false,
38+
cacheKey: 'gzip-compressed',
39+
});
40+
41+
const isGzipped = response.headers.get('content-encoding') === 'gzip';
42+
43+
return new Response(JSON.stringify({
44+
test: 'decompress-false',
45+
contentEncoding: response.headers.get('content-encoding'),
46+
isGzipped,
47+
status: response.status,
48+
message: isGzipped ? 'Content is gzipped as expected' : 'Warning: Content not gzipped',
49+
}), {
50+
headers: { 'content-type': 'application/json' },
51+
});
52+
}
53+
54+
if (path.includes('/gzip')) {
55+
// Fetch with decompress: true (default) - should receive decompressed data
56+
const response = await fetch('https://httpbin.org/gzip', {
57+
backend: 'httpbin.org',
58+
decompress: true,
59+
cacheKey: 'gzip-decompressed',
60+
});
61+
62+
const data = await response.json();
63+
const isDecompressed = !response.headers.get('content-encoding');
64+
65+
return new Response(JSON.stringify({
66+
test: 'decompress-true',
67+
contentEncoding: response.headers.get('content-encoding') || 'none',
68+
isDecompressed,
69+
gzipped: data.gzipped || false,
70+
status: response.status,
71+
message: isDecompressed ? 'Content decompressed successfully' : 'Warning: Content still encoded',
72+
}), {
73+
headers: { 'content-type': 'application/json' },
74+
});
75+
}
76+
77+
if (path.includes('/json')) {
78+
// Test JSON endpoint with caching
79+
const response = await fetch('https://httpbin.org/json', {
80+
backend: 'httpbin.org',
81+
cacheKey: 'json-data',
82+
});
83+
84+
const data = await response.json();
85+
86+
return new Response(JSON.stringify({
87+
test: 'json-cached',
88+
slideshow: data.slideshow?.title || 'unknown',
89+
status: response.status,
90+
cached: response.headers.get('x-cache') === 'HIT',
91+
}), {
92+
headers: { 'content-type': 'application/json' },
93+
});
94+
}
95+
96+
if (path.includes('/headers')) {
97+
// Return request headers for debugging
98+
const headers = {};
99+
req.headers.forEach((value, key) => {
100+
headers[key] = value;
101+
});
102+
103+
return new Response(JSON.stringify({
104+
test: 'headers',
105+
headers,
106+
context: {
107+
functionName: context?.func?.name,
108+
runtime: context?.runtime?.name,
109+
},
110+
}), {
111+
headers: { 'content-type': 'application/json' },
112+
});
113+
}
114+
115+
// Default response with usage instructions
116+
return new Response(JSON.stringify({
117+
name: 'decompress-test',
118+
version: '1.0.0',
119+
endpoints: [
120+
{ path: '/gzip', description: 'Test decompress: true (default) - receives decompressed content' },
121+
{ path: '/gzip-compressed', description: 'Test decompress: false - receives compressed content' },
122+
{ path: '/json', description: 'Test JSON endpoint with caching' },
123+
{ path: '/headers', description: 'View request headers and context' },
124+
],
125+
runtime: context?.runtime?.name,
126+
region: context?.runtime?.region,
127+
}), {
128+
status: 200,
129+
headers: { 'content-type': 'application/json' },
130+
});
131+
} catch (error) {
132+
return new Response(JSON.stringify({
133+
error: error.message,
134+
stack: error.stack,
135+
}), {
136+
status: 500,
137+
headers: { 'content-type': 'application/json' },
138+
});
139+
}
140+
}

0 commit comments

Comments
 (0)