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

Question : Extracting text from a request.body that is not json #201

Closed
fransjvanderlinde opened this issue Sep 6, 2018 · 7 comments
Closed

Comments

@fransjvanderlinde
Copy link

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.

@StefH StefH changed the title pulling text from a request.body that is not json Question : Extracting text from a request.body that is not json Sep 6, 2018
@StefH
Copy link
Collaborator

StefH commented Sep 6, 2018

Correct.
Currently it's only possible to use all functionality from Handlebars on a text string or on a json object.

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.

@StefH
Copy link
Collaborator

StefH commented Sep 7, 2018

Hello @fransjvanderlinde, I could implement logic for

  • RegEx
  • XPath
  • DynamicLinq

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 Regex.Match could be:

{
    "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?

@fransjvanderlinde
Copy link
Author

fransjvanderlinde commented Sep 7, 2018

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}}")

StefH added a commit that referenced this issue Sep 7, 2018
StefH added a commit that referenced this issue Sep 7, 2018
@StefH
Copy link
Collaborator

StefH commented Sep 7, 2018

Hi @fransjvanderlinde,

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?

@StefH
Copy link
Collaborator

StefH commented Sep 8, 2018

Or maybe better to keep the name Regex.Match for both?

@fransjvanderlinde
Copy link
Author

Looks good. Thanks

@StefH
Copy link
Collaborator

StefH commented Sep 11, 2018

I'll create PR and new NuGet.

StefH added a commit that referenced this issue Sep 11, 2018
* #201

* Handlebars : Linq and JsonPath

* Rename to Regex.Match

* unit test coverage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants