Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Jan 9, 2024
1 parent 1650a04 commit ddb212c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
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
38 changes: 15 additions & 23 deletions tests/Tests/Connection/Http/HttpConnectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/* 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.
*/
*
* 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;
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;

Expand All @@ -28,25 +29,16 @@ public static TheoryData<HttpMethod> HttpMethods()
[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(new System.Net.Http.HttpMethod(method.ToString()));
}

private class MockHttpConnection : HttpConnection
{
public HttpRequestMessage LastRequest { get; private set; }

protected override HttpRequestMessage CreateHttpRequestMessage(RequestData requestData)
HttpRequestMessage sentRequest = null;
var connection = new MockableHttpConnection(r =>
{
LastRequest = base.CreateHttpRequestMessage(requestData);
return LastRequest;
}
sentRequest = r;
return new HttpResponseMessage();
});
var client = new OpenSearchLowLevelClient(new ConnectionConfiguration(connection: connection));

public override TResponse Request<TResponse>(RequestData requestData)
{
CreateHttpRequestMessage(requestData);
return new TResponse();
}
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;
}

0 comments on commit ddb212c

Please sign in to comment.