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

Updated display URL parser to also support regular expressions #297

Merged
merged 5 commits into from
Dec 17, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Added "show more" button to request body for when the request body is very long (https://github.com/dukeofharen/httplaceholder/pull/294).
- Added text file response writer (https://github.com/dukeofharen/httplaceholder/pull/295).
- Updated request body variable parser to also support regular expressions to insert part of request body in the response body (https://github.com/dukeofharen/httplaceholder/pull/296).
- An option was added to user the display URL variable parser with a regular expression, to parse only a part of the URL as part of your response (https://github.com/dukeofharen/httplaceholder/pull/297).

# BREAKING CHANGES
- If you use any of the pre-built binaries of HttPlaceholder, you won't notice anything with upgrading. If you use the .NET tool version of HttPlaceholder, you need to have [.NET 7](https://dotnet.microsoft.com/en-us/) installed from now on.
Expand Down
21 changes: 20 additions & 1 deletion docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,7 @@ key3=value3

### Display URL

The display URL body parser makes it possible to write the complete URL to the response.
The display URL body parser makes it possible to write the complete URL to the response. It is also possible to provide a regular expression to only parse a part of the display URL in the response body of the stub. When providing a regular expression, you might want to surround the regex with single quotes (see example below) because else the regex might not be parsed correctly.

```yml
- id: dynamic-display-url-example
Expand All @@ -1795,6 +1795,25 @@ Let's say you do the following GET request: `http://localhost:5000/dynamic-displ
URL: http://localhost:5000/dynamic-display-url.txt?var1=value&var2=value2
```

```yml
- id: dynamic-display-url-regex-example
conditions:
method: GET
url:
path:
regex: /dynamic-display-url-regex/users/(.*)/orders
response:
enableDynamicMode: true
text: "User ID: ((display_url:'\/users\/([0-9]{3})\/orders'))"
headers:
X-Header: "((display_url:'\/users\/([0-9]{3})\/orders'))"
priority: 0
```

Let's say you make the request `http://localhost:5000/dynamic-display-url-regex/users/123/orders`.

`((display_url:'\/users\/([0-9]{3})\/orders'))` will be replaced with `123`.

### Root URL

The root URL body parser makes it possible to write the root URL (so URL without path + query string) to the response.
Expand Down
14 changes: 14 additions & 0 deletions docs/samples/14.6-dynamic-mode-display-url.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,18 @@
text: 'URL: ((display_url))'
headers:
X-Header: ((display_url))
priority: 0

# The display URL of the current request is parsed with a regular expression and the result of the regex is written to the response.
- id: dynamic-display-url-regex-example
conditions:
method: GET
url:
path:
regex: /dynamic-display-url-regex/users/(.*)/orders
response:
enableDynamicMode: true
text: "User ID: ((display_url:'\/users\/([0-9]{3})\/orders'))"
headers:
X-Header: "((display_url:'\/users\/([0-9]{3})\/orders'))"
priority: 0
19 changes: 19 additions & 0 deletions docs/samples/requests.json
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,25 @@
}
},
"response": []
},
{
"name": "Regex",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{rootUrl}}dynamic-display-url-regex/users/451/orders",
"host": [
"{{rootUrl}}dynamic-display-url-regex"
],
"path": [
"users",
"451",
"orders"
]
}
},
"response": []
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,52 @@ public async Task RequestBodyVariableHandler_Parse_HappyFlow()
// assert
Assert.AreEqual(expectedResult, result);
}

[TestMethod]
public async Task RequestBodyVariableHandler_Parse_Regex_HappyFlow()
{
// arrange
const string input = @"User ID: ((display_url:'\/users\/([0-9]{3})\/orders'))";
const string url = "http://localhost:5000/users/123/orders?key=value123";

const string expectedResult = "User ID: 123";

var httpContextServiceMock = _mocker.GetMock<IHttpContextService>();
var handler = _mocker.CreateInstance<DisplayUrlResponseVariableParsingHandler>();

httpContextServiceMock
.Setup(m => m.DisplayUrl)
.Returns(url);

// act
var matches = ResponseVariableParser.VarRegex.Matches(input);
var result = await handler.ParseAsync(input, matches, new StubModel(), CancellationToken.None);

// assert
Assert.AreEqual(expectedResult, result);
}

