diff --git a/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchVersionManager.cs b/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchVersionManager.cs index 7322c328..124b1764 100644 --- a/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchVersionManager.cs +++ b/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchVersionManager.cs @@ -56,19 +56,18 @@ public Version EffectiveVersion { try { - var response = _client.Cat.Nodes(new CatNodesRequestParameters() - { - Headers = new[] { "v" } - }); + var response = _client.DoRequest(HttpMethod.GET, "/"); if (!response.Success) return null; - var discoveredVersion = response.Body.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) - .FirstOrDefault(); + var discoveredVersion = response.Dictionary["version"]["number"]; - if (discoveredVersion == null) + if (!discoveredVersion.HasValue) return null; - return new Version(discoveredVersion); + if (discoveredVersion.Value is not string strVersion) + return null; + + return new Version(strVersion); } catch (Exception ex) diff --git a/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTests.cs b/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTests.cs index 785026c4..ce940659 100644 --- a/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTests.cs +++ b/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTests.cs @@ -1,4 +1,5 @@ using Elasticsearch.Net; +using Serilog.Sinks.Elasticsearch.Tests.Stubs; using System.Text; using Xunit; @@ -18,7 +19,7 @@ public void Ctor_DetectElasticsearchVersionSetToTrue_SetsTypeName(string elastic /* ARRANGE */ var options = new ElasticsearchSinkOptions { - Connection = FakeResponse(elasticVersion), + Connection = FakeProductCheckResponse(elasticVersion), TypeName = configuredTypeName }; @@ -41,7 +42,7 @@ public void Ctor_DetectElasticsearchVersionSetToFalseAssumesVersion7_SetsTypeNam /* ARRANGE */ var options = new ElasticsearchSinkOptions { - Connection = FakeResponse(elasticVersion), + Connection = FakeProductCheckResponse(elasticVersion), DetectElasticsearchVersion = false, TypeName = configuredTypeName }; @@ -65,7 +66,7 @@ public void CreateLogger_DetectElasticsearchVersionSetToTrue_SetsTypeName(string /* ARRANGE */ var options = new ElasticsearchSinkOptions { - Connection = FakeResponse(elasticVersion), + Connection = FakeProductCheckResponse(elasticVersion), DetectElasticsearchVersion = true, TypeName = configuredTypeName }; @@ -83,10 +84,10 @@ public void CreateLogger_DetectElasticsearchVersionSetToTrue_SetsTypeName(string Assert.Equal(expectedTypeName, options.TypeName); } - private static IConnection FakeResponse(string responseText) + private static IConnection FakeProductCheckResponse(string responseText) { - byte[] responseBody = Encoding.UTF8.GetBytes(responseText); - return new InMemoryConnection(responseBody, contentType: "text/plain; charset=UTF-8"); + var productCheckResponse = ConnectionStub.ModifiedProductCheckResponse(responseText); + return new InMemoryConnection(productCheckResponse); } } } diff --git a/test/Serilog.Sinks.Elasticsearch.Tests/Stubs/ConnectionStub.cs b/test/Serilog.Sinks.Elasticsearch.Tests/Stubs/ConnectionStub.cs index c1ae51e9..fdc0fadf 100644 --- a/test/Serilog.Sinks.Elasticsearch.Tests/Stubs/ConnectionStub.cs +++ b/test/Serilog.Sinks.Elasticsearch.Tests/Stubs/ConnectionStub.cs @@ -53,7 +53,7 @@ public override TReturn Request(RequestData requestData) $" to be productCheck pre-flight request"); _productCheckDone = true; - return ReturnConnectionStatus(requestData); // root page returned + return ReturnConnectionStatus(requestData); // hard-coded root page returned } byte[] responseBytes = Array.Empty(); @@ -66,6 +66,7 @@ public override TReturn Request(RequestData requestData) int responseStatusCode = 200; string contentType = null; + InMemoryHttpResponse productCheckResponse = null; switch (requestData.Method) { @@ -79,13 +80,10 @@ public override TReturn Request(RequestData requestData) switch (requestData.Uri.PathAndQuery.ToLower()) { case "/": - // ReturnConnectionStatus(...) call at the bottom will return dummy product page - // when root "/" is requested. + productCheckResponse = ModifiedProductCheckResponse(_productVersion); break; - case "/_cat/nodes": case "/_cat/nodes?h=v": responseBytes = Encoding.UTF8.GetBytes(_productVersion); - responseStatusCode = 200; contentType = "text/plain; charset=UTF-8"; break; } @@ -100,12 +98,29 @@ public override TReturn Request(RequestData requestData) break; } - return ReturnConnectionStatus(requestData, responseBytes, responseStatusCode, contentType); + return ReturnConnectionStatus(requestData, productCheckResponse, responseBytes, responseStatusCode, contentType); } public override Task RequestAsync(RequestData requestData, CancellationToken cancellationToken) { return Task.FromResult(Request(requestData)); } + + public static InMemoryHttpResponse ModifiedProductCheckResponse(string productVersion) + { + var productCheckResponse = ValidProductCheckResponse(); + if (productVersion is not null) + { + using var originalMemoryStream = new MemoryStream(productCheckResponse.ResponseBytes, false); + { + var json = LowLevelRequestResponseSerializer.Instance.Deserialize(originalMemoryStream); + json["version"]["number"] = productVersion; + using var modifiedMemoryStream = new MemoryStream(); + LowLevelRequestResponseSerializer.Instance.Serialize(json, modifiedMemoryStream); + productCheckResponse.ResponseBytes = modifiedMemoryStream.ToArray(); + } + } + return productCheckResponse; + } } } \ No newline at end of file diff --git a/test/Serilog.Sinks.Elasticsearch.Tests/Templating/DiscoverVersionTests.cs b/test/Serilog.Sinks.Elasticsearch.Tests/Templating/DiscoverVersionTests.cs index d922a3e7..9e4b3395 100644 --- a/test/Serilog.Sinks.Elasticsearch.Tests/Templating/DiscoverVersionTests.cs +++ b/test/Serilog.Sinks.Elasticsearch.Tests/Templating/DiscoverVersionTests.cs @@ -34,7 +34,7 @@ public DiscoverVersionTests() public void TemplatePutToCorrectUrl() { var uri = _templateGet.Item1; - uri.AbsolutePath.Should().Be("/_cat/nodes"); + uri.AbsolutePath.Should().Be("/"); } } } \ No newline at end of file