fix(cloudfront): stop duplicating CachedMethods into AllowedMethods.Items (#3217)

DefaultCacheBehavior/CacheBehavior parsing tracked a single inAllowedMethods
flag across the whole <AllowedMethods> element, so <Method> entries nested
inside the sibling <CachedMethods> sub-object were folded into the parsed
AllowedMethods list too (and CachedMethods itself was silently dropped).
Since terraform-provider-aws's aws_cloudfront_distribution requires both
allowed_methods and cached_methods on every cache behavior, this produced a
permanent per-plan diff: AllowedMethods.Items came back duplicated
(["GET","HEAD","GET","HEAD","OPTIONS"]) and CachedMethods was missing
entirely.

Scope one of the two follow-up fidelity gaps flagged (but deliberately left
unfixed) by #3015, which fixed the TrustedSigners crash from #2767. The
other gap from that issue (ForwardedValues/MinTTL/DefaultTTL/MaxTTL dropped
on read-back for the legacy forwarded_values cache-behavior path) is a
separate, larger change, filed as #3208.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-07 20:28:44 -05:00
1 parent 3af6f47bf8
commit 9e9b0b6e91
2 files changed
+103 -14

No files matched your search

@@ -191,6 +191,56 @@ class CloudFrontControllerTest {
}
}
@Test
void defaultCacheBehaviorEchoesCachedMethodsWithoutDuplicatingAllowedMethods() {
CloudFrontService service = mock(CloudFrontService.class);
CloudFrontController controller = new CloudFrontController(service);
// AWS nests CachedMethods inside AllowedMethods. A parser that doesn't scope Method
// elements to the right block folds the CachedMethods items into AllowedMethods too,
// producing duplicates (["GET","HEAD","GET","HEAD","OPTIONS"]) and dropping CachedMethods.
String body = distributionConfigBody("").replace(
"""
<AllowedMethods><Quantity>2</Quantity><Items>
<Method>GET</Method><Method>HEAD</Method></Items></AllowedMethods>""",
"""
<AllowedMethods><Quantity>3</Quantity><Items>
<Method>GET</Method><Method>HEAD</Method><Method>OPTIONS</Method></Items>
<CachedMethods><Quantity>2</Quantity><Items>
<Method>GET</Method><Method>HEAD</Method></Items></CachedMethods>
</AllowedMethods>""");
ArgumentCaptor<Distribution> captor = ArgumentCaptor.forClass(Distribution.class);
when(service.createDistribution(captor.capture(), any())).thenAnswer(inv -> {
Distribution d = inv.getArgument(0);
d.setId("dist-cm");
d.setEtag("etag-cm");
return d;
});
try (Response created = controller.createDistribution(null, body)) {
assertEquals(201, created.getStatus());
}
when(service.getDistribution("dist-cm")).thenReturn(captor.getValue());
try (Response dist = controller.getDistribution("dist-cm")) {
String xml = (String) dist.getEntity();
int start = xml.indexOf("<AllowedMethods>");
int end = xml.indexOf("</AllowedMethods>") + "</AllowedMethods>".length();
String block = xml.substring(start, end);
int cachedStart = block.indexOf("<CachedMethods>");
assertTrue(cachedStart >= 0, "AllowedMethods must echo a nested CachedMethods object");
String outer = block.substring(0, cachedStart);
String cached = block.substring(cachedStart);
assertEquals(List.of("GET", "HEAD", "OPTIONS"), XmlParser.extractAll(outer, "Method"),
"AllowedMethods.Items must not include CachedMethods entries");
assertEquals(List.of("GET", "HEAD"), XmlParser.extractAll(cached, "Method"));
}
}
@Test
void orderedCacheBehaviorRoundTripsLambdaFunctionAssociations() {
CloudFrontService service = mock(CloudFrontService.class);