-
-
Notifications
You must be signed in to change notification settings - Fork 234
ProxyUrlTransformer #1361
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
Merged
Merged
ProxyUrlTransformer #1361
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright © WireMock.Net | ||
|
|
||
| using System; | ||
| using WireMock.Settings; | ||
| using WireMock.Transformers; | ||
|
|
||
| namespace WireMock.Proxy; | ||
|
|
||
| internal static class ProxyUrlTransformer | ||
| { | ||
| internal static string Transform(WireMockServerSettings settings, ProxyUrlReplaceSettings replaceSettings, string url) | ||
| { | ||
| if (!replaceSettings.UseTransformer) | ||
| { | ||
| return url.Replace(replaceSettings.OldValue, replaceSettings.NewValue, replaceSettings.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); | ||
| } | ||
|
|
||
| var transformer = TransformerFactory.Create(replaceSettings.TransformerType, settings); | ||
| return transformer.Transform(replaceSettings.TransformTemplate, url); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 4 additions & 5 deletions
9
src/WireMock.Net.Minimal/Transformers/ITransformerContextFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| // Copyright © WireMock.Net | ||
|
|
||
| namespace WireMock.Transformers | ||
| namespace WireMock.Transformers; | ||
|
|
||
| internal interface ITransformerContextFactory | ||
| { | ||
| interface ITransformerContextFactory | ||
| { | ||
| ITransformerContext Create(); | ||
| } | ||
| ITransformerContext Create(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/WireMock.Net.Minimal/Transformers/TransformerFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright © WireMock.Net | ||
|
|
||
| using System; | ||
| using WireMock.Settings; | ||
| using WireMock.Transformers.Handlebars; | ||
| using WireMock.Transformers.Scriban; | ||
| using WireMock.Types; | ||
|
|
||
| namespace WireMock.Transformers; | ||
|
|
||
| internal static class TransformerFactory | ||
| { | ||
| internal static ITransformer Create(TransformerType transformerType, WireMockServerSettings settings) | ||
| { | ||
| switch (transformerType) | ||
| { | ||
| case TransformerType.Handlebars: | ||
| var factoryHandlebars = new HandlebarsContextFactory(settings); | ||
| return new Transformer(settings, factoryHandlebars); | ||
|
|
||
| case TransformerType.Scriban: | ||
| case TransformerType.ScribanDotLiquid: | ||
| var factoryDotLiquid = new ScribanContextFactory(settings.FileSystemHandler, transformerType); | ||
| return new Transformer(settings, factoryDotLiquid); | ||
|
|
||
| default: | ||
| throw new NotSupportedException($"{nameof(TransformerType)} '{transformerType}' is not supported."); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using System.Globalization; | ||
| using Moq; | ||
| using WireMock.Handlers; | ||
| using WireMock.Proxy; | ||
| using WireMock.Settings; | ||
| using WireMock.Types; | ||
| using Xunit; | ||
|
|
||
| namespace WireMock.Net.Tests.Proxy; | ||
|
|
||
| public class ProxyUrlTransformerTests | ||
| { | ||
| private readonly Mock<IFileSystemHandler> _fileSystemHandlerMock = new(); | ||
|
|
||
| [Fact] | ||
| public void Transform_WithUseTransformerFalse_PerformsSimpleReplace_CaseSensitive() | ||
| { | ||
| // Arrange | ||
| var settings = new WireMockServerSettings | ||
| { | ||
| FileSystemHandler = _fileSystemHandlerMock.Object, | ||
| Culture = CultureInfo.InvariantCulture | ||
| }; | ||
|
|
||
| var replaceSettings = new ProxyUrlReplaceSettings | ||
| { | ||
| TransformTemplate = null, | ||
| OldValue = "/old", | ||
| NewValue = "/new", | ||
| IgnoreCase = false | ||
| }; | ||
|
|
||
| var url = "http://example.com/old/path"; | ||
| var expected = "http://example.com/new/path"; | ||
|
|
||
| // Act | ||
| var actual = ProxyUrlTransformer.Transform(settings, replaceSettings, url); | ||
|
|
||
| // Assert | ||
| Assert.Equal(expected, actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Transform_WithUseTransformerFalse_PerformsSimpleReplace_IgnoreCase() | ||
| { | ||
| // Arrange | ||
| var settings = new WireMockServerSettings | ||
| { | ||
| FileSystemHandler = _fileSystemHandlerMock.Object, | ||
| Culture = CultureInfo.InvariantCulture | ||
| }; | ||
|
|
||
| var replaceSettings = new ProxyUrlReplaceSettings | ||
| { | ||
| TransformTemplate = null, // UseTransformer == false | ||
| OldValue = "/OLD", | ||
| NewValue = "/new", | ||
| IgnoreCase = true | ||
| }; | ||
|
|
||
| var url = "http://example.com/old/path"; // lowercase 'old' but OldValue is uppercase | ||
| var expected = "http://example.com/new/path"; | ||
|
|
||
| // Act | ||
| var actual = ProxyUrlTransformer.Transform(settings, replaceSettings, url); | ||
|
|
||
| // Assert | ||
| Assert.Equal(expected, actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Transform_WithUseTransformerTrue_UsesTransformer_ToTransformUrl() | ||
| { | ||
| // Arrange | ||
| var settings = new WireMockServerSettings | ||
| { | ||
| FileSystemHandler = _fileSystemHandlerMock.Object, | ||
| Culture = CultureInfo.InvariantCulture | ||
| }; | ||
|
|
||
| // Handlebars is the default TransformerType; the TransformTemplate uses the model directly. | ||
| var replaceSettings = new ProxyUrlReplaceSettings | ||
| { | ||
| TransformTemplate = "{{this}}-transformed", | ||
| // TransformerType defaults to Handlebars but set explicitly for clarity. | ||
| TransformerType = TransformerType.Handlebars | ||
| }; | ||
|
|
||
| var url = "http://example.com/path"; | ||
| var expected = "http://example.com/path-transformed"; | ||
|
|
||
| // Act | ||
| var actual = ProxyUrlTransformer.Transform(settings, replaceSettings, url); | ||
|
|
||
| // Assert | ||
| Assert.Equal(expected, actual); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.