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

Add support for PATCH in HttpConnection.ConvertHttpMethod #489

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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Removed the `Features` API which is not supported by OpenSearch from the low-level client ([#331](https://github.com/opensearch-project/opensearch-net/pull/331))
- Removed the deprecated low-level `IndexTemplateV2` APIs in favour of the `ComposableIndexTemplate` APIs ([#437](https://github.com/opensearch-project/opensearch-net/pull/437))

### Fixed
- Fix `HttpConnection.ConvertHttpMethod` to support `Patch` method ([#489](https://github.com/opensearch-project/opensearch-net/pull/489))

### Dependencies
- Bumps `Microsoft.CodeAnalysis.CSharp` from 4.2.0 to 4.6.0
- Bumps `NSwag.Core.Yaml` from 13.19.0 to 13.20.0
Expand Down Expand Up @@ -130,4 +133,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
[1.6.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.5.0...v1.6.0
[1.5.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.4.0...v1.5.0
[1.4.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.3.0...v1.4.0
[1.3.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.2.0...v1.3.0
[1.3.0]: https://github.com/opensearch-project/opensearch-net/compare/v1.2.0...v1.3.0
25 changes: 12 additions & 13 deletions src/OpenSearch.Net/Connection/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
Expand Down Expand Up @@ -70,6 +69,8 @@ public class HttpConnection : IConnection
+ $" please set {nameof(ConnectionConfiguration.ConnectionLimit)} to -1 on your connection configuration/settings."
+ $" this will cause the {nameof(HttpClientHandler.MaxConnectionsPerServer)} not to be set on {nameof(HttpClientHandler)}";

private static readonly System.Net.Http.HttpMethod Patch = new System.Net.Http.HttpMethod("PATCH");

private RequestDataHttpClientFactory HttpClientFactory { get; }

public int InUseHandlers => HttpClientFactory.InUseHandlers;
Expand Down Expand Up @@ -430,19 +431,17 @@ private static async Task SetContentAsync(HttpRequestMessage message, RequestDat
}
}

private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMethod)
{
switch (httpMethod)
private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMethod) =>
httpMethod switch
{
case HttpMethod.GET: return System.Net.Http.HttpMethod.Get;
case HttpMethod.POST: return System.Net.Http.HttpMethod.Post;
case HttpMethod.PUT: return System.Net.Http.HttpMethod.Put;
case HttpMethod.DELETE: return System.Net.Http.HttpMethod.Delete;
case HttpMethod.HEAD: return System.Net.Http.HttpMethod.Head;
default:
throw new ArgumentException("Invalid value for HttpMethod", nameof(httpMethod));
}
}
HttpMethod.GET => System.Net.Http.HttpMethod.Get,
HttpMethod.POST => System.Net.Http.HttpMethod.Post,
HttpMethod.PUT => System.Net.Http.HttpMethod.Put,
HttpMethod.DELETE => System.Net.Http.HttpMethod.Delete,
HttpMethod.HEAD => System.Net.Http.HttpMethod.Head,
HttpMethod.PATCH => Patch,
_ => throw new ArgumentException("Invalid value for HttpMethod", nameof(httpMethod))
};

internal static int GetClientKey(RequestData requestData)
{
Expand Down
1 change: 1 addition & 0 deletions tests/Tests.Auth.AwsSigV4/AwsSigV4HttpConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using OpenSearch.Client;
using OpenSearch.OpenSearch.Xunit.XunitPlumbing;
using Tests.Auth.AwsSigV4.Utils;
using Tests.Core.Connection.Http;
using Xunit;

namespace Tests.Auth.AwsSigV4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Amazon.Runtime;
using OpenSearch.Net;
using OpenSearch.Net.Auth.AwsSigV4;
using Tests.Core.Connection.Http;

namespace Tests.Auth.AwsSigV4.Utils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
using System.Net.Http;
using FluentAssertions;

namespace Tests.Auth.AwsSigV4.Utils;
namespace Tests.Core.Connection.Http;

internal static class HttpRequestMessageAssertions
public static class HttpRequestMessageAssertions
{
public static void ShouldHaveMethod(this HttpRequestMessage request, string method) =>
request.Method.Should().Be(new HttpMethod(method));

public static void ShouldHaveHeader(this HttpRequestMessage request, string name, string value) =>
request.Headers.GetValues(name).Should().BeEquivalentTo(value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
using System.Threading;
using System.Threading.Tasks;

namespace Tests.Auth.AwsSigV4.Utils;
namespace Tests.Core.Connection.Http;

internal class MockHttpMessageHandler : HttpMessageHandler
public class MockHttpMessageHandler : HttpMessageHandler
{
public delegate HttpResponseMessage Handler(HttpRequestMessage request);

Expand Down
44 changes: 44 additions & 0 deletions tests/Tests/Connection/Http/HttpConnectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System;
assaftir marked this conversation as resolved.
Show resolved Hide resolved
using System.Net.Http;
using FluentAssertions;
using OpenSearch.Net;
using OpenSearch.OpenSearch.Xunit.XunitPlumbing;
using Tests.Core.Connection.Http;
using Xunit;
using HttpMethod = OpenSearch.Net.HttpMethod;

namespace Tests.Connection.Http;

public class HttpConnectionTests
{
public static TheoryData<HttpMethod> HttpMethods()
{
var data = new TheoryData<HttpMethod>();
foreach (var httpMethod in Enum.GetValues<HttpMethod>()) data.Add(httpMethod);
return data;
}

[TU]
[MemberData(nameof(HttpMethods))]
public void UsesCorrectHttpMethod(HttpMethod method)
{
HttpRequestMessage sentRequest = null;
var connection = new MockableHttpConnection(r =>
{
sentRequest = r;
return new HttpResponseMessage();
});
var client = new OpenSearchLowLevelClient(new ConnectionConfiguration(connection: connection));

client.DoRequest<VoidResponse>(method, "/");

sentRequest.ShouldHaveMethod(method.ToString());
}
}
22 changes: 22 additions & 0 deletions tests/Tests/Connection/Http/MockableHttpConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System.Net.Http;
using OpenSearch.Net;
using Tests.Core.Connection.Http;

namespace Tests.Connection.Http;

public class MockableHttpConnection : HttpConnection
{
private readonly MockHttpMessageHandler _handler;

public MockableHttpConnection(MockHttpMessageHandler.Handler handler) =>
_handler = new MockHttpMessageHandler(handler);

protected override HttpMessageHandler CreateHttpClientHandler(RequestData requestData) => _handler;
}
Loading