mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-22 08:18:26 +02:00
Improved relative path checking based on file existence (#411)
* Improved relative path checking based on file existence If the file exists at the relative path, then use it. If not, then use the path as is. * Apply File.Exists logic to ReadResponseBodyAsString as well * Make path handling more robust since path is user defined * Unit tests for relative path feature * Replace all back and forward slashes with system dependent DirectorySeparatorChar * Attempt fix broken directory separator chars for Unix platforms * Revert wrapping GetMappingFolder with CleanPath * Move CleanPath logic to its own class * Remove whitespace * Remove more whitespace * Improve CleanPath method * Move PathUtils tests to separate class Add another test to ResponseWithBodyFromFileTests * Fix Response_ProvideResponse_WithBodyFromFile_InAdminMappingFolder * Debug Linux CI build * Debug Linux CI * print all files from admin mappings folder * Debug CleanPath * Fix removed leading directory separator char in Linux breaks file path Remove debugging statements * Move combine to PathUtils * PathUtils + PathUtilsTests * Remove replicated (3x) tests throughout ResponseWithBodyFromFileTests Co-authored-by: Stef Heyenrath <Stef.Heyenrath@gmail.com>
This commit is contained in:
36
src/WireMock.Net/Util/PathUtils.cs
Normal file
36
src/WireMock.Net/Util/PathUtils.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.IO;
|
||||
|
||||
namespace WireMock.Util
|
||||
{
|
||||
internal static class PathUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Robust handling of the user defined path.
|
||||
/// Also supports Unix and Windows platforms
|
||||
/// </summary>
|
||||
/// <param name="path">The path to clean</param>
|
||||
public static string CleanPath(string path)
|
||||
{
|
||||
return path?.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes leading directory separator chars from the filepath, which could break Path.Combine
|
||||
/// </summary>
|
||||
/// <param name="path">The path to remove the loading DirectorySeparatorChars</param>
|
||||
public static string RemoveLeadingDirectorySeparators(string path)
|
||||
{
|
||||
return path?.TrimStart(new[] { Path.DirectorySeparatorChar });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combine two paths
|
||||
/// </summary>
|
||||
/// <param name="root">The root path</param>
|
||||
/// <param name="path">The path</param>
|
||||
public static string Combine(string root, string path)
|
||||
{
|
||||
return Path.Combine(root, RemoveLeadingDirectorySeparators(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user