Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Consider an empty string header value to be present #832

Closed
wants to merge 5 commits into from

Conversation

jbelkins
Copy link
Contributor

@jbelkins jbelkins commented Sep 24, 2024

Issue #

Description of changes

An empty string is an acceptable value for a HTTP header, and SDKs should send an empty string value for a header when one is passed.

Swift implements this behavior correctly, but incorrectly validates "forbidden headers" in a protocol test by considering a HTTP header to "not exist" when its value is an empty string.

This PR fixes the exists() method on the Headers type so that it returns true rather than false when the header being checked has an empty string as its value.

Note that the current NullAndEmptyHeaders protocol tests (in RestJSON & RestXML) for this behavior are incorrect, and this PR will cause the current version of these tests to fail. These tests are being corrected in a future version of Smithy and this PR should be merged only when that Smithy version is adopted.

Also:

  • Create a new SmithyHTTPAPITests test target and move the Headers tests into it from ClientRuntimeTests.

Scope

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

}

return !value.isEmpty
headers.index(of: name) != nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous definition of exists was wrong because it considered a header with an empty string for its value to "not exist". Turns out a header with empty string value is valid, so it should definitely "exist".

I suspect that the reason for this logic is that the previous developers changed the definition of "exists" to suit the behavior demanded by empty-header protocol tests, which is wrong and is being fixed by Smithy team.

Package.swift Outdated
.testTarget(
name: "SmithyHTTPAPITests",
dependencies: ["SmithyHTTPAPI"]
),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new test target & moved the Headers tests into it, since Headers is in the SmithyHTTPAPI module.

func test_exists_trueIfHeaderValueIsEmptyString() {
let subject = Headers(["a": ""])
XCTAssertTrue(subject.exists(name: "a"))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new tests check the expected behavior for the exists() method on Headers.

@jbelkins
Copy link
Contributor Author

This PR is failing the downstream CI builds because the NullAndEmptyHeaders tests for RestJSON & RestXML are failing - this is expected because those tests have incorrect expectations for empty-string headers.

This PR should merge when this PR ships in a new version of Smithy:
smithy-lang/smithy#2403

@jbelkins
Copy link
Contributor Author

Closing this PR because this change is being made in #843 (update to Smithy 1.52) instead.

@jbelkins jbelkins closed this Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant