Skip to content

Commit

Permalink
Update logging logic to make it more flexible, deprecate existing met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
basdijkstra committed Nov 19, 2024
1 parent 2194d2b commit a83796f
Show file tree
Hide file tree
Showing 13 changed files with 696 additions and 65 deletions.
146 changes: 134 additions & 12 deletions RestAssured.Net.Tests/LoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
namespace RestAssured.Tests
{
using NUnit.Framework;
using RestAssured.Logging;
using RestAssured.Request.Builders;
using RestAssured.Request.Logging;
using RestAssured.Response.Logging;
using WireMock.RequestBuilders;
Expand All @@ -39,8 +41,35 @@ public void RequestDetailsCanBeWrittenToStandardOutputForJson()
{
this.CreateStubForLoggingJsonResponse();

var logConfig = new LogConfiguration
{
RequestLogLevel = Logging.RequestLogLevel.All,
};

Given()
.Log(logConfig)
.And()
.Accept("application/json")
.Header("CustomHeader", "custom header value")
.ContentType("application/json")
.Body(this.jsonBody)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-json-response")
.Then()
.StatusCode(200);
}

/// <summary>
/// A test demonstrating RestAssuredNet syntax for logging
/// JSON request details to the standard output.
/// </summary>
[Test]
public void RequestLogLevelCanBeSpecifiedUsingObsoleteMethod()
{
this.CreateStubForLoggingJsonResponse();

Given()
.Log(RequestLogLevel.All)
.Log(RestAssured.Request.Logging.RequestLogLevel.All)
.And()
.Accept("application/json")
.Header("CustomHeader", "custom header value")
Expand All @@ -61,8 +90,13 @@ public void RequestDetailsCanBeWrittenToStandardOutputForXml()
{
this.CreateStubForLoggingXmlResponse();

var logConfig = new LogConfiguration
{
RequestLogLevel = Logging.RequestLogLevel.All,
};

Given()
.Log(RequestLogLevel.All)
.Log(logConfig)
.ContentType("application/xml")
.Body(this.GetLocationAsXmlString())
.When()
Expand All @@ -80,11 +114,33 @@ public void ResponseDetailsCanBeWrittenToStandardOutputForJson()
{
this.CreateStubForLoggingJsonResponse();

var logConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.All,
};

Given()
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-json-response")
.Then()
.Log(ResponseLogLevel.All)
.StatusCode(200);
}

/// <summary>
/// A test demonstrating RestAssuredNet syntax for logging
/// XML response details to the standard output.
/// </summary>
[Test]
public void ResponseDetailsCanBeWrittenToStandardOutputForXmlUsingObsoleteMethod()
{
this.CreateStubForLoggingXmlResponse();

Given()
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-xml-response")
.Then()
.Log(RestAssured.Response.Logging.ResponseLogLevel.All)
.And()
.StatusCode(200);
}
Expand All @@ -98,12 +154,16 @@ public void ResponseDetailsCanBeWrittenToStandardOutputForXml()
{
this.CreateStubForLoggingXmlResponse();

var logConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.All,
};

Given()
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-xml-response")
.Then()
.Log(ResponseLogLevel.All)
.And()
.StatusCode(200);
}

Expand All @@ -117,12 +177,16 @@ public void NoResponseBodyDoesntThrowNullReferenceException()
{
this.CreateStubForLoggingResponseWithoutBody();

var logConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.All,
};

Given()
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-no-response-body")
.Then()
.Log(ResponseLogLevel.All)
.And()
.StatusCode(200);
}

Expand All @@ -136,8 +200,13 @@ public void NoRequestBodyDoesntThrowNullReferenceException()
{
this.CreateStubForLoggingResponseWithoutBody();

var logConfig = new LogConfiguration
{
RequestLogLevel = Logging.RequestLogLevel.All,
};

Given()
.Log(RequestLogLevel.All)
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-no-response-body")
.Then()
Expand All @@ -155,11 +224,16 @@ public void ResponseBodyDetailsAreLoggedOnlyOnErrorResponseCode()
{
this.CreateStubForErrorResponse();

var logConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.OnError,
};

Given()
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/error-response-body")
.Then()
.Log(ResponseLogLevel.OnError)
.StatusCode(404);
}

Expand All @@ -174,11 +248,16 @@ public void ResponseBodyDetailsAreNotLoggedOnOkResponseCode()
{
this.CreateStubForLoggingJsonResponse();

var logConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.OnError,
};

Given()
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-json-response")
.Then()
.Log(ResponseLogLevel.OnError)
.StatusCode(200);
}

Expand All @@ -193,11 +272,49 @@ public void ResponseBodyDetailsAreLoggedIfVerificationFails()
{
this.CreateStubForLoggingJsonResponse();

var logConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.OnVerificationFailure,
};

