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

Proposal to send a Func when creating Responses #19

Open
iKarthik opened this issue May 27, 2016 · 3 comments
Open

Proposal to send a Func when creating Responses #19

iKarthik opened this issue May 27, 2016 · 3 comments

Comments

@iKarthik
Copy link

iKarthik commented May 27, 2016

Wouldn't it be a good idea if we can pass a Func while creating the Responses, this way when the response is being creating, the func will be invoked which then will decide the response
For example

server
  .Given(
    Requests.WithUrl("/api").UsingGet()
  )
  .RespondWith(
    Responses
      .WithStatusCode(200)
      .WithResponseFunc((request) => new Response())
  ); 

In the lambda, we can decide what response to send.
Thoughts?
Kay

@alexvictoor
Copy link
Owner

Hello Kay
RespondWith() method already takes as a parameter a IProvideResponses object.
So you can already implement this interface to buid a dynamic way a response.
Do you think we should replace this interface by a delegate in order to be able to use lambdas?
Regards

Alex

@iKarthik
Copy link
Author

iKarthik commented May 31, 2016

I want to be able to build the responses with lamdas so that we can do the following

_server.Given(Requests.WithUrl("/foo").UsingPost())
                .RespondWith(Responses.WithStatusCode(200).WithResponseFunc(Func));

Where Func could be

` private Response Func(string request)
        {
            if(request.Contains("SomeValue")) return new Response() {Body = "SomeValue Response"};
            if(request.Contains("ZeroValue")) return new Response() {Body = "ZeroValue Response"};
            if(request.Contains("OneValue")) return new Response() {Body = "OneValue Response"};
            return new Response();;
        }`

Hope that made sense

Thanks
Kay

@alexvictoor
Copy link
Owner

alexvictoor commented Jun 4, 2016

It totally makes sens
What I said in my previous message is that you can already do it, implementing interface IProvideResponses .
You could have:

class Handler : IProvideResponses {

  Task<Response> ProvideResponse(Request request) {
      if(request.Url.Contains("SomeValue")) return Task.FromResult(new Response() {Body = "SomeValue Response"});
...
   }
}

_server.Given(Requests.WithUrl("/foo").UsingPost())
                .RespondWith(new Handler())

Still you cannot use any lambda. We could replace interface IProvideResponses by a delegate to make it possible.

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