|
| 1 | +// deploy/test-rewrites.js |
| 2 | +const assert = require('assert'); |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// Load and evaluate the CloudFront function |
| 7 | +const functionCode = fs.readFileSync(path.join(__dirname, 'rewrites.js'), 'utf8'); |
| 8 | + |
| 9 | +// CloudFront functions don't use module.exports, so we need to eval it |
| 10 | +// and extract the handler function |
| 11 | +const handler = eval(`(function() { ${functionCode}; return handler; })()`); |
| 12 | + |
| 13 | +// Helper to create test events |
| 14 | +function createEvent(uri) { |
| 15 | + return { |
| 16 | + request: { |
| 17 | + uri: uri, |
| 18 | + method: 'GET', |
| 19 | + querystring: {}, |
| 20 | + headers: {} |
| 21 | + } |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +// Test suite |
| 26 | +console.log('Running CloudFront Rewrite Function Tests...\n'); |
| 27 | + |
| 28 | +let passed = 0; |
| 29 | +let failed = 0; |
| 30 | + |
| 31 | +function test(description, testFn) { |
| 32 | + try { |
| 33 | + testFn(); |
| 34 | + console.log(`✓ ${description}`); |
| 35 | + passed++; |
| 36 | + } catch (error) { |
| 37 | + console.log(`✗ ${description}`); |
| 38 | + console.log(` Error: ${error.message}`); |
| 39 | + failed++; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Test 1: /bushman redirect |
| 44 | +test('Redirects /bushman to https://usegalaxy.org/bushman/', () => { |
| 45 | + const event = createEvent('/bushman'); |
| 46 | + const result = handler(event); |
| 47 | + assert.strictEqual(result.statusCode, 302); |
| 48 | + assert.strictEqual(result.headers.location.value, 'https://usegalaxy.org/bushman/'); |
| 49 | +}); |
| 50 | + |
| 51 | +// Test 2: /bushman/ redirect (with trailing slash) |
| 52 | +test('Redirects /bushman/ to https://usegalaxy.org/bushman/', () => { |
| 53 | + const event = createEvent('/bushman/'); |
| 54 | + const result = handler(event); |
| 55 | + assert.strictEqual(result.statusCode, 302); |
| 56 | + assert.strictEqual(result.headers.location.value, 'https://usegalaxy.org/bushman/'); |
| 57 | +}); |
| 58 | + |
| 59 | +// Test 3: /learn/api redirect |
| 60 | +test('Redirects /learn/api to /develop/api/', () => { |
| 61 | + const event = createEvent('/learn/api'); |
| 62 | + const result = handler(event); |
| 63 | + assert.strictEqual(result.statusCode, 302); |
| 64 | + assert.strictEqual(result.headers.location.value, '/develop/api/'); |
| 65 | +}); |
| 66 | + |
| 67 | +// Test 4: /learn/api/ redirect (with trailing slash) |
| 68 | +test('Redirects /learn/api/ to /develop/api/', () => { |
| 69 | + const event = createEvent('/learn/api/'); |
| 70 | + const result = handler(event); |
| 71 | + assert.strictEqual(result.statusCode, 302); |
| 72 | + assert.strictEqual(result.headers.location.value, '/develop/api/'); |
| 73 | +}); |
| 74 | + |
| 75 | +// Test 5: Non-matching URI passes through |
| 76 | +test('Passes through non-matching URI /other/path', () => { |
| 77 | + const event = createEvent('/other/path'); |
| 78 | + const result = handler(event); |
| 79 | + assert.strictEqual(result.uri, '/other/path'); |
| 80 | + assert.strictEqual(result.statusCode, undefined); |
| 81 | +}); |
| 82 | + |
| 83 | +// Test 6: Root path passes through |
| 84 | +test('Passes through root path /', () => { |
| 85 | + const event = createEvent('/'); |
| 86 | + const result = handler(event); |
| 87 | + assert.strictEqual(result.uri, '/'); |
| 88 | + assert.strictEqual(result.statusCode, undefined); |
| 89 | +}); |
| 90 | + |
| 91 | +// Test 7: /bushman with additional path should not match |
| 92 | +test('Does not redirect /bushman/extra', () => { |
| 93 | + const event = createEvent('/bushman/extra'); |
| 94 | + const result = handler(event); |
| 95 | + assert.strictEqual(result.uri, '/bushman/extra'); |
| 96 | + assert.strictEqual(result.statusCode, undefined); |
| 97 | +}); |
| 98 | + |
| 99 | +// Test 8: /learn/api with additional path should not match |
| 100 | +test('Does not redirect /learn/api/extra', () => { |
| 101 | + const event = createEvent('/learn/api/extra'); |
| 102 | + const result = handler(event); |
| 103 | + assert.strictEqual(result.uri, '/learn/api/extra'); |
| 104 | + assert.strictEqual(result.statusCode, undefined); |
| 105 | +}); |
| 106 | + |
| 107 | +// Summary |
| 108 | +console.log(`\n${passed} passed, ${failed} failed`); |
| 109 | + |
| 110 | +if (failed > 0) { |
| 111 | + process.exit(1); |
| 112 | +} |
0 commit comments