Skip to content

Commit

Permalink
Merge pull request #93 from edouardpoitras/patch
Browse files Browse the repository at this point in the history
Only allow one value on certain headers
  • Loading branch information
andrueastman committed Jun 13, 2023
2 parents efd1326 + 4dd558d commit 7c0871c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

## [1.1.3] - 2023-06-13

### Fixed

- Fixed a bug that would allow multiple "Content-Type", "Content-Length", and "Content-Location" header values.

## [1.1.2] - 2023-05-17

### Changed
Expand Down Expand Up @@ -65,7 +71,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Adds support for nullable reference types
- Adds support for nullable reference types

## [1.0.0-rc.3] - 2023-01-09

Expand Down
7 changes: 7 additions & 0 deletions Microsoft.Kiota.Abstractions.Tests/RequestHeadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public class RequestHeadersTests {
Assert.Equal(new [] { "value", "value2" }, instance["name"]);
}
[Fact]
public void AddsSingleValueHeaderToExistent() {
var instance = new RequestHeaders();
instance.Add("Content-Type", "value");
instance.Add("Content-Type", "value2");
Assert.Equal(new[] { "value2" }, instance["Content-Type"]);
}
[Fact]
public void RemovesValue() {
var instance = new RequestHeaders();
instance.Remove("name", "value");
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://microsoft.github.io/kiota/</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.1.2</VersionPrefix>
<VersionPrefix>1.1.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SignAssembly>false</SignAssembly>
Expand Down
9 changes: 8 additions & 1 deletion src/RequestHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace Microsoft.Kiota.Abstractions;
/// <summary>Represents a collection of request headers.</summary>
public class RequestHeaders : IDictionary<string,IEnumerable<string>> {
private readonly Dictionary<string, HashSet<string>> _headers = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
private readonly HashSet<string> _singleValueHeaders = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"Content-Type",
"Content-Encoding",
"Content-Length"
};
/// <summary>
/// Adds values to the header with the specified name.
/// </summary>
Expand All @@ -20,7 +25,9 @@ public class RequestHeaders : IDictionary<string,IEnumerable<string>> {
throw new ArgumentNullException(nameof(headerValues));
if(!headerValues.Any())
return;
if(_headers.TryGetValue(headerName, out var values))
if(_singleValueHeaders.Contains(headerName))
_headers[headerName] = new HashSet<string> { headerValues[0] };
else if(_headers.TryGetValue(headerName, out var values))
foreach(var headerValue in headerValues)
values.Add(headerValue);
else
Expand Down

0 comments on commit 7c0871c

Please sign in to comment.