[TestMethod]
public async Task RequestBodyVariableHandler_Parse_Regex_NoResultFound_HappyFlow()
{
// arrange
const string input = @"User ID: ((display_url:'\/users\/([A-Z]{3})\/orders'))";
const string url = "http://localhost:5000/users/123/orders?key=value123";

const string expectedResult = "User ID: ";

var httpContextServiceMock = _mocker.GetMock<IHttpContextService>();
var handler = _mocker.CreateInstance<DisplayUrlResponseVariableParsingHandler>();

httpContextServiceMock
.Setup(m => m.DisplayUrl)
.Returns(url);

// act
var matches = ResponseVariableParser.VarRegex.Matches(input);
var result = await handler.ParseAsync(input, matches, new StubModel(), CancellationToken.None);

// assert
Assert.AreEqual(expectedResult, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,30 @@ public async Task RequestBodyVariableHandler_Parse_RegexMultiLine_HappyFlow()
// assert
Assert.AreEqual(expectedResult, result);
}

[TestMethod]
public async Task RequestBodyVariableHandler_Parse_RegexNoResultFound_HappyFlow()
{
// arrange
const string input = "Posted content: ((request_body:'key4=([a-z0-9]*)'))";
const string body = @"key1=value1
key2=value2
key3=value3";

const string expectedResult = "Posted content: ";

var httpContextServiceMock = _mocker.GetMock<IHttpContextService>();
var handler = _mocker.CreateInstance<RequestBodyResponseVariableParsingHandler>();

httpContextServiceMock
.Setup(m => m.GetBodyAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(body);

// act
var matches = ResponseVariableParser.VarRegex.Matches(input);
var result = await handler.ParseAsync(input, matches, new StubModel(), CancellationToken.None);

// assert
Assert.AreEqual(expectedResult, result);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using HttPlaceholder.Application.Infrastructure.DependencyInjection;
using HttPlaceholder.Application.Interfaces.Http;
using HttPlaceholder.Common;
using HttPlaceholder.Domain;
using Microsoft.Extensions.Logging;

namespace HttPlaceholder.Application.StubExecution.ResponseVariableParsingHandlers;

Expand All @@ -15,11 +17,14 @@ namespace HttPlaceholder.Application.StubExecution.ResponseVariableParsingHandle
internal class DisplayUrlResponseVariableParsingHandler : BaseVariableParsingHandler, ISingletonService
{
private readonly IHttpContextService _httpContextService;
private readonly ILogger<DisplayUrlResponseVariableParsingHandler> _logger;

public DisplayUrlResponseVariableParsingHandler(IHttpContextService httpContextService, IFileService fileService) :
public DisplayUrlResponseVariableParsingHandler(IHttpContextService httpContextService, IFileService fileService,
ILogger<DisplayUrlResponseVariableParsingHandler> logger) :
base(fileService)
{
_httpContextService = httpContextService;
_logger = logger;
}

/// <inheritdoc />
Expand All @@ -29,12 +34,50 @@ public DisplayUrlResponseVariableParsingHandler(IHttpContextService httpContextS
public override string FullName => "Display URL";

/// <inheritdoc />
public override string[] Examples => new[] {$"(({Name}))"};
public override string[] Examples => new[] {$"(({Name}))", $@"(({Name}:'\/users\/([0-9]{3})\/orders'))"};

/// <inheritdoc />
protected override Task<string> InsertVariablesAsync(string input, Match[] matches, StubModel stub,
CancellationToken cancellationToken) =>
Task.FromResult(matches
.Where(match => match.Groups.Count >= 2)
.Aggregate(input, (current, match) => current.Replace(match.Value, _httpContextService.DisplayUrl)));
.Aggregate(input, (current, match) => HandleDisplayUrl(match, current, _httpContextService.DisplayUrl)));

private string HandleDisplayUrl(Match match, string current, string url)
{
var result = string.Empty;
if (match.Groups.Count != 3)
{
_logger.LogWarning(
$"Number of regex matches for variable parser {GetType().Name} was {match.Groups.Count}, which should be 3.");
}
else if (string.IsNullOrWhiteSpace(match.Groups[2].Value))
{
result = url;
}
else
{
var regexValue = match.Groups[2].Value;
try
{
var regex = new Regex(regexValue, RegexOptions.Multiline,
TimeSpan.FromSeconds(Constants.RegexTimeoutSeconds));
var matches = regex.Matches(url).ToArray();
if (matches.Length >= 1 && matches[0].Groups.Count > 1)
{
result = matches[0].Groups[1].Value;
}
else
{
_logger.LogInformation($"No result found in display URL for regular expression '{regexValue}'.");
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, $"Error occurred while executing regex '{regexValue}' on display URL.'");
}
}

return current.Replace(match.Value, result);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The display URL body parser makes it possible to write the complete URL to the response.
The display URL body parser makes it possible to write the complete URL to the response. You can also use a regular expression to insert a part of the display URL in the response body.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ public async Task StubIntegration_RegularGet_Dynamic_DisplayUrl()
Assert.AreEqual(url, response.Headers.Single(h => h.Key == "X-Header").Value.Single());
}

[TestMethod]
public async Task StubIntegration_RegularGet_Dynamic_DisplayUrl_Regex()
{
// Arrange
var url = $"{TestServer.BaseAddress}dynamic-display-url-regex/users/123/orders?key=val";
const string expectedResult = "User ID: 123";

ClientDataResolverMock
.Setup(m => m.GetHost())
.Returns("localhost");

var request = new HttpRequestMessage(HttpMethod.Get, url);

// Act / Assert
using var response = await Client.SendAsync(request);
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
Assert.AreEqual(expectedResult, content);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual(MimeTypes.TextMime, response.Content.Headers.ContentType.ToString());

Assert.AreEqual("123", response.Headers.Single(h => h.Key == "X-Header").Value.Single());
}

[TestMethod]
public async Task StubIntegration_RegularGet_Dynamic_RootUrl()
{
Expand Down
13 changes: 13 additions & 0 deletions src/HttPlaceholder.Tests/Resources/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,19 @@
X-Header: ((display_url))
priority: 0

- id: dynamic-display-url-regex-example
conditions:
method: GET
url:
path:
equals: /dynamic-display-url-regex/users/123/orders
response:
enableDynamicMode: true
text: "User ID: ((display_url:'\/users\/([0-9]{3})\/orders'))"
headers:
X-Header: "((display_url:'\/users\/([0-9]{3})\/orders'))"
priority: 0

- id: dynamic-root-url-example
conditions:
method: GET
Expand Down
Loading