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

.Net: Handle media types with parameters #10000

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,15 @@ private async Task<RestApiOperationResponse> ReadContentAndCreateOperationRespon
mediaType = mediaTypeFallback;
}

if (!this._payloadFactoryByMediaType.TryGetValue(mediaType!, out var payloadFactory))
// Remove media type parameters, such as x-api-version, from the "text/plain; x-api-version=2.0" media type string.
mediaType = mediaType!.Split(';').First();
SergeyMenshykh marked this conversation as resolved.
Show resolved Hide resolved

// Normalize the media type to lowercase and remove trailing whitespaces.
#pragma warning disable CA1308 // Normalize strings to uppercase
mediaType = mediaType!.ToLowerInvariant().Trim();
#pragma warning restore CA1308 // Normalize strings to uppercase

if (!this._payloadFactoryByMediaType.TryGetValue(mediaType, out var payloadFactory))
SergeyMenshykh marked this conversation as resolved.
Show resolved Hide resolved
{
throw new KernelException($"The media type {mediaType} of the {operation.Id} operation is not supported by {nameof(RestApiOperationRunner)}.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,44 @@ public async Task ItShouldReturnExpectedSchemaAsync(string expectedStatusCode, p
Assert.Equal(JsonSerializer.Serialize(expected), JsonSerializer.Serialize(result.ExpectedSchema));
}

[Theory]
[InlineData("application/json;x-api-version=2.0", "application/json")]
[InlineData("application/json ; x-api-version=2.0", "application/json")]
[InlineData(" application/JSON; x-api-version=2.0", "application/json")]
[InlineData(" TEXT/PLAIN ; x-api-version=2.0", "text/plain")]
public async Task ItShouldNormalizeContentTypeArgumentAsync(string actualContentType, string normalizedContentType)
{
// Arrange
this._httpMessageHandlerStub.ResponseToReturn.Content = new StringContent("fake-content", Encoding.UTF8, MediaTypeNames.Text.Plain);

var operation = new RestApiOperation(
id: "fake-id",
servers: [new RestApiServer("https://fake-random-test-host")],
path: "fake-path",
method: HttpMethod.Post,
description: "fake-description",
parameters: [],
responses: new Dictionary<string, RestApiExpectedResponse>(),
securityRequirements: [],
payload: null
);

var arguments = new KernelArguments
{
{ "payload", "fake-input-value" },
{ "content-type", actualContentType },
};

var sut = new RestApiOperationRunner(this._httpClient, this._authenticationHandlerMock.Object, enableDynamicPayload: false);

// Act
var result = await sut.RunAsync(operation, arguments);

// Assert
Assert.NotNull(this._httpMessageHandlerStub.ContentHeaders);
Assert.Contains(this._httpMessageHandlerStub.ContentHeaders, h => h.Key == "Content-Type" && h.Value.Any(h => h.StartsWith(normalizedContentType, StringComparison.InvariantCulture)));
}

/// <summary>
/// Disposes resources used by this class.
/// </summary>
Expand Down
Loading