-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
Question : Extracting text from a request.body that is not json #201
Comments
Correct. Using RegEx (on a string), XPath (on a string) or Linq (on a json object) is not yet supported. I'm working on the last now. |
Hello @fransjvanderlinde, I could implement logic for
on the request body (string) using extensions on HandleBars, just like I did for https://github.com/WireMock-Net/WireMock.Net/wiki/Response-Templating#jsonpathselecttoken So an example mapping for {
"Request": {
"Path": {
"Matchers": [
{
"Name": "WildcardMatcher",
"Pattern": "/regex"
}
]
},
"Methods": [
"post"
]
},
"Response": {
"Body": "{{Regex.Match request.body \"...Regex to match something here...\"}}",
"UseTransformer": true
}
} Would this idea be what you need? |
Yes that sounds like it will work. I did something like below in my app which did give me the values but i would like to do a lot more complex regex so solution suggested will work much better. Handlebars.RegisterHelper("extractamount", (output, context, arguments) =>
{
var match = Regex.Match((string) arguments[0], @"\d +.+\d");
if (match.Success)
{
output.WriteSafeString(match.Value);
}
else
{
output.WriteSafeString("0");
}
}
server
.Given(Request
.Create()
.WithPath("/test")
.UsingPost())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/text")
.WithTransformer().WithBody(@"{{extractvalue request.body}}") |
I've update the code to support this logic: Regex.Matches // Assign
var body = new BodyData { BodyAsString = "https://localhost:5000/" };
var request = new RequestMessage(new UrlDetails("http:https://localhost:1234"), "POST", ClientIp, body);
var response = Response.Create()
.WithBody("{{#Regex.Matches request.body \"^(?<proto>\\w+):https://[^/]+?(?<port>\\d+)/?\"}}{{this.port}}-{{this.proto}}{{/Regex.Matches}}")
.WithTransformer();
// Act
var responseMessage = await response.ProvideResponseAsync(request);
// assert
Check.That(responseMessage.Body).Equals("5000-https"); Regex.Match // Assign
var body = new BodyData { BodyAsString = "abc" };
var request = new RequestMessage(new UrlDetails("http:https://localhost:1234"), "POST", ClientIp, body);
var response = Response.Create()
.WithBody("{{Regex.Match request.body \"^(?<x>\\w+)$\"}}")
.WithTransformer();
// Act
var responseMessage = await response.ProvideResponseAsync(request);
// assert
Check.That(responseMessage.Body).Equals("abc"); Would this be as you would use it? |
Or maybe better to keep the name |
Looks good. Thanks |
I'll create PR and new NuGet. |
Hi is there any way to use the transformers to pull text from a requestbody by passing in a regex expression and using that value in response body. At moment it looks like code only caters for json to get info from a json node.
The text was updated successfully, but these errors were encountered: