Skip to content

Commit

Permalink
Fix issue with not properly masking headers and cookies when specifie…
Browse files Browse the repository at this point in the history
…d in request specification
  • Loading branch information
basdijkstra committed Nov 20, 2024
1 parent 0bf817e commit 5d3b042
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
26 changes: 13 additions & 13 deletions RestAssured.Net.Tests/LoggingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void RequestDetailsCanBeWrittenToStandardOutputForJson()

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

Given()
Expand Down Expand Up @@ -90,7 +90,7 @@ public void RequestDetailsCanBeWrittenToStandardOutputForXml()

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

Given()
Expand All @@ -114,7 +114,7 @@ public void ResponseDetailsCanBeWrittenToStandardOutputForJson()

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

Given()
Expand Down Expand Up @@ -154,7 +154,7 @@ public void ResponseDetailsCanBeWrittenToStandardOutputForXml()

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

Given()
Expand All @@ -177,7 +177,7 @@ public void NoResponseBodyDoesntThrowNullReferenceException()

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

Given()
Expand All @@ -200,7 +200,7 @@ public void NoRequestBodyDoesntThrowNullReferenceException()

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

Given()
Expand All @@ -224,7 +224,7 @@ public void ResponseBodyDetailsAreLoggedOnlyOnErrorResponseCode()

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

Given()
Expand All @@ -248,7 +248,7 @@ public void ResponseBodyDetailsAreNotLoggedOnOkResponseCode()

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

Given()
Expand All @@ -272,7 +272,7 @@ public void ResponseBodyDetailsAreLoggedIfVerificationFails()

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

Given()
Expand All @@ -295,7 +295,7 @@ public void ResponseBodyDetailsAreLoggedCorrectlyUsingRequestSpecificationSettin

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

var requestSpecification = new RequestSpecBuilder()
Expand All @@ -322,7 +322,7 @@ public void ResponseBodyDetailsAreLoggedCorrectlyOverwritingRequestSpecification

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

var requestSpecification = new RequestSpecBuilder()
Expand All @@ -331,7 +331,7 @@ public void ResponseBodyDetailsAreLoggedCorrectlyOverwritingRequestSpecification

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

Given()
Expand All @@ -354,7 +354,7 @@ public void ResponseCookieDetailsAreLogged()

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

Given()
Expand Down
15 changes: 8 additions & 7 deletions RestAssured.Net.Tests/MaskingSensitiveDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ public class MaskingSensitiveDataTests : TestBase
[SetUp]
public void CreateRequestSpecification()
{
var logConfig = new LogConfiguration
{
RequestLogLevel = RequestLogLevel.All,
ResponseLogLevel = ResponseLogLevel.All,
SensitiveRequestHeadersAndCookies = new List<string>() { "SensitiveRequestHeader", "SensitiveRequestCookie" },
};

this.requestSpecification = new RequestSpecBuilder()
.WithPort(9876)
.WithMaskingOfHeadersAndCookies(new List<string>() { "SensitiveRequestHeader", "SensitiveRequestCookie" })
.WithLogConfiguration(logConfig)
.Build();
}

