-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for PATCH in HttpConnection.ConvertHttpMethod
Signed-off-by: Assaf Tirangel <[email protected]>
- Loading branch information
Assaf Tirangel
authored and
atirangel
committed
Jan 8, 2024
1 parent
a9f9eb4
commit e2ad643
Showing
3 changed files
with
77 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Net.Http; | ||
using FluentAssertions; | ||
using OpenSearch.Client; | ||
using OpenSearch.Net; | ||
using OpenSearch.OpenSearch.Xunit.XunitPlumbing; | ||
using Xunit; | ||
using HttpMethod = OpenSearch.Net.HttpMethod; | ||
|
||
namespace Tests.Connection.Http; | ||
|
||
public class HttpConnectionTests | ||
{ | ||
private static readonly System.Net.Http.HttpMethod Patch = new System.Net.Http.HttpMethod("PATCH"); | ||
|
||
private class MockHttpConnection : HttpConnection | ||
{ | ||
public HttpRequestMessage LastRequest { get; private set; } | ||
|
||
protected override HttpRequestMessage CreateHttpRequestMessage(RequestData requestData) | ||
{ | ||
LastRequest = base.CreateHttpRequestMessage(requestData); | ||
return LastRequest; | ||
} | ||
|
||
public override TResponse Request<TResponse>(RequestData requestData) | ||
{ | ||
CreateHttpRequestMessage(requestData); | ||
return new TResponse(); | ||
} | ||
} | ||
|
||
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) | ||
{ | ||
var mockHttpConnection = new MockHttpConnection(); | ||
mockHttpConnection.Request<StringResponse>(new RequestData(method, "", null, null, null, null)); | ||
mockHttpConnection.LastRequest.Method.Should().Be(ConvertHttpMethod(method)); | ||
} | ||
|
||
private static System.Net.Http.HttpMethod ConvertHttpMethod(HttpMethod httpMethod) => | ||
httpMethod switch | ||
{ | ||
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)) | ||
}; | ||
|
||
} |