diff --git a/compatibility-tests/sdk-test-python/tests/test_appconfig_cached_configuration.py b/compatibility-tests/sdk-test-python/tests/test_appconfig_cached_configuration.py new file mode 100644 index 0000000000..4225e14458 --- /dev/null +++ b/compatibility-tests/sdk-test-python/tests/test_appconfig_cached_configuration.py @@ -0,0 +1,79 @@ +"""AppConfig distinguishes a cached version from newly delivered empty content.""" + +from contextlib import ExitStack + +import boto3 +import pytest + + +@pytest.mark.parametrize("by_name", [False, True], ids=["ids", "names"]) +@pytest.mark.parametrize( + "content,content_type", + [ + (b'{"enabled":true}', "application/json"), + (b"\x00\xffconfiguration\x80", "application/octet-stream"), + (b"", "application/octet-stream"), + ], + ids=["json", "binary", "empty"], +) +def test_cached_configuration_response(aws_config, client_config, unique_name, + by_name, content, content_type): + with ExitStack() as cleanup: + client = boto3.client("appconfig", config=client_config, **aws_config) + cleanup.callback(client.close) + app = client.create_application(Name=unique_name)["Id"] + cleanup.callback(client.delete_application, ApplicationId=app) + profile = client.create_configuration_profile( + ApplicationId=app, Name="settings", LocationUri="hosted", Type="AWS.Freeform" + )["Id"] + cleanup.callback( + client.delete_configuration_profile, + ApplicationId=app, ConfigurationProfileId=profile, + ) + env = client.create_environment(ApplicationId=app, Name="local")["Id"] + cleanup.callback(client.delete_environment, ApplicationId=app, EnvironmentId=env) + version = client.create_hosted_configuration_version( + ApplicationId=app, ConfigurationProfileId=profile, + Content=content, ContentType=content_type, + )["VersionNumber"] + client.start_deployment( + ApplicationId=app, EnvironmentId=env, ConfigurationProfileId=profile, + ConfigurationVersion=str(version), DeploymentStrategyId="AppConfig.AllAtOnce", + ) + request = { + "Application": unique_name if by_name else app, + "Environment": "local" if by_name else env, + "Configuration": "settings" if by_name else profile, + "ClientId": unique_name, + } + + for hint in (None, str(version), "unknown"): + response = client.get_configuration( + **request, **({"ClientConfigurationVersion": hint} if hint is not None else {}) + ) + cleanup.callback(response["Content"].close) + cached = hint == str(version) + assert response["ResponseMetadata"]["HTTPStatusCode"] == (204 if cached else 200) + assert response["ConfigurationVersion"] == str(version) + assert response["Content"].read() == (b"" if cached else content) + headers = response["ResponseMetadata"]["HTTPHeaders"] + if cached: + assert "ContentType" not in response + assert "content-type" not in headers and "content-length" not in headers + else: + assert response["ContentType"] == content_type + + next_version = client.create_hosted_configuration_version( + ApplicationId=app, ConfigurationProfileId=profile, + Content=b"changed", ContentType="text/plain", + )["VersionNumber"] + client.start_deployment( + ApplicationId=app, EnvironmentId=env, ConfigurationProfileId=profile, + ConfigurationVersion=str(next_version), DeploymentStrategyId="AppConfig.AllAtOnce", + ) + response = client.get_configuration(**request, ClientConfigurationVersion=str(version)) + cleanup.callback(response["Content"].close) + assert response["ResponseMetadata"]["HTTPStatusCode"] == 200 + assert response["ConfigurationVersion"] == str(next_version) + assert response["ContentType"] == "text/plain" + assert response["Content"].read() == b"changed" diff --git a/docs/services/appconfig.md b/docs/services/appconfig.md index 76a41c1376..81561c1472 100644 --- a/docs/services/appconfig.md +++ b/docs/services/appconfig.md @@ -35,7 +35,9 @@ The management plane allows you to create and manage applications, environments, `GetConfiguration` accepts application, environment and configuration profile names or IDs, and requires `client_id`. It returns the deployed version's content with its `Content-Type` and `Configuration-Version`; feature flags are resolved as on the [data plane](#data-plane), including Ion for a caller that accepts it. -A `client_configuration_version` equal to the deployed version returns HTTP 200 with an empty body. +A `client_configuration_version` equal to the deployed version returns HTTP 204 with an empty body +and `Configuration-Version`, without `Content-Type` or `Content-Length`. An empty configuration +that the caller has not received still returns HTTP 200 and its stored content type. A missing or undeployed environment returns `BadRequestException`; an unknown application or configuration profile returns `ResourceNotFoundException`. Only hosted configurations are served: a deployed version with no hosted configuration version behind it, diff --git a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigController.java b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigController.java index 351d29176d..9fc2f9d1f6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigController.java +++ b/src/main/java/io/github/hectorvent/floci/services/appconfig/AppConfigController.java @@ -293,6 +293,11 @@ public class AppConfigController { String clientVersion = uriInfo.getQueryParameters().getFirst("client_configuration_version"); LegacyConfiguration data = dataService.getConfiguration( appId, envId, configuration, clientId, clientVersion, accept); + if (data.configurationVersion().equals(clientVersion)) { + return Response.noContent() + .header("Configuration-Version", data.configurationVersion()) + .build(); + } // A version stored without a content type would otherwise go out as the */* of @Produces. String contentType = data.contentType() != null ? data.contentType() : MediaType.APPLICATION_OCTET_STREAM; return Response.ok(data.content()) diff --git a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigLegacyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigLegacyIntegrationTest.java index 465c67d5a5..fe4a2b72de 100644 --- a/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigLegacyIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/appconfig/AppConfigLegacyIntegrationTest.java @@ -127,9 +127,13 @@ class AppConfigLegacyIntegrationTest { } @Test - void matchingClientVersionReturnsAnEmptyBody() { + void matchingClientVersionReturnsNoContentWithoutEntityHeaders() { given().queryParam("client_id", "reader").queryParam("client_configuration_version", "1") - .get(path()).then().statusCode(200).header("Configuration-Version", "1").body(equalTo("")); + .get(path()).then().statusCode(204) + .header("Configuration-Version", "1") + .header("Content-Type", nullValue()) + .header("Content-Length", nullValue()) + .body(equalTo("")); given().queryParam("client_id", "reader").queryParam("client_configuration_version", "0") .get(path()).then().statusCode(200).body("enabled", equalTo(true)); given().queryParam("client_id", "another-reader").get(path())