Skip to content

Commit 85c6f3c

Browse files
committed
tests
1 parent 9b4ce43 commit 85c6f3c

4 files changed

Lines changed: 25 additions & 28 deletions

File tree

ManagedCode.Communication.Extensions/CommunicationMiddleware.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ public async Task Invoke(HttpContext httpContext)
3434
ex is InvalidDataContractException)
3535
{
3636
_logger.LogError("Request throw an error", ex);
37-
//httpContext.Response.Clear();
3837
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
39-
var json = JsonSerializer.Serialize(Result.Fail(HttpStatusCode.InternalServerError, ex));
38+
var result = Result.Fail(HttpStatusCode.InternalServerError, ex);
39+
var json = JsonSerializer.Serialize(result);
4040
await httpContext.Response.WriteAsJsonAsync(json);
4141
}
4242
catch (Exception ex) when (ex is ValidationException)
4343
{
4444
_logger.LogError("Request throw an error", ex);
45-
//httpContext.Response.Clear();
4645
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
47-
var json = JsonSerializer.Serialize(Result.Fail(HttpStatusCode.InternalServerError, ex));
46+
var result = Result.Fail(HttpStatusCode.InternalServerError, ex.Message);
47+
var json = JsonSerializer.Serialize(result);
4848
await httpContext.Response.WriteAsJsonAsync(json);
4949
}
5050
catch (Exception ex)
5151
{
5252
_logger.LogError("Request throw an error", ex);
53-
//httpContext.Response.Clear();
5453
httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
55-
var json = JsonSerializer.Serialize(Result.Fail(HttpStatusCode.InternalServerError, ex));
54+
var result = Result.Fail(HttpStatusCode.InternalServerError, ex.Message);
55+
var json = JsonSerializer.Serialize(result);
5656
await httpContext.Response.WriteAsJsonAsync(json);
5757
}
5858
sw.Stop();

ManagedCode.Communication.Tests/ControllerTests.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

ManagedCode.Communication.Tests/ControllerTests/MiddlewareTests.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.ComponentModel.DataAnnotations;
12
using System.Net;
23
using System.Net.Http.Json;
34
using System.Net.Mime;
@@ -6,6 +7,7 @@
67
using ManagedCode.Communication.Tests.TestApp;
78
using Xunit;
89
using Xunit.Abstractions;
10+
using JsonSerializer = System.Text.Json.JsonSerializer;
911

1012
namespace ManagedCode.Communication.Tests;
1113

@@ -22,13 +24,20 @@ public MiddlewareTests(ITestOutputHelper outputHelper, TestClusterApplication ap
2224
}
2325

2426
[Fact]
25-
public async Task Track()
27+
public async Task ValidationException()
2628
{
2729
var response = await _application.CreateClient().GetAsync($"test/test1");
2830
response.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
2931
var content = await response.Content.ReadAsStringAsync();
3032
var result = await response.Content.ReadFromJsonAsync<Result>();
3133
}
32-
3334

35+
[Fact]
36+
public async Task InvalidDataException()
37+
{
38+
var response = await _application.CreateClient().GetAsync($"test/test2");
39+
response.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
40+
var content = await response.Content.ReadAsStringAsync();
41+
var result = await response.Content.ReadFromJsonAsync<Result>();
42+
}
3443
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel.DataAnnotations;
2+
using System.IO;
23
using Microsoft.AspNetCore.Mvc;
34

45
namespace ManagedCode.Communication.Tests.TestApp.Controllers;
@@ -7,9 +8,15 @@ namespace ManagedCode.Communication.Tests.TestApp.Controllers;
78
public class TestController : ControllerBase
89
{
910
[HttpGet("test1")]
10-
public ActionResult<string> Authorize()
11+
public ActionResult<string> Test1()
1112
{
1213
throw new ValidationException();
1314
}
1415

16+
[HttpGet("test2")]
17+
public ActionResult<string> Test2()
18+
{
19+
throw new InvalidDataException();
20+
}
21+
1522
}

0 commit comments

Comments
 (0)