fix(cloudfront): dedupe CacheBehavior XML parsing (#3705)

DefaultCacheBehavior and CacheBehavior each had their own ~250-line
hand-written XML-parsing state machine for the same element shape,
found via a CPD pass (#3652). Extracted the shared logic into one
parseCacheBehaviorScope helper, used by both parsing call sites, with
a small CacheBehaviorSettings interface so the helper can populate
either model type.

The duplication had already caused the exact bug it risked: the
CacheBehavior copy never parsed RealtimeLogConfigArn, even though the
field exists on both types in AWS's real CloudFront wire model and on
the CacheBehavior model class itself. The response XML builders for
both types also never emitted it. Fixed both and added a regression
test.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-15 22:30:49 -04:00
1 parent b79bf4f7b2
commit 3641a074dc
5 files changed
+344 -568

No files matched your search

@@ -285,6 +285,45 @@ class CloudFrontControllerTest {
}
}
@Test
void orderedCacheBehaviorRoundTripsRealtimeLogConfigArn() {
// A hand-written parser copy-pasted for CacheBehavior once silently dropped
// RealtimeLogConfigArn, since only the DefaultCacheBehavior copy captured it.
CloudFrontService service = mock(CloudFrontService.class);
CloudFrontController controller = new CloudFrontController(service);
String body = distributionConfigBody("").replace("</DefaultCacheBehavior>", """
</DefaultCacheBehavior>
<CacheBehaviors><Quantity>1</Quantity><Items><CacheBehavior>
<PathPattern>/api/*</PathPattern>
<TargetOriginId>o1</TargetOriginId>
<ViewerProtocolPolicy>https-only</ViewerProtocolPolicy>
<RealtimeLogConfigArn>arn:aws:cloudfront::000000000000:realtime-log-config/rtlc-1</RealtimeLogConfigArn>
</CacheBehavior></Items></CacheBehaviors>
""");
ArgumentCaptor<Distribution> captor = ArgumentCaptor.forClass(Distribution.class);
when(service.createDistribution(captor.capture(), any())).thenAnswer(inv -> {
Distribution d = inv.getArgument(0);
d.setId("dist-3");
d.setEtag("etag-3");
return d;
});
try (Response created = controller.createDistribution(null, body)) {
assertEquals(201, created.getStatus());
}
when(service.getDistribution("dist-3")).thenReturn(captor.getValue());
try (Response cfg = controller.getDistributionConfig("dist-3")) {
String xml = (String) cfg.getEntity();
int start = xml.indexOf("<CacheBehaviors>");
String cacheBehaviorsBlock = xml.substring(start);
assertEquals(
"arn:aws:cloudfront::000000000000:realtime-log-config/rtlc-1",
XmlParser.extractFirst(cacheBehaviorsBlock, "RealtimeLogConfigArn", null));
}
}
@Test
void invalidLambdaFunctionAssociationEventTypeIsRejected() {
CloudFrontService service = mock(CloudFrontService.class);