|
| 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