azagniotov / stubby4j

HTTP/1.1, HTTP/2 and WebSockets stub server for stubbing distributed web services in Docker and non-containerized environments for integration and contract testing
https://www.stubby4j.com
Apache License 2.0
357 stars 103 forks source link

[FEATURE REQUEST] Request proxying #232

Closed LenarBad closed 3 years ago

LenarBad commented 3 years ago

Proxy.

For example there is an existing web service.

So we can modify the existing behavior of the web service for specific requests. The service url could be configured in property files or through rest calls for all requests or for every request individually.

The url for the reference - WireMock - Proxying. It doesn't have to be the same, it has to be better :)

azagniotov commented 3 years ago

Hi @LenarBad ,

Thank you for providing the context. Right now, I am thinking about a potential implementation approach (below, just thinking aloud here), please let me know what you think:

In addition to configuring stubs in YAML or via a POST/PUT, you will also have to configure one or more proxy-configs, where the proxy-config describes how stubby4j should proxy requests that don't match any stubs, when they should go to the existing service.

In the following example of this new proposed config, I have configured four proxy-config that describe different behavior (their brief explanation follows below):

- proxy-config:
    name: some-unique-name-1
    proxy-type: as-is
    proxy-properties:
      url: https://jsonplaceholder.typicode.com/posts/1

- proxy-config:
    name: some-unique-name-2
    proxy-type: as-is
    proxy-properties:
      url: https://jsonplaceholder.typicode.com/posts/8

- proxy-config:
    name: some-unique-name-3
    proxy-type: mutated
    proxy-properties:
      url: https://jsonplaceholder.typicode.com/posts/1
      method: POST
      headers:
        content-type: application/json
      file: ../json/request/json_payload_1.json

- proxy-config:
    name: some-unique-name-4
    proxy-type: mutated
    proxy-properties:
      url: https://jsonplaceholder.typicode.com/posts/1/comments
      method: GET
      headers:
        content-type: application/json
      query:
        postId: 1

The proxy-type: as-is ensures that requests that don't match any stubby4j's stub will go to the existing service as-is, while proxy-type: mutated ensures that the request being proxied will be modified as per the properties of the proxy-properties.

How will the proxying be triggered/enabled at runtime?

When making an HTTP request to stubby4j, you will pass an additional header, let's say: x-stubby-proxy-config: some-unique-name-2 , which would determine which proxy-config to use when the request cannot be matches to any of the configured stubs.

Let me know your thoughts. In particular, I am curious to understand how rigid/flexible the above approach is and tedious (or not) to you as a user

LenarBad commented 3 years ago

Looks good. However I would start with one "DEFAULT" proxy config like

- proxy-config:
    name: DEFAULT
    proxy-type: as-is
    proxy-properties:
      path: https://jsonplaceholder.typicode.com

So if this config exists then all non-matching requests will be proxied

For example a non-matching request to /posts/8/comment will go to https://jsonplaceholder.typicode.com/posts/8/comment Also I suggest adding a new header to the proxied requests for security purposes that let users track these proxied requests in the logs.

Small baby steps will allow us to figure out if there is any issues with the approach and make changes to the approach if needed accordingly.

azagniotov commented 3 years ago

Thanks for the feedback, @LenarBad . Sounds good. Good suggestion on the tracking header too.

azagniotov commented 3 years ago

Hi @LenarBad , I am nearly finished and writing documentation now. Will you be able to test the new functionality? If so, can you clone the repo & build a JAR from the master branch OR will you require the JAR to be available on Maven Central?

P.S. Do not test yet, I want to finish the doco first

azagniotov commented 3 years ago

@LenarBad FYI: https://github.com/azagniotov/stubby4j#request-proxying

azagniotov commented 3 years ago

I released v7.3.0

azagniotov commented 3 years ago

I released v7.3.1. The docs: https://github.com/azagniotov/stubby4j#request-proxying

For now, I will close this ticket. Feel free to reopen it if needed or raise a new ticket. Thank you for raising this feature request!