Skip to content

Commit

Permalink
アセットコードから改行文字を取り除いてからログ出力する (#2192)
Browse files Browse the repository at this point in the history
* アセットコードから改行文字を取り除いてからログ出力する

* 改行文字を取り除くstringの拡張メソッドを定義

* テストスイートの単位を修正

* null の入力に対して null を返却するように修正

* メソッド名を NewlineCharacters に変更
  • Loading branch information
KentaHizume authored Dec 27, 2024
1 parent f378a30 commit d25d012
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
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);
}
}

0 comments on commit d25d012

Please sign in to comment.