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

Need change response in runtime #120

Closed
seabornlee opened this issue Jul 10, 2015 · 9 comments
Closed

Need change response in runtime #120

seabornlee opened this issue Jul 10, 2015 · 9 comments

Comments

@seabornlee
Copy link
Contributor

In some scenarios, we need changing the response of an API in runtime.
I think there are 2 ways to achieve it.

  1. Config response as an array and iterate it
  2. Provide an API to toggle it manually

I see below feature on plan page, does it mean the same thing?

  • Runtime configuration change
@hirtenfelder
Copy link

+1

I had a similar scenario and created an ConditionalResponseHandler to handle it. The ConditionalResponseHandler holds a list with ConditionalResponses. For every request, it iterates through the list and selects the first wherein the condition is evaluated to true.

Example:
As long as checkTwo() returns false and firstTest is true, the response will be "content1". If
the returned value of checkTwo() will switch to true, from this moment the response will be "content2".

ConditionalResponse response1 =
ConditionalResponse.conditionalResponse(() -> fistTest, "content1");

ConditionalResponse response2 =
ConditionalResponse.conditionalResponse(() -> checkTwo(), "content2");

ConditionalResponseHandler handler = 
ConditionalResponseHandler.conditional(response2, response1);

httpServer.get(and(by(uri("/any/url"))).response(handler);

Maybe a candiate for a Pull Request?

Just a pity that Moco uses sourceCompatibility = 1.6 so that I can't use the Java Supplier<T> functional interface to define the condition.

@dreamhead
Copy link
Owner

Can you provide me an example condition? I just wonder why you need a handler dispatcher rather than a request matcher.

One more thing, Moco uses Guava which provides Supplier<T> too.

@hirtenfelder
Copy link

Hi @dreamhead

I'm not sure if a request matcher can help for my case. But I am pretty sure you can tell me if so.
The example is simplified..

I have an synchronization logic for two instances. The first instance tries to get information about a resource. Let’s do a HTTP GET /resource/1234. The remote instance (Moco) says HTTP 404 resource not found. An background synchronization task calls HTTP PUT to create the resource on the remote instance, I get an HTTP 201 created.

The remote instance now knows the resource with the id 1234. So let’s try again a HTTP GET /resource/1234. The remote instance says HTTP 200 OK.

My condition is, as long as I request GET /resource/1234 before the resource is synchronized, the remote instance should response with resource not found. After successfully creating the resource on the remote instance, the response should be HTTP OK.

I also tried the SequenceHandler. But it depends on how fast the test itself and the background synchronization task runs to have more or less sequences.

@dreamhead
Copy link
Owner

@hirtenfelder Thank you for your example.

But I'd like to put this condition to matcher rather than response handler if I were you.

httpServer.get(and(by(uri("/any/url"))), your_matcher1)).response(handler1);
httpServer.get(and(by(uri("/any/url"))), your_matcher2)).response(handler2);

Personally, I prefer that Moco acts as a static server which means nothing will be changed at runtime.

BTW, REST APIs will be published in a few days.

@hirtenfelder
Copy link

Hi @dreamhead

But my matchers are identical. There is no difference between the GET /resource/1234 before (your_matcher1) and after (your_matcher2) the PUT request.

@dreamhead
Copy link
Owner

GET /resource/1234 is identical, yes.

But your_matcher1 and your_matcher2 are different, these two different matchers could be your condition to distinguish your request is before or after your PUT request. You can simply put your condition to your matcher.

Moco will compare the whole request matcher including GET /resource/1234 and your matcher, rather than GET /resource/1234.

@hirtenfelder
Copy link

Hi @dreamhead

Thank you for your patience. I think now I understand. Of course, you’re right.

Instead of using my ConditionalResponseHandlerit’s better to use a new ConditionalRequestMatcher. The match(..) method of the RequestMatcherinterface is well-suited for my case.

This solution is much better. I try it and will show you the solution.

@dreamhead
Copy link
Owner

@hirtenfelder Great!

@ComeonBug
Copy link

ComeonBug commented Apr 11, 2022 via email

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

4 participants