-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose HTTP response headers in InfluxDBApiException (#118)
* feat: expose HTTP response headers in ApiException, refactor integration tests. * chore: cleaning up lint. * docs: add General example Runner and HttpErrorHandled example * chore: cleanup lint * docs: update Examples/README.md * chore: lint README.md * test: adds test of null message value in InfluxDBApiException * chore: refactor Headers and StatusCode to property * chore: add Examples/General to global solution * docs: update CHANGELOG.md * chore: change assert in integration write test
- Loading branch information
1 parent
7fed31d
commit fe8e214
Showing
13 changed files
with
378 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
|
||
using System; | ||
using System.Diagnostics; | ||
using NUnit.Framework; | ||
|
||
namespace InfluxDB3.Client.Test.Integration; | ||
|
||
public abstract class IntegrationTest | ||
{ | ||
private static readonly TraceListener ConsoleOutListener = new TextWriterTraceListener(Console.Out); | ||
|
||
protected string Host { get; private set; } = Environment.GetEnvironmentVariable("TESTING_INFLUXDB_URL") ?? | ||
throw new InvalidOperationException( | ||
"TESTING_INFLUXDB_URL environment variable is not set."); | ||
|
||
protected string Token { get; private set; } = Environment.GetEnvironmentVariable("TESTING_INFLUXDB_TOKEN") ?? | ||
throw new InvalidOperationException( | ||
"TESTING_INFLUXDB_TOKEN environment variable is not set."); | ||
|
||
protected string Database { get; private set; } = Environment.GetEnvironmentVariable("TESTING_INFLUXDB_DATABASE") ?? | ||
throw new InvalidOperationException( | ||
"TESTING_INFLUXDB_DATABASE environment variable is not set."); | ||
|
||
[OneTimeSetUp] | ||
public void OneTimeSetUp() | ||
{ | ||
if (!Trace.Listeners.Contains(ConsoleOutListener)) | ||
{ | ||
Console.SetOut(TestContext.Progress); | ||
Trace.Listeners.Add(ConsoleOutListener); | ||
} | ||
} | ||
|
||
[OneTimeTearDownAttribute] | ||
public void OneTimeTearDownAttribute() | ||
{ | ||
ConsoleOutListener.Dispose(); | ||
} | ||
} |
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,58 @@ | ||
using System; | ||
using System.Collections.Frozen; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using InfluxDB3.Client.Config; | ||
using NUnit.Framework; | ||
|
||
namespace InfluxDB3.Client.Test.Integration; | ||
|
||
public class WriteTest : IntegrationTest | ||
{ | ||
|
||
[Test] | ||
public async Task WriteWithError() | ||
{ | ||
using var client = new InfluxDBClient(new ClientConfig | ||
{ | ||
Host = Host, | ||
Token = Token, | ||
Database = Database, | ||
}); | ||
|
||
try | ||
{ | ||
await client.WriteRecordAsync("vehicle,id=vwbus vel=0.0,distance=,status=\"STOPPED\""); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (ex is InfluxDBApiException) | ||
{ | ||
var iaex = (InfluxDBApiException)ex; | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(iaex.Message, | ||
Contains.Substring("Found trailing content: 'distance=,status=\"STOPPED\"'")); | ||
Assert.That(iaex.StatusCode.ToString(), Is.EqualTo("BadRequest")); | ||
Assert.That(iaex.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.BadRequest)); | ||
}); | ||
var headersDix = iaex.Headers.ToFrozenDictionary(); | ||
Check warning on line 39 in Client.Test.Integration/WriteTest.cs GitHub Actions / CodeQL-Build
Check warning on line 39 in Client.Test.Integration/WriteTest.cs GitHub Actions / CodeQL-Build
|
||
Assert.DoesNotThrow(() => | ||
{ | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(headersDix["trace-id"].First(), Is.Not.Empty); | ||
Assert.That(headersDix["trace-sampled"].First(), Is.EqualTo("false")); | ||
Assert.That(headersDix["Strict-Transport-Security"].First(), Is.Not.Empty); | ||
Assert.That(headersDix["X-Influxdb-Request-ID"].First(), Is.Not.Empty); | ||
Assert.That(headersDix["X-Influxdb-Build"].First(), Is.EqualTo("Cloud")); | ||
}); | ||
}); | ||
} | ||
else | ||
{ | ||
Assert.Fail($"Should catch InfluxDBApiException, but received {ex.GetType()}: {ex.Message}."); | ||
} | ||
} | ||
} | ||
} |
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,86 @@ | ||
using System; | ||
using System.Collections.Frozen; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using WireMock.RequestBuilders; | ||
using WireMock.ResponseBuilders; | ||
|
||
namespace InfluxDB3.Client.Test; | ||
|
||
public class InfluxDBApiExceptionTest : MockServerTest | ||
{ | ||
|
||
private InfluxDBClient _client; | ||
|
||
[TearDown] | ||
public void TearDown() | ||
Check warning on line 17 in Client.Test/InfluxDBApiExceptionTest.cs GitHub Actions / CodeQL-Build
|
||
{ | ||
base.TearDown(); | ||
_client?.Dispose(); | ||
} | ||
|
||
[Test] | ||
public void NullValuesTest() | ||
{ | ||
var exception = new InfluxDBApiException("Testing exception", null); | ||
Assert.That(exception.StatusCode.ToString(), Is.EqualTo("0")); | ||
var headers = exception.Headers; | ||
Assert.That(exception.Headers, Is.Null); | ||
} | ||
|
||
[Test] | ||
public async Task GeneratedInfluxDbException() | ||
{ | ||
var requestId = Guid.NewGuid().ToString(); | ||
|
||
MockServer | ||
.Given(Request.Create().WithPath("/api/v2/write").UsingPost()) | ||
.RespondWith(Response.Create() | ||
.WithStatusCode(400) | ||
.WithBody("{ \"message\": \"just testing\", \"statusCode\": \"bad request\" }") | ||
.WithHeaders(new Dictionary<string, string>() | ||
{ | ||
{"Content-Type", "application/json"}, | ||
{"Trace-Id", "123456789ABCDEF0"}, | ||
{"Trace-Sampled", "false"}, | ||
{"X-Influxdb-Request-ID", requestId}, | ||
{"X-Influxdb-Build", "Cloud"} | ||
}) | ||
); | ||
|
||
_client = new InfluxDBClient(MockServerUrl, | ||
"my-token", | ||
"my-org", | ||
"my-database"); | ||
try | ||
{ | ||
await _client.WriteRecordAsync("wetbulb,location=prg val=20.1"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (ex is InfluxDBApiException) | ||
{ | ||
var idbae = (InfluxDBApiException)ex; | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(idbae.Message, Is.EqualTo("just testing")); | ||
Assert.That(idbae.StatusCode.ToString(), Is.EqualTo("BadRequest")); | ||
Assert.That(idbae.Headers.Count() == 7); | ||
}); | ||
var headersDix = idbae.Headers.ToFrozenDictionary(); | ||
Assert.Multiple(() => | ||
{ | ||
Assert.That(headersDix["Trace-Id"].First(), Is.EqualTo("123456789ABCDEF0")); | ||
Assert.That(headersDix["Trace-Sampled"].First(), Is.EqualTo("false")); | ||
Assert.That(headersDix["X-Influxdb-Request-ID"].First(), Is.EqualTo(requestId)); | ||
Assert.That(headersDix["X-Influxdb-Build"].First(), Is.EqualTo("Cloud")); | ||
}); | ||
} | ||
else | ||
{ | ||
Assert.Fail($"Should have thrown InfluxdbApiException. Not - {ex}"); | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AssemblyName>InfluxDB3.Examples.General</AssemblyName> | ||
<RootNamespace>InfluxDB3.Examples.General</RootNamespace> | ||
<StartupObject>InfluxDB3.Examples.General.Runner</StartupObject> | ||
<AssemblyOriginatorKeyFile>../../Keys/Key.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Client\Client.csproj" /> | ||
<ProjectReference Include="..\Downsampling\Downsampling.csproj" /> | ||
<ProjectReference Include="..\IOx\Examples.IOx.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.