File
Blob: src/workerd/api/tests/queue-error-codes-test.js
| 1 | // Copyright (c) 2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | import assert from 'node:assert'; |
| 6 | import { Buffer } from 'node:buffer'; |
| 7 | |
| 8 | export default { |
| 9 | async fetch(request, env, ctx) { |
| 10 | const { pathname } = new URL(request.url); |
| 11 | |
| 12 | if (pathname === '/message') { |
| 13 | const body = await request.text(); |
| 14 | |
| 15 | // Return error with headers defined |
| 16 | if (body.includes('with-headers')) { |
| 17 | return new Response('', { |
| 18 | status: 503, |
| 19 | headers: { |
| 20 | 'CF-Queues-Error-Code': '10503', |
| 21 | 'CF-Queues-Error-Cause': 'Service temporarily unavailable', |
| 22 | }, |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | // Return error without headers |
| 27 | if (body.includes('no-headers')) { |
| 28 | return new Response('', { status: 503 }); |
| 29 | } |
| 30 | |
| 31 | // Default success |
| 32 | return new Response(''); |
| 33 | } |
| 34 | |
| 35 | if (pathname === '/batch') { |
| 36 | const body = await request.json(); |
| 37 | |
| 38 | if (body.messages.length > 0) { |
| 39 | const firstMessage = Buffer.from( |
| 40 | body.messages[0].body, |
| 41 | 'base64' |
| 42 | ).toString(); |
| 43 | |
| 44 | // Return error with headers defined |
| 45 | if (firstMessage.includes('with-headers')) { |
| 46 | return new Response('', { |
| 47 | status: 503, |
| 48 | headers: { |
| 49 | 'CF-Queues-Error-Code': '10503', |
| 50 | 'CF-Queues-Error-Cause': 'Service temporarily unavailable', |
| 51 | }, |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | // Return error without headers |
| 56 | if (firstMessage.includes('no-headers')) { |
| 57 | return new Response('', { status: 503 }); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Default success |
| 62 | return new Response(''); |
| 63 | } |
| 64 | |
| 65 | if (pathname === '/metrics') { |
| 66 | // Return error with headers defined for metrics |
| 67 | return new Response('', { |
| 68 | status: 503, |
| 69 | headers: { |
| 70 | 'CF-Queues-Error-Code': '10503', |
| 71 | 'CF-Queues-Error-Cause': 'Service temporarily unavailable', |
| 72 | }, |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | return new Response('Not Found', { status: 404 }); |
| 77 | }, |
| 78 | |
| 79 | async test(ctrl, env, ctx) { |
| 80 | const flagEnabled = env.ERROR_CODES_FLAG; |
| 81 | |
| 82 | // Test with error headers defined |
| 83 | try { |
| 84 | await env.QUEUE.send('with-headers', { contentType: 'text' }); |
| 85 | assert.fail('Expected send() to throw'); |
| 86 | } catch (error) { |
| 87 | if (flagEnabled) { |
| 88 | // Flag ON + Headers defined → detailed error with code |
| 89 | assert.strictEqual( |
| 90 | error.message, |
| 91 | 'Service temporarily unavailable (10503)' |
| 92 | ); |
| 93 | } else { |
| 94 | // Flag OFF + Headers defined → generic error (ignores headers) |
| 95 | assert.strictEqual( |
| 96 | error.message, |
| 97 | 'Queue send failed: Service Unavailable' |
| 98 | ); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Test without error headers |
| 103 | try { |
| 104 | await env.QUEUE.send('no-headers', { contentType: 'text' }); |
| 105 | assert.fail('Expected send() to throw'); |
| 106 | } catch (error) { |
| 107 | if (flagEnabled) { |
| 108 | // Flag ON + Headers missing → default error with code 15000 |
| 109 | assert.strictEqual(error.message, 'Unknown Internal Error (15000)'); |
| 110 | } else { |
| 111 | // Flag OFF + Headers missing → generic error |
| 112 | assert.strictEqual( |
| 113 | error.message, |
| 114 | 'Queue send failed: Service Unavailable' |
| 115 | ); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Test sendBatch with headers defined |
| 120 | try { |
| 121 | await env.QUEUE.sendBatch([ |
| 122 | { body: 'with-headers', contentType: 'text' }, |
| 123 | ]); |
| 124 | assert.fail('Expected sendBatch() to throw'); |
| 125 | } catch (error) { |
| 126 | if (flagEnabled) { |
| 127 | // Flag ON + Headers defined → detailed error with code |
| 128 | assert.strictEqual( |
| 129 | error.message, |
| 130 | 'Service temporarily unavailable (10503)' |
| 131 | ); |
| 132 | } else { |
| 133 | // Flag OFF + Headers defined → generic error (ignores headers) |
| 134 | assert.strictEqual( |
| 135 | error.message, |
| 136 | 'Queue sendBatch failed: Service Unavailable' |
| 137 | ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Test sendBatch without headers |
| 142 | try { |
| 143 | await env.QUEUE.sendBatch([{ body: 'no-headers', contentType: 'text' }]); |
| 144 | assert.fail('Expected sendBatch() to throw'); |
| 145 | } catch (error) { |
| 146 | if (flagEnabled) { |
| 147 | // Flag ON + Headers missing → default error with code 15000 |
| 148 | assert.strictEqual(error.message, 'Unknown Internal Error (15000)'); |
| 149 | } else { |
| 150 | // Flag OFF + Headers missing → generic error |
| 151 | assert.strictEqual( |
| 152 | error.message, |
| 153 | 'Queue sendBatch failed: Service Unavailable' |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Test metrics with error headers defined |
| 159 | try { |
| 160 | await env.QUEUE.metrics(); |
| 161 | assert.fail('Expected metrics() to throw'); |
| 162 | } catch (error) { |
| 163 | assert.strictEqual( |
| 164 | error.message, |
| 165 | 'Service temporarily unavailable (10503)' |
| 166 | ); |
| 167 | } |
| 168 | }, |
| 169 | }; |