Skip to content

Commit 171e3a7

Browse files
Logging bug in HttpRequesterMiddleware (#1953)
* Fix a bug in HttpRequesterMiddleware.cs, BadRequest should be logged as debug! * Clean code: refactor `CreateLogBasedOnResponse` * Unit tests * Remove redundant if-checking --------- Co-authored-by: Raman Maksimchuk <[email protected]>
1 parent 0404c24 commit 171e3a7

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

src/Ocelot/Requester/Middleware/HttpRequesterMiddleware.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ public async Task Invoke(HttpContext httpContext)
3939

4040
private void CreateLogBasedOnResponse(Response<HttpResponseMessage> response)
4141
{
42-
if (response.Data?.StatusCode <= HttpStatusCode.BadRequest)
42+
var status = response.Data?.StatusCode ?? HttpStatusCode.Processing;
43+
var reason = response.Data?.ReasonPhrase ?? "unknown";
44+
var uri = response.Data?.RequestMessage?.RequestUri?.ToString() ?? string.Empty;
45+
46+
string message() => $"{(int)status} ({reason}) status code of request URI: {uri}.";
47+
48+
if (status < HttpStatusCode.BadRequest)
4349
{
44-
Logger.LogInformation(() =>
45-
$"{(int)response.Data.StatusCode} ({response.Data.ReasonPhrase}) status code, request uri: {response.Data.RequestMessage?.RequestUri}");
50+
Logger.LogInformation(message);
4651
}
47-
else if (response.Data?.StatusCode >= HttpStatusCode.BadRequest)
52+
else
4853
{
49-
Logger.LogWarning(
50-
() => $"{(int)response.Data.StatusCode} ({response.Data.ReasonPhrase}) status code, request uri: {response.Data.RequestMessage?.RequestUri}");
54+
Logger.LogWarning(message);
5155
}
5256
}
5357
}

test/Ocelot.UnitTests/Requester/HttpRequesterMiddlewareTests.cs

+30-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public HttpRequesterMiddlewareTests()
3434
public void should_call_services_correctly()
3535
{
3636
this.Given(x => x.GivenTheRequestIs())
37-
.And(x => x.GivenTheRequesterReturns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage(System.Net.HttpStatusCode.OK))))
37+
.And(x => x.GivenTheRequesterReturns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage(HttpStatusCode.OK))))
3838
.When(x => x.WhenICallTheMiddleware())
3939
.Then(x => x.ThenTheDownstreamResponseIsSet())
4040
.Then(x => InformationIsLogged())
@@ -56,12 +56,38 @@ public void should_log_downstream_internal_server_error()
5656
{
5757
this.Given(x => x.GivenTheRequestIs())
5858
.And(x => x.GivenTheRequesterReturns(
59-
new OkResponse<HttpResponseMessage>(new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError))))
59+
new OkResponse<HttpResponseMessage>(new HttpResponseMessage(HttpStatusCode.InternalServerError))))
6060
.When(x => x.WhenICallTheMiddleware())
6161
.Then(x => x.WarningIsLogged())
6262
.BDDfy();
6363
}
64+
65+
[Theory]
66+
[Trait("Bug", "1953")]
67+
[InlineData(HttpStatusCode.OK)]
68+
[InlineData(HttpStatusCode.PermanentRedirect)]
69+
public void Should_LogInformation_when_status_is_less_than_BadRequest(HttpStatusCode status)
70+
{
71+
this.Given(x => x.GivenTheRequestIs())
72+
.And(x => x.GivenTheRequesterReturns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage(status))))
73+
.When(x => x.WhenICallTheMiddleware())
74+
.Then(x => x.InformationIsLogged())
75+
.BDDfy();
76+
}
6477

78+
[Theory]
79+
[Trait("Bug", "1953")]
80+
[InlineData(HttpStatusCode.BadRequest)]
81+
[InlineData(HttpStatusCode.NotFound)]
82+
public void Should_LogWarning_when_status_is_BadRequest_or_greater(HttpStatusCode status)
83+
{
84+
this.Given(x => x.GivenTheRequestIs())
85+
.And(x => x.GivenTheRequesterReturns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage(status))))
86+
.When(x => x.WhenICallTheMiddleware())
87+
.Then(x => x.WarningIsLogged())
88+
.BDDfy();
89+
}
90+
6591
private void ThenTheErrorIsSet()
6692
{
6793
_httpContext.Items.Errors().Count.ShouldBeGreaterThan(0);
@@ -103,18 +129,14 @@ private void ThenTheDownstreamResponseIsSet()
103129
private void WarningIsLogged()
104130
{
105131
_logger.Verify(
106-
x => x.LogWarning(
107-
It.IsAny<Func<string>>()
108-
),
132+
x => x.LogWarning(It.IsAny<Func<string>>()),
109133
Times.Once);
110134
}
111135

112136
private void InformationIsLogged()
113137
{
114138
_logger.Verify(
115-
x => x.LogInformation(
116-
It.IsAny<Func<string>>()
117-
),
139+
x => x.LogInformation(It.IsAny<Func<string>>()),
118140
Times.Once);
119141
}
120142
}

0 commit comments

Comments
 (0)