Expand Down Expand Up @@ -158,16 +165,10 @@ public void SensitiveHeaderAndCookieNamesCanBeDefinedInRequestSpecification()
{
this.CreateStubForMaskingSensitiveData();

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

Given()
.Spec(this.requestSpecification)
.Header("NonsensitiveRequestHeader", "This one is printed")
.Header("SensitiveRequestHeader", "This one is masked")
.Log(logConfig)
.When()
.Get($"{MOCK_SERVER_BASE_URL}/masking-sensitive-data")
.Then()
Expand Down
3 changes: 2 additions & 1 deletion RestAssured.Net/Request/Builders/RequestSpecBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,10 @@ public RequestSpecBuilder WithJsonSerializerSettings(JsonSerializerSettings json
/// </summary>
/// <param name="sensitiveHeaderOrCookieNames">The names of the request headers or cookies to be masked when logging.</param>
/// <returns>The current <see cref="RequestSpecBuilder"/> object.</returns>
[Obsolete("Please specify request header and cookie names to be masked using the LogConfiguration. This method will be removed in RestAssured.Net 5.0.0")]
public RequestSpecBuilder WithMaskingOfHeadersAndCookies(List<string> sensitiveHeaderOrCookieNames)
{
this.requestSpecification.SensitiveRequestHeadersAndCookies.AddRange(sensitiveHeaderOrCookieNames);
this.requestSpecification.LogConfiguration.SensitiveRequestHeadersAndCookies.AddRange(sensitiveHeaderOrCookieNames);
return this;
}

Expand Down
16 changes: 8 additions & 8 deletions RestAssured.Net/Request/ExecutableRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public class ExecutableRequest : IDisposable
/// <summary>
/// The response logging level for this request.
/// </summary>
internal RestAssured.Response.Logging.ResponseLogLevel ResponseLoggingLevel { get; set; }
internal Response.Logging.ResponseLogLevel ResponseLoggingLevel { get; set; }

Check warning on line 73 in RestAssured.Net/Request/ExecutableRequest.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

'ResponseLogLevel' is obsolete: 'Use the ResponseLogLevel enum in the RestAssured.Logging namespace instead. This enum will be removed in RestAssured.Net 5.0.0'

Check warning on line 73 in RestAssured.Net/Request/ExecutableRequest.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

'ResponseLogLevel' is obsolete: 'Use the ResponseLogLevel enum in the RestAssured.Logging namespace instead. This enum will be removed in RestAssured.Net 5.0.0'

Check warning on line 73 in RestAssured.Net/Request/ExecutableRequest.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

'ResponseLogLevel' is obsolete: 'Use the ResponseLogLevel enum in the RestAssured.Logging namespace instead. This enum will be removed in RestAssured.Net 5.0.0'

Check warning on line 73 in RestAssured.Net/Request/ExecutableRequest.cs

View workflow job for this annotation

GitHub Actions / build (9.0.x)

'ResponseLogLevel' is obsolete: 'Use the ResponseLogLevel enum in the RestAssured.Logging namespace instead. This enum will be removed in RestAssured.Net 5.0.0'

/// <summary>
/// The configuration settings to use when logging request and response details.
/// </summary>
internal LogConfiguration LogConfiguration { get; set; }
internal LogConfiguration? LogConfiguration { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="ExecutableRequest"/> class.
Expand Down Expand Up @@ -702,12 +702,6 @@ private VerifiableResponse Send(HttpMethod httpMethod, string endpoint)
this.httpCompletionOption = this.requestSpecification.HttpCompletionOption;
}

// Add header and cookie values to be masked specified in RequestSpecification to the list
if (this.requestSpecification != null)
{
this.sensitiveRequestHeadersAndCookies.AddRange(this.requestSpecification.SensitiveRequestHeadersAndCookies);
}

var legacyLogConfiguration = new LogConfiguration
{
RequestLogLevel = (RequestLogLevel)this.RequestLoggingLevel,
Expand All @@ -723,6 +717,12 @@ private VerifiableResponse Send(HttpMethod httpMethod, string endpoint)
this.LogConfiguration ??= this.requestSpecification.LogConfiguration;
}

// Add header and cookie values to be masked specified in RequestSpecification to the list
if (this.requestSpecification != null)
{
this.sensitiveRequestHeadersAndCookies.AddRange(this.requestSpecification.SensitiveRequestHeadersAndCookies);
}

var logger = new RequestResponseLogger(this.LogConfiguration ?? legacyLogConfiguration);

// RequestLogger.LogToConsole(this.request, this.RequestLoggingLevel, this.cookieCollection, this.sensitiveRequestHeadersAndCookies);
Expand Down

0 comments on commit 5d3b042

Please sign in to comment.