| 1 | // Copyright (c) 2019-2026 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module s3 |
| 5 | |
| 6 | fn test_extract_xml_tag_basic() { |
| 7 | body := '<?xml version="1.0"?><Error><Code>NoSuchKey</Code><Message>Key not found</Message><RequestId>R1</RequestId></Error>' |
| 8 | assert extract_xml_tag(body, 'Code') == 'NoSuchKey' |
| 9 | assert extract_xml_tag(body, 'Message') == 'Key not found' |
| 10 | assert extract_xml_tag(body, 'RequestId') == 'R1' |
| 11 | assert extract_xml_tag(body, 'Missing') == '' |
| 12 | } |
| 13 | |
| 14 | fn test_extract_xml_tag_decodes_entities() { |
| 15 | body := '<Message>a & b <tag></Message>' |
| 16 | assert extract_xml_tag(body, 'Message') == 'a & b <tag>' |
| 17 | } |
| 18 | |
| 19 | fn test_decode_xml_entities() { |
| 20 | assert decode_xml_entities('"hello"') == '"hello"' |
| 21 | assert decode_xml_entities('a & b < c > d 'e'') == "a & b < c > d 'e'" |
| 22 | assert decode_xml_entities('no entities here') == 'no entities here' |
| 23 | } |
| 24 | |
| 25 | fn test_s3error_msg_contains_code_and_path() { |
| 26 | e := S3Error{ |
| 27 | code: 'NoSuchKey' |
| 28 | message: 'object missing' |
| 29 | status: 404 |
| 30 | path: 'foo/bar.txt' |
| 31 | } |
| 32 | rendered := e.msg() |
| 33 | assert rendered.contains('NoSuchKey') |
| 34 | assert rendered.contains('object missing') |
| 35 | assert rendered.contains('404') |
| 36 | assert rendered.contains('foo/bar.txt') |
| 37 | } |
| 38 | |
| 39 | fn test_s3error_code_maps_known_strings() { |
| 40 | assert (&S3Error{ |
| 41 | code: 'NoSuchKey' |
| 42 | }).code() == 404 |
| 43 | assert (&S3Error{ |
| 44 | code: 'NoSuchBucket' |
| 45 | }).code() == 404 |
| 46 | assert (&S3Error{ |
| 47 | code: 'AccessDenied' |
| 48 | }).code() == 403 |
| 49 | assert (&S3Error{ |
| 50 | code: 'BucketAlreadyExists' |
| 51 | }).code() == 409 |
| 52 | // Unknown code falls back to the wire status. |
| 53 | assert (&S3Error{ |
| 54 | code: 'WeirdCode' |
| 55 | status: 418 |
| 56 | }).code() == 418 |
| 57 | } |
| 58 | |
| 59 | fn test_new_http_error_parses_xml_body() { |
| 60 | body := '<?xml version="1.0"?><Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message><Resource>/bucket/missing.txt</Resource><RequestId>ABC123</RequestId></Error>' |
| 61 | err := new_http_error(404, 'missing.txt', body) |
| 62 | if err is S3Error { |
| 63 | assert err.code == 'NoSuchKey' |
| 64 | assert err.message == 'The specified key does not exist.' |
| 65 | assert err.resource == '/bucket/missing.txt' |
| 66 | assert err.request_id == 'ABC123' |
| 67 | assert err.status == 404 |
| 68 | assert err.path == 'missing.txt' |
| 69 | } else { |
| 70 | assert false, 'expected S3Error' |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn test_new_http_error_falls_back_when_body_empty() { |
| 75 | err := new_http_error(503, 'foo', '') |
| 76 | if err is S3Error { |
| 77 | assert err.code == 'ServiceUnavailable' |
| 78 | assert err.status == 503 |
| 79 | } else { |
| 80 | assert false, 'expected S3Error' |
| 81 | } |
| 82 | } |
| 83 | |