John Hamlett IV
John Hamlett IV

Reputation: 495

Proxying multiple URL using wiremock-docker

I have a project that utilizes microservices. I am trying to use wire-mock to create a test environment.

How can I proxy multiple URLs using docker-compose.

Here is my docker-compose file.

networks:
    ft-simulator:
        external: false
services:
    app:
        depends_on:
        - nginx
        environment:
        - SPRING_PROFILES_ACTIVE=simulator
        - JAVA_FLAGS=-Dhttp.proxyHost=wiremock -Dhttp.proxyPort=8080
        healthcheck:
            interval: 1m
            retries: 3
            test:
            - CMD
            - curl
            - -f
            - http://localhost:8080/health
            timeout: 10s
        image: ft-simulator:latest
        ports:
          - "8080:8080"
        networks:
            ft-simulator:
                aliases:
                - bcp
    nginx:
        image: nginx
        ports:
          - "80:80"
          - "443:443"
        networks:
        - ft-simulator
    wiremock:
        image: rodolpheche/wiremock:2.8.0-alpine
        networks:
        - ft-simulator
        ports:
          - "8081:8080"
        volumes:
          - "$PWD/stubs:/home/wiremock"
        command: ["--proxy-all=http://bcp:8080, http://www.google.com"]
version: '3.1'

The Google is simply a placeholder until it starts up.

When I run docker-compose up I get the following error

crp11070m:wiremock-simulation john.hamlett$ docker logs -f wiremocksimulation_wiremock_1
Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in authority at index 7: http://bcp:8080, http://www.google.com
    at java.net.URI.create(URI.java:852)
    at com.github.tomakehurst.wiremock.standalone.CommandLineOptions.proxyHostHeader(CommandLineOptions.java:274)
    at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:123)
    at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:72)
    at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:65)
    at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.main(WireMockServerRunner.java:113)
Caused by: java.net.URISyntaxException: Illegal character in authority at index 7: http://bcp:8080, http://www.google.com
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.parseAuthority(URI.java:3186)
    at java.net.URI$Parser.parseHierarchical(URI.java:3097)
    at java.net.URI$Parser.parse(URI.java:3053)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 5 more

Upvotes: 0

Views: 4094

Answers (1)

Tom
Tom

Reputation: 4159

It isn't possible to proxy to more than one target URL at the same time using the --proxy-all parameter.

What you can do is vary the proxy target based on the incoming request by creating a stub mapping with a proxy response for each route e.g.

Route 1:

{
    "request": {
        "urlPattern": "/route1/.*"
    },

    "response": {
        "proxyBaseUrl" : "http://target-host-1.com"
    }
}

Route 2:

{
    "request": {
        "urlPattern": "/route2/.*"
    },

    "response": {
        "proxyBaseUrl" : "http://target-host-2.com"
    }
}

Note that the URL path part will get added to the URL, so you'd end up with requests proxied to http://target-host-1.com/route1/whatever and http://target-host-2.com/route2/whatever.

Upvotes: 2

Related Questions