Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

アセットコードから改行文字を取り除いてからログ出力する #2192

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public AssetApplicationService(
/// </exception>
public async Task<AssetStreamInfo> GetAssetStreamInfoAsync(string assetCode)
{
this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoStart, assetCode);
// ログインジェクションを防ぐために改行文字を取り除きます。
var sanitizedAssetCode = assetCode.RemoveNewlineCharacters();
this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoStart, sanitizedAssetCode);

Asset? asset;
Stream? stream;
Expand All @@ -68,7 +70,7 @@ public async Task<AssetStreamInfo> GetAssetStreamInfoAsync(string assetCode)
scope.Complete();
}

this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoEnd, assetCode);
this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoEnd, sanitizedAssetCode);
return new(asset, stream);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Diagnostics.CodeAnalysis;

namespace System;

/// <summary>
/// <see cref="string"/> クラスの拡張メソッドを提供します。
/// </summary>
public static class StringExtentions
{
/// <summary>
/// 対象の文字列から改行文字(\r、\n)を取り除きます。
/// </summary>
/// <param name="target">対象の文字列。</param>
/// <returns>元の文字列から改行文字を取り除いた文字列。</returns>
[return: NotNullIfNotNull(nameof(target))]
public static string? RemoveNewlineCharacters(this string? target)
{
if (target == null)
{
return null;
}

return target.Replace("\n", string.Empty).Replace("\r", string.Empty);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace Dressca.UnitTests.SystemCommon;

public class StringExtentionsTest
{
[Theory]
[InlineData("\r\nLine1Line2", "Line1Line2")] // CRとLFを含む、先頭
[InlineData("Line1\rLine2", "Line1Line2")] // CRのみを含む、中間
[InlineData("Line1Line2\n", "Line1Line2")] // LFのみを含む、末尾
public void RemoveNewlineCharacters_改行文字があれば取り除かれる(string input, string expected)
{
// Arrange

// Act
var actual = input.RemoveNewlineCharacters();

// Assert
Assert.Equal(expected, actual);
}

[Fact]
public void RemoveNewlineCharacters_改行文字なし_変化なし()
{
// Arrange
var target = "Line1Line2";

// Act
var actual = target.RemoveNewlineCharacters();

// Assert
Assert.Equal(target, actual);
}

[Fact]
public void RemoveNewlineCharacters_null_nullを返却()
{
// Arrange
string? target = null;

// Act
var actual = target.RemoveNewlineCharacters();

// Assert
Assert.Null(actual);
}

[Fact]
public void RemoveNewlineCharacters_空文字_変化なし()
{
// Arrange
var target = string.Empty;

// Act
var actual = target.RemoveNewlineCharacters();

// Assert
Assert.Equal(target, actual);
}

[Theory]
[InlineData(" ")]
[InlineData(" ")]
public void RemoveNewlineCharacters_空白文字_変化なし(string target)
{
// Arrange

// Act
var actual = target.RemoveNewlineCharacters();

// Assert
Assert.Equal(target, actual);
}

[Theory]
[InlineData("\r\n")]
[InlineData("\r")]
[InlineData("\n")]
public void RemoveNewlineCharacters_改行コードのみ_取り除かれて空文字になる(string target)
{
// Arrange
var expected = string.Empty;

// Act
var actual = target.RemoveNewlineCharacters();

// Assert
Assert.Equal(expected, actual);
}
}
Loading