-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Do not use Newtonsoft.Json any more. 2. Test for message validation.
- Loading branch information
Showing
17 changed files
with
179 additions
and
54 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,74 @@ | ||
using System.Net; | ||
using System.Text; | ||
using System.Text.Json; | ||
using Microsoft.Extensions.Options; | ||
using RaygunCore.Messages; | ||
using RaygunCore.Services; | ||
|
||
namespace RaygunCore.Test; | ||
|
||
public class RaygunClientTest | ||
{ | ||
[Fact] | ||
public async Task Index() | ||
{ | ||
string serverUrl = "http://localhost:8888/"; | ||
var server = new HttpListener(); | ||
server.Prefixes.Add(serverUrl); | ||
server.Start(); | ||
var resultTask = server.GetContextAsync().ContinueWith(task => | ||
{ | ||
var context = task.Result; | ||
string content; | ||
using (var sr = new StreamReader(context.Request.InputStream, Encoding.UTF8)) | ||
content = sr.ReadToEnd(); | ||
|
||
context.Response.ContentLength64 = 0; | ||
context.Response.Close(); | ||
return ( | ||
Method: context.Request.HttpMethod, | ||
ContentType: context.Request.ContentType, | ||
Headers: context.Request.Headers, | ||
Content: content | ||
); | ||
}); | ||
|
||
string message = "Test message"; | ||
var severity = RaygunSeverity.Critical; | ||
var tags = new string[] { "tag1", "tag2" }; | ||
var httpClientFactory = new TestHttpClientFactory(); | ||
var raygunOptions = new RaygunOptions | ||
{ | ||
ApiKey = "API_KEY", | ||
ApiEndpoint = new Uri(serverUrl), | ||
AppVersion = "1.0", | ||
ThrowOnError = true | ||
}; | ||
var raygunMessageBuilder = new DefaultRaygunMessageBuilder(new IRaygunMessageProvider[] | ||
{ | ||
new MainMessageProvider(Options.Create(raygunOptions)), | ||
new TestUserMessageProvider() | ||
}); | ||
var raygunClient = new DefaultRaygunClient(httpClientFactory, raygunMessageBuilder, Enumerable.Empty<IRaygunValidator>(), Options.Create(raygunOptions)); | ||
await raygunClient.SendAsync(message, severity, tags); | ||
server.Stop(); | ||
|
||
var result = await resultTask; | ||
result.Method.Should().Be("POST"); | ||
result.Headers["X-ApiKey"].Should().Be(raygunOptions.ApiKey); | ||
result.ContentType.Should().StartWith("application/json"); | ||
result.Content.Should().NotBeNullOrEmpty(); | ||
result.Content.Should().NotContain("\t"); | ||
|
||
var resultMessage = JsonSerializer.Deserialize<RaygunMessage>(result.Content, new JsonSerializerOptions | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase | ||
})!; | ||
resultMessage.Details.Version.Should().Be(raygunOptions.AppVersion); | ||
resultMessage.Details.Tags.Should().BeEquivalentTo(tags.Prepend(severity.ToString())); | ||
resultMessage.Details.Error.Message.Should().Be(message); | ||
resultMessage.Details.User?.Identifier.Should().Be(TestUserMessageProvider.Email); | ||
resultMessage.Details.User?.Email.Should().Be(TestUserMessageProvider.Email); | ||
resultMessage.Details.User?.IsAnonymous.Should().BeFalse(); | ||
} | ||
} |
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="../RaygunCore.props" /> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>RaygunCore.Test</AssemblyName> | ||
<TargetFramework>net7.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="FluentAssertions" /> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../RaygunCore/RaygunCore.csproj" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
<PackageReference Include="FluentAssertions" Version="6.8.0" /> | ||
</ItemGroup> | ||
</Project> |
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,15 @@ | ||
namespace RaygunCore.Test; | ||
|
||
sealed class TestHttpClientFactory: IHttpClientFactory, IDisposable | ||
{ | ||
private readonly Lazy<HttpMessageHandler> _handlerLazy = new(() => new HttpClientHandler()); | ||
|
||
public HttpClient CreateClient(string name) | ||
=> new (_handlerLazy.Value, disposeHandler: false); | ||
|
||
public void Dispose() | ||
{ | ||
if (_handlerLazy.IsValueCreated) | ||
_handlerLazy.Value.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using RaygunCore.Messages; | ||
|
||
namespace RaygunCore.Test; | ||
|
||
class TestUserMessageProvider : IRaygunMessageProvider | ||
{ | ||
public const string Email = "user@example.net"; | ||
|
||
public void Apply(RaygunMessageDetails details) | ||
{ | ||
details.User = new(Email) | ||
{ | ||
Email = Email, | ||
IsAnonymous = false | ||
}; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.