From eb18b4ab1fcd7f9b819c03eddf087a4490ffd134 Mon Sep 17 00:00:00 2001 From: XuJian <265865031+xujiantop-crypto@users.noreply.github.com> Date: Mon, 21 Sep 2026 05:33:18 +0900 Subject: [PATCH] fix(cloudfront): preserve policy configurations (#4009) * fix(cloudfront): preserve policy configurations * fix(cloudfront): align policy config edge cases --- .../cloudfront/CloudFrontController.java | 36 +-- .../CloudFrontPolicyConfigCodec.java | 191 ++++++++++++++ .../CloudFrontManagementProtocolTest.java | 239 ++++++++++++++++++ .../CloudFrontPolicyConfigCodecTest.java | 163 ++++++++++++ 4 files changed, 613 insertions(+), 16 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontPolicyConfigCodec.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontPolicyConfigCodecTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java index b6cdf61f8c..c4534ae1b1 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java @@ -302,13 +302,13 @@ public class CloudFrontController { public Response getCachePolicyConfig(@PathParam("Id") String id) { try { CachePolicy policy = service.getCachePolicy(id); - String xml = new XmlBuilder() + XmlBuilder xml = new XmlBuilder() .start("CachePolicyConfig", NS) .elem("Name", policy.getName()) - .elem("Comment", policy.getComment() != null ? policy.getComment() : "") - .end("CachePolicyConfig") - .build(); - return Response.ok(xml, XML).header("ETag", policy.getEtag()).build(); + .elem("Comment", policy.getComment() != null ? policy.getComment() : ""); + CloudFrontPolicyConfigCodec.serializeCachePolicy(xml, policy.getConfig()); + return Response.ok(xml.end("CachePolicyConfig").build(), XML) + .header("ETag", policy.getEtag()).build(); } catch (AwsException e) { return xmlErrorResponse(e); } @@ -414,13 +414,13 @@ public class CloudFrontController { public Response getOriginRequestPolicyConfig(@PathParam("Id") String id) { try { OriginRequestPolicy policy = service.getOriginRequestPolicy(id); - String xml = new XmlBuilder() + XmlBuilder xml = new XmlBuilder() .start("OriginRequestPolicyConfig", NS) .elem("Name", policy.getName()) - .elem("Comment", policy.getComment() != null ? policy.getComment() : "") - .end("OriginRequestPolicyConfig") - .build(); - return Response.ok(xml, XML).header("ETag", policy.getEtag()).build(); + .elem("Comment", policy.getComment() != null ? policy.getComment() : ""); + CloudFrontPolicyConfigCodec.serializeOriginRequestPolicy(xml, policy.getConfig()); + return Response.ok(xml.end("OriginRequestPolicyConfig").build(), XML) + .header("ETag", policy.getEtag()).build(); } catch (AwsException e) { return xmlErrorResponse(e); } @@ -2311,29 +2311,31 @@ public class CloudFrontController { } private String xmlCachePolicyResponse(CachePolicy policy) { - return new XmlBuilder() + XmlBuilder xml = new XmlBuilder() .start("CachePolicy") .elem("Id", policy.getId()) .elem("LastModifiedTime", policy.getLastModifiedTime() != null ? policy.getLastModifiedTime().toString() : "") .start("CachePolicyConfig") .elem("Name", policy.getName()) - .elem("Comment", policy.getComment() != null ? policy.getComment() : "") - .end("CachePolicyConfig") + .elem("Comment", policy.getComment() != null ? policy.getComment() : ""); + CloudFrontPolicyConfigCodec.serializeCachePolicy(xml, policy.getConfig()); + return xml.end("CachePolicyConfig") .end("CachePolicy") .build(); } private String xmlOriginRequestPolicyResponse(OriginRequestPolicy policy) { - return new XmlBuilder() + XmlBuilder xml = new XmlBuilder() .start("OriginRequestPolicy") .elem("Id", policy.getId()) .elem("LastModifiedTime", policy.getLastModifiedTime() != null ? policy.getLastModifiedTime().toString() : "") .start("OriginRequestPolicyConfig") .elem("Name", policy.getName()) - .elem("Comment", policy.getComment() != null ? policy.getComment() : "") - .end("OriginRequestPolicyConfig") + .elem("Comment", policy.getComment() != null ? policy.getComment() : ""); + CloudFrontPolicyConfigCodec.serializeOriginRequestPolicy(xml, policy.getConfig()); + return xml.end("OriginRequestPolicyConfig") .end("OriginRequestPolicy") .build(); } @@ -3538,6 +3540,7 @@ public class CloudFrontController { CachePolicy policy = new CachePolicy(); policy.setName(XmlParser.extractFirst(body, "Name", null)); policy.setComment(XmlParser.extractFirst(body, "Comment", null)); + policy.setConfig(CloudFrontPolicyConfigCodec.parseCachePolicy(body)); return policy; } @@ -3545,6 +3548,7 @@ public class CloudFrontController { OriginRequestPolicy policy = new OriginRequestPolicy(); policy.setName(XmlParser.extractFirst(body, "Name", null)); policy.setComment(XmlParser.extractFirst(body, "Comment", null)); + policy.setConfig(CloudFrontPolicyConfigCodec.parseOriginRequestPolicy(body)); return policy; } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontPolicyConfigCodec.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontPolicyConfigCodec.java new file mode 100644 index 0000000000..afe3833c32 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontPolicyConfigCodec.java @@ -0,0 +1,191 @@ +package io.github.hectorvent.floci.services.cloudfront; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.XmlBuilder; +import io.github.hectorvent.floci.core.common.XmlParser; +import io.github.hectorvent.floci.core.common.XmlParser.XmlElement; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** Parses and serializes nested configuration for CloudFront cache and origin request policies. */ +final class CloudFrontPolicyConfigCodec { + + private static final long DEFAULT_TTL = 86_400L; + private static final long DEFAULT_MAX_TTL = 31_536_000L; + + private CloudFrontPolicyConfigCodec() { + } + + static Map parseCachePolicy(String body) { + XmlElement root = requireRoot(body, "CachePolicyConfig"); + Map config = new LinkedHashMap<>(); + copyScalar(root, config, "MinTTL"); + copyScalar(root, config, "DefaultTTL"); + copyScalar(root, config, "MaxTTL"); + applyCachePolicyTtlDefaults(config); + + XmlElement parameters = root.child("ParametersInCacheKeyAndForwardedToOrigin"); + if (parameters != null) { + Map values = new LinkedHashMap<>(); + copyScalar(parameters, values, "EnableAcceptEncodingGzip"); + copyScalar(parameters, values, "EnableAcceptEncodingBrotli"); + copySelection(parameters, values, "HeadersConfig", "HeaderBehavior", "Headers"); + copySelection(parameters, values, "CookiesConfig", "CookieBehavior", "Cookies"); + copySelection(parameters, values, "QueryStringsConfig", "QueryStringBehavior", "QueryStrings"); + config.put("ParametersInCacheKeyAndForwardedToOrigin", values); + } + return config; + } + + static Map parseOriginRequestPolicy(String body) { + XmlElement root = requireRoot(body, "OriginRequestPolicyConfig"); + Map config = new LinkedHashMap<>(); + copySelection(root, config, "HeadersConfig", "HeaderBehavior", "Headers"); + copySelection(root, config, "CookiesConfig", "CookieBehavior", "Cookies"); + copySelection(root, config, "QueryStringsConfig", "QueryStringBehavior", "QueryStrings"); + return config; + } + + static void serializeCachePolicy(XmlBuilder xml, Map config) { + if (config == null) { + return; + } + serializeScalar(xml, config, "MinTTL"); + serializeScalar(xml, config, "DefaultTTL"); + serializeScalar(xml, config, "MaxTTL"); + + Map parameters = asMap(config.get("ParametersInCacheKeyAndForwardedToOrigin")); + if (parameters != null) { + xml.start("ParametersInCacheKeyAndForwardedToOrigin"); + serializeScalar(xml, parameters, "EnableAcceptEncodingGzip"); + serializeScalar(xml, parameters, "EnableAcceptEncodingBrotli"); + serializeSelection(xml, parameters, "HeadersConfig", "HeaderBehavior", "Headers"); + serializeSelection(xml, parameters, "CookiesConfig", "CookieBehavior", "Cookies"); + serializeSelection(xml, parameters, "QueryStringsConfig", "QueryStringBehavior", "QueryStrings"); + xml.end("ParametersInCacheKeyAndForwardedToOrigin"); + } + } + + static void serializeOriginRequestPolicy(XmlBuilder xml, Map config) { + if (config == null) { + return; + } + serializeSelection(xml, config, "HeadersConfig", "HeaderBehavior", "Headers"); + serializeSelection(xml, config, "CookiesConfig", "CookieBehavior", "Cookies"); + serializeSelection(xml, config, "QueryStringsConfig", "QueryStringBehavior", "QueryStrings"); + } + + private static XmlElement requireRoot(String body, String name) { + XmlElement root = XmlParser.extractElementTree(body, name); + if (root == null) { + throw new AwsException("InvalidArgument", + "The request body must contain a valid " + name + " element.", 400); + } + return root; + } + + private static void copyScalar(XmlElement parent, Map target, String name) { + XmlElement child = parent.child(name); + if (child != null) { + target.put(name, child.text()); + } + } + + private static void copySelection(XmlElement parent, Map target, + String blockName, String behaviorName, String listName) { + XmlElement block = parent.child(blockName); + if (block == null) { + return; + } + Map values = new LinkedHashMap<>(); + copyScalar(block, values, behaviorName); + XmlElement names = block.child(listName); + if (names != null) { + List items = new ArrayList<>(); + XmlElement itemContainer = names.child("Items"); + if (itemContainer != null) { + for (XmlElement item : itemContainer.children()) { + if ("Name".equals(item.name())) { + items.add(item.text()); + } + } + } + values.put(listName, items); + } + target.put(blockName, values); + } + + private static void serializeSelection(XmlBuilder xml, Map parent, + String blockName, String behaviorName, String listName) { + Map block = asMap(parent.get(blockName)); + if (block == null) { + return; + } + xml.start(blockName); + serializeScalar(xml, block, behaviorName); + if (block.containsKey(listName)) { + List items = stringList(block.get(listName)); + xml.start(listName).elem("Quantity", items.size()); + if (!items.isEmpty()) { + xml.start("Items"); + for (String item : items) { + xml.elem("Name", item); + } + xml.end("Items"); + } + xml.end(listName); + } + xml.end(blockName); + } + + private static void serializeScalar(XmlBuilder xml, Map values, String name) { + Object value = values.get(name); + if (value != null) { + xml.elem(name, value.toString()); + } + } + + private static void applyCachePolicyTtlDefaults(Map config) { + long minTtl = longValue(config.get("MinTTL"), 0L); + if (!config.containsKey("DefaultTTL")) { + config.put("DefaultTTL", Long.toString(Math.max(DEFAULT_TTL, minTtl))); + } + if (!config.containsKey("MaxTTL")) { + long defaultTtl = longValue(config.get("DefaultTTL"), DEFAULT_TTL); + long maxTtl = minTtl > DEFAULT_MAX_TTL || defaultTtl > DEFAULT_MAX_TTL + ? defaultTtl : DEFAULT_MAX_TTL; + config.put("MaxTTL", Long.toString(maxTtl)); + } + } + + private static long longValue(Object value, long fallback) { + if (value == null) { + return fallback; + } + try { + return Long.parseLong(value.toString()); + } catch (NumberFormatException ignored) { + return fallback; + } + } + + private static Map asMap(Object value) { + return value instanceof Map map ? map : null; + } + + private static List stringList(Object value) { + if (!(value instanceof List list)) { + return List.of(); + } + List strings = new ArrayList<>(list.size()); + for (Object item : list) { + if (item != null) { + strings.add(item.toString()); + } + } + return strings; + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java index 374701decd..f62ce8e663 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java @@ -1,6 +1,8 @@ package io.github.hectorvent.floci.services.cloudfront; +import io.github.hectorvent.floci.core.common.XmlParser; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -208,4 +210,241 @@ class CloudFrontManagementProtocolTest { "//*[local-name()='Code']/text()", equalTo("InvalidArgument"))); } + + @Test + void policyCreationRejectsMissingConfigurationRoot() { + given() + .contentType("application/xml") + .body("missing-cache-root") + .when() + .post(API + "cache-policy") + .then() + .statusCode(400) + .body(hasXPath("//*[local-name()='Code']/text()", equalTo("InvalidArgument"))); + + given() + .contentType("application/xml") + .body("missing-origin-root") + .when() + .post(API + "origin-request-policy") + .then() + .statusCode(400) + .body(hasXPath("//*[local-name()='Code']/text()", equalTo("InvalidArgument"))); + } + + @Test + void cachePolicyReadFillsAwsTtlDefaults() { + String name = "default-ttl-cache-policy-" + UUID.randomUUID(); + String id = null; + String etag = null; + try { + Response created = given() + .contentType("application/xml") + .body(""" + + %s + 10 + + false + false + none + none + + none + + + + """.formatted(name)) + .when() + .post(API + "cache-policy"); + created.then() + .statusCode(201) + .body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("86400"))) + .body(hasXPath("//*[local-name()='MaxTTL']/text()", equalTo("31536000"))); + id = XmlParser.extractFirst(created.asString(), "Id", null); + etag = created.header("ETag"); + + given() + .when() + .get(API + "cache-policy/" + id + "/config") + .then() + .statusCode(200) + .body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("86400"))) + .body(hasXPath("//*[local-name()='MaxTTL']/text()", equalTo("31536000"))); + } finally { + if (id != null && etag != null) { + given().header("If-Match", etag) + .when().delete(API + "cache-policy/" + id) + .then().statusCode(204); + } + } + } + + @Test + void cachePolicyPreservesItsCompleteConfigAcrossManagementOperations() { + String name = "complete-cache-policy-" + UUID.randomUUID(); + String id = null; + String etag = null; + try { + Response created = given() + .contentType("application/xml") + .body(cachePolicyBody(name, "created", "3600", "session")) + .when() + .post(API + "cache-policy"); + created.then() + .statusCode(201) + .body(hasXPath("//*[local-name()='MinTTL']/text()", equalTo("10"))) + .body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("3600"))) + .body(hasXPath("//*[local-name()='MaxTTL']/text()", equalTo("86400"))) + .body(hasXPath("//*[local-name()='EnableAcceptEncodingGzip']/text()", equalTo("true"))) + .body(hasXPath("//*[local-name()='EnableAcceptEncodingBrotli']/text()", equalTo("true"))) + .body(hasXPath("//*[local-name()='Cookies']/*[local-name()='Quantity']/text()", + equalTo("2"))) + .body(hasXPath("//*[local-name()='Cookies']//*[local-name()='Name' and text()='session']")); + id = XmlParser.extractFirst(created.asString(), "Id", null); + etag = created.header("ETag"); + + given() + .when() + .get(API + "cache-policy/" + id + "/config") + .then() + .statusCode(200) + .header("ETag", equalTo(etag)) + .body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("3600"))) + .body(hasXPath( + "//*[local-name()='Headers']//*[local-name()='Name' and text()='Accept-Language']")) + .body(hasXPath( + "//*[local-name()='QueryStrings']//*[local-name()='Name' and text()='page']")); + + given() + .when() + .get(API + "cache-policy") + .then() + .statusCode(200) + .body(hasXPath("//*[local-name()='CachePolicy'][*[local-name()='Id' and text()='" + id + + "']]//*[local-name()='DefaultTTL']/text()", equalTo("3600"))); + + Response updated = given() + .contentType("application/xml") + .header("If-Match", etag) + .body(cachePolicyBody(name, "updated", "7200", "updated-session")) + .when() + .put(API + "cache-policy/" + id); + updated.then() + .statusCode(200) + .header("ETag", not(equalTo(etag))) + .body(hasXPath("//*[local-name()='Comment']/text()", equalTo("updated"))) + .body(hasXPath("//*[local-name()='DefaultTTL']/text()", equalTo("7200"))) + .body(hasXPath( + "//*[local-name()='Cookies']//*[local-name()='Name' and text()='updated-session']")); + etag = updated.header("ETag"); + } finally { + if (id != null && etag != null) { + given().header("If-Match", etag) + .when().delete(API + "cache-policy/" + id) + .then().statusCode(204); + } + } + } + + @Test + void originRequestPolicyPreservesItsCompleteConfigAcrossManagementOperations() { + String name = "complete-origin-policy-" + UUID.randomUUID(); + String id = null; + String etag = null; + try { + Response created = given() + .contentType("application/xml") + .body(originRequestPolicyBody(name, "created", "Origin")) + .when() + .post(API + "origin-request-policy"); + created.then() + .statusCode(201) + .body(hasXPath("//*[local-name()='HeaderBehavior']/text()", equalTo("whitelist"))) + .body(hasXPath("//*[local-name()='Headers']//*[local-name()='Name' and text()='Origin']")) + .body(hasXPath("//*[local-name()='Cookies']//*[local-name()='Name' and text()='session']")) + .body(hasXPath( + "//*[local-name()='QueryStrings']//*[local-name()='Name' and text()='page']")); + id = XmlParser.extractFirst(created.asString(), "Id", null); + etag = created.header("ETag"); + + given() + .when() + .get(API + "origin-request-policy/" + id + "/config") + .then() + .statusCode(200) + .header("ETag", equalTo(etag)) + .body(hasXPath("//*[local-name()='HeaderBehavior']/text()", equalTo("whitelist"))) + .body(hasXPath("//*[local-name()='Headers']/*[local-name()='Quantity']/text()", + equalTo("1"))); + + given() + .when() + .get(API + "origin-request-policy") + .then() + .statusCode(200) + .body(hasXPath("//*[local-name()='OriginRequestPolicy'][*[local-name()='Id' and text()='" + id + + "']]//*[local-name()='QueryStringBehavior']/text()", equalTo("whitelist"))); + + Response updated = given() + .contentType("application/xml") + .header("If-Match", etag) + .body(originRequestPolicyBody(name, "updated", "CloudFront-Viewer-Country")) + .when() + .put(API + "origin-request-policy/" + id); + updated.then() + .statusCode(200) + .header("ETag", not(equalTo(etag))) + .body(hasXPath("//*[local-name()='Comment']/text()", equalTo("updated"))) + .body(hasXPath( + "//*[local-name()='Headers']//*[local-name()='Name'" + + " and text()='CloudFront-Viewer-Country']")); + etag = updated.header("ETag"); + } finally { + if (id != null && etag != null) { + given().header("If-Match", etag) + .when().delete(API + "origin-request-policy/" + id) + .then().statusCode(204); + } + } + } + + private static String cachePolicyBody(String name, String comment, String defaultTtl, String cookie) { + return """ + + %s%s + 10%s86400 + + true + true + whitelist + 1Accept-Language + + whitelist + 2%slocale + + whitelist + 1page + + + + """.formatted(name, comment, defaultTtl, cookie); + } + + private static String originRequestPolicyBody(String name, String comment, String header) { + return """ + + %s%s + whitelist + 1%s + + whitelist + 1session + + whitelist + 1page + + + """.formatted(name, comment, header); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontPolicyConfigCodecTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontPolicyConfigCodecTest.java new file mode 100644 index 0000000000..f417693163 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontPolicyConfigCodecTest.java @@ -0,0 +1,163 @@ +package io.github.hectorvent.floci.services.cloudfront; + +import io.github.hectorvent.floci.core.common.XmlBuilder; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CloudFrontPolicyConfigCodecTest { + + @Test + void cachePolicyConfigRoundTripsAllTtlsAndCacheKeyMembers() { + String body = """ + + complete-cache-policy + all fields + 10 + 3600 + 86400 + + true + true + + whitelist + 2 + Accept-LanguageCloudFront-Viewer-Country + + + + whitelist + 2 + sessionlocale + + + + whitelist + 2 + pagesort + + + + + """; + + Map config = CloudFrontPolicyConfigCodec.parseCachePolicy(body); + assertEquals(Map.of( + "MinTTL", "10", + "DefaultTTL", "3600", + "MaxTTL", "86400", + "ParametersInCacheKeyAndForwardedToOrigin", Map.of( + "EnableAcceptEncodingGzip", "true", + "EnableAcceptEncodingBrotli", "true", + "HeadersConfig", Map.of( + "HeaderBehavior", "whitelist", + "Headers", List.of("Accept-Language", "CloudFront-Viewer-Country")), + "CookiesConfig", Map.of( + "CookieBehavior", "whitelist", + "Cookies", List.of("session", "locale")), + "QueryStringsConfig", Map.of( + "QueryStringBehavior", "whitelist", + "QueryStrings", List.of("page", "sort")))), + config); + + XmlBuilder xml = new XmlBuilder().start("CachePolicyConfig"); + CloudFrontPolicyConfigCodec.serializeCachePolicy(xml, config); + Map reparsed = CloudFrontPolicyConfigCodec.parseCachePolicy( + xml.end("CachePolicyConfig").build()); + assertEquals(config, reparsed); + } + + @Test + void originRequestPolicyConfigRoundTripsAllForwardedMembers() { + String body = """ + + complete-origin-policy + + whitelist + 1Origin + + + whitelist + 1session + + + whitelist + 1page + + + """; + + Map config = CloudFrontPolicyConfigCodec.parseOriginRequestPolicy(body); + assertEquals(Map.of( + "HeadersConfig", Map.of( + "HeaderBehavior", "whitelist", "Headers", List.of("Origin")), + "CookiesConfig", Map.of( + "CookieBehavior", "whitelist", "Cookies", List.of("session")), + "QueryStringsConfig", Map.of( + "QueryStringBehavior", "whitelist", "QueryStrings", List.of("page"))), + config); + + XmlBuilder xml = new XmlBuilder().start("OriginRequestPolicyConfig"); + CloudFrontPolicyConfigCodec.serializeOriginRequestPolicy(xml, config); + Map reparsed = CloudFrontPolicyConfigCodec.parseOriginRequestPolicy( + xml.end("OriginRequestPolicyConfig").build()); + assertEquals(config, reparsed); + } + + @Test + void cachePolicyConfigAppliesAwsTtlDefaults() { + Map standard = CloudFrontPolicyConfigCodec.parseCachePolicy(""" + 10 + """); + assertEquals("86400", standard.get("DefaultTTL")); + assertEquals("31536000", standard.get("MaxTTL")); + + Map raisedMinimum = CloudFrontPolicyConfigCodec.parseCachePolicy(""" + 40000000 + """); + assertEquals("40000000", raisedMinimum.get("DefaultTTL")); + assertEquals("40000000", raisedMinimum.get("MaxTTL")); + + Map raisedDefault = CloudFrontPolicyConfigCodec.parseCachePolicy(""" + 040000000 + """); + assertEquals("40000000", raisedDefault.get("MaxTTL")); + } + + @Test + void selectionParsingAcceptsOnlyNamesAndPreservesEmptyShapes() { + String body = """ + + none + whitelist + 0 + + whitelist + 1 + not-a-namepage + + + + """; + + Map config = CloudFrontPolicyConfigCodec.parseOriginRequestPolicy(body); + assertEquals(Map.of("HeaderBehavior", "none"), config.get("HeadersConfig")); + assertEquals(Map.of("CookieBehavior", "whitelist", "Cookies", List.of()), + config.get("CookiesConfig")); + assertEquals(Map.of("QueryStringBehavior", "whitelist", "QueryStrings", List.of("page")), + config.get("QueryStringsConfig")); + + XmlBuilder xml = new XmlBuilder().start("OriginRequestPolicyConfig"); + CloudFrontPolicyConfigCodec.serializeOriginRequestPolicy(xml, config); + String serialized = xml.end("OriginRequestPolicyConfig").build(); + assertTrue(serialized.contains("none")); + assertFalse(serialized.contains("")); + assertTrue(serialized.contains("0")); + assertFalse(serialized.contains("0")); + } +}