Given()
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-json-response")
.Then()
.StatusCode(200);
}

/// <summary>
/// A test demonstrating RestAssuredNet syntax for logging
/// response details to the standard output, overwriting logging details from
/// a request specification with specific settings.
/// </summary>
[Test]
public void ResponseBodyDetailsAreLoggedCorrectlyOverwritingRequestSpecificationSettings()
{
this.CreateStubForLoggingJsonResponse();

var originalLogConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.All,
};

var requestSpecification = new RequestSpecBuilder()
.WithLogConfiguration(originalLogConfig)
.Build();

var logConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.ResponseTime,
};

Given()
.Spec(requestSpecification)
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-json-response")
.Then()
.Log(ResponseLogLevel.OnVerificationFailure)
.StatusCode(200);
}

Expand All @@ -210,11 +327,16 @@ public void ResponseCookieDetailsAreLogged()
{
this.CreateStubForLoggingResponseWithCookie();

var logConfig = new LogConfiguration
{
ResponseLogLevel = Logging.ResponseLogLevel.All,
};

Given()
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/log-response-cookie")
.Then()
.Log(ResponseLogLevel.All)
.StatusCode(200);
}

Expand Down
13 changes: 11 additions & 2 deletions RestAssured.Net/Configuration/RestAssuredConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// </copyright>
namespace RestAssured.Configuration
{
using System;
using System.Net.Http;
using RestAssured.Logging;
using RestAssured.Request.Logging;
using RestAssured.Response.Logging;

Expand All @@ -29,15 +31,22 @@ public class RestAssuredConfiguration
/// </summary>
public bool DisableSslCertificateValidation { get; set; } = false;

/// <summary>
/// Configuration for be used when logging request and response details.
/// </summary>
public LogConfiguration LogConfiguration { get; set; } = new LogConfiguration();

/// <summary>
/// Setting to configure request logging level for all tests.
/// </summary>
public RequestLogLevel RequestLogLevel { get; set; } = RequestLogLevel.None;
[Obsolete("Use the LogConfiguration property to set request logging options instead. This property will be removed in RestAssured.Net 5.0.0")]
public Request.Logging.RequestLogLevel RequestLogLevel { get; set; } = Request.Logging.RequestLogLevel.None;

/// <summary>
/// Setting to configure response logging level for all tests.
/// </summary>
public ResponseLogLevel ResponseLogLevel { get; set; } = ResponseLogLevel.None;
[Obsolete("Use the LogConfiguration property to set response logging options instead. This property will be removed in RestAssured.Net 5.0.0")]
public Response.Logging.ResponseLogLevel ResponseLogLevel { get; set; } = Response.Logging.ResponseLogLevel.None;

/// <summary>
/// Setting to configure the <see cref="HttpCompletionOption"/> for all tests.
Expand Down
46 changes: 46 additions & 0 deletions RestAssured.Net/Logging/LogConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <copyright file="LogConfiguration.cs" company="On Test Automation">
// Copyright 2019 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

namespace RestAssured.Logging
{
using System.Collections.Generic;

/// <summary>
/// Defines the configuration to be used when logging request or response details.
/// </summary>
public class LogConfiguration
{
/// <summary>
/// The request logging level for this request.
/// </summary>
public RequestLogLevel RequestLogLevel { get; set; } = RequestLogLevel.None;

/// <summary>
/// The response logging level for this request.
/// </summary>
public ResponseLogLevel ResponseLogLevel { get; set; } = ResponseLogLevel.None;

/// <summary>
/// A list of sensitive request header and cookie names that should be redacted when logging request details.
/// </summary>
public List<string> SensitiveRequestHeadersAndCookies { get; set; } = new List<string>();

/// <summary>
/// A list of sensitive response header and cookie names that should be redacted when logging response details.
/// </summary>
public List<string> SensitiveResponseHeadersAndCookies { get; set; } = new List<string>();
}
}
49 changes: 49 additions & 0 deletions RestAssured.Net/Logging/RequestLogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <copyright file="RequestLogLevel.cs" company="On Test Automation">
// Copyright 2019 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

namespace RestAssured.Logging
{
/// <summary>
/// Contains the different logging levels for request logging.
/// </summary>
public enum RequestLogLevel
{
/// <summary>
/// Nothing will be logged to the console.
/// </summary>
None = 0,

/// <summary>
/// The HTTP method and the endpoint will be logged to the console.
/// </summary>
Endpoint = 1,

/// <summary>
/// Request headers will be logged to the console.
/// </summary>
Headers = 2,

/// <summary>
/// Request body will be logged to the console.
/// </summary>
Body = 3,

/// <summary>
/// All request details will be logged to the console.
/// </summary>
All = 4,
}
}
Loading

0 comments on commit a83796f

Please sign in to comment.