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

Interested in callbacks? #66

Closed
alastairtree opened this issue Nov 20, 2017 · 8 comments
Closed

Interested in callbacks? #66

alastairtree opened this issue Nov 20, 2017 · 8 comments

Comments

@alastairtree
Copy link
Collaborator

alastairtree commented Nov 20, 2017

Hello again.

Having played with the API a bit I was struggling to implement a stub where I needed a tiny bit more logic in creating the response, for example to do string manipulation on the request in a more granular way than handlebars, or in generating new IDs say.

I have experimented with a callback API like this:

  server.Given(Request.Create()
                .WithPath("/foo")
                .UsingGet())
            .RespondWith(Response.Create()
                .WithCallback(req => new ResponseMessage {Body = req.Path + "Bar"}));

The minimal implementation is in my fork, see the comparison: https://github.com/WireMock-Net/WireMock.Net/compare/master...alastairtree:feat/callbacks?expand=1

Is this something you would be interested in? Happy to do some extra work if needed to get something like it adopted. I noticed it also came up in Mock4net (alexvictoor/mock4net#19)

Thanks for the great library

Alastair

@StefH
Copy link
Collaborator

StefH commented Nov 20, 2017

Hi Alastair. This sounds ok.

However maybe the names should be WithResponseFunc and WithResponseFuncAsync. (We would also need the async version if possible.)

Some code is already there in DynamicResponseProvider.cs, however we cannot reuse those classes.

Would this be an idea?

@joostvanhassel
Copy link

Hi, looks like a great way to provide access to the request! What can I do to help?

@StefH
Copy link
Collaborator

StefH commented Feb 21, 2018

@joostvanhassel Thanks for helping out. I think that the code from @alastairtree is a good start, and maybe already enough to get this issue solved?

@joostvanhassel
Copy link

@StefH the changes as proposed by @alastairtree do fully cover what I would like to do for now. I think it is good to take these changes in and improve later on if further if needed.

@StefH
Copy link
Collaborator

StefH commented Feb 22, 2018

OK.
Currently I'm working on issue #91, this needs to be solved 100% before I continue on this one. So if you have time, you can also review this issue.

@StefH
Copy link
Collaborator

StefH commented Mar 1, 2018

Added to the source-code.

@alastairtree
Copy link
Collaborator Author

alastairtree commented Mar 2, 2018

@StefH thanks - this looks pretty good! Been thinking about syntax a bit, it is worth adding an overload on the WithBody() response builder method in addition that accepts a func<RequestMessage,string> something like the below. Would probably be a nice productivity gain and makes it easy for the majority case where you just want to build the body string dynamically?

server
  .Given(
    Request.Create().WithPath("/some/thing").UsingGet()
  )
  .RespondWith(
    Response.Create()
      .WithStatusCode(200)
      .WithBody(req => $"{{ path : '{req.Path}' }}")
  );

@alastairtree
Copy link
Collaborator Author

I have added a PR and test to show what i mean, see #102

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

3 participants