Hi, I was wondering if someone could help me with ...
# help
m
Hi, I was wondering if someone could help me with my wiremock setup. I’m usin the following docker compose setup:
Copy code
mockserver:
    image: "wiremock/wiremock:latest"
    container_name: mockserver
    volumes:
      - $PWD/tests:/home/wiremock
    entrypoint: [ "/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose" ]
and even doing:
Copy code
curl <http://localhost:8080/__admin/mappings>
gives me the following error:
Copy code
html><head><meta http-equiv='refresh' content='1;url=/login?from=%2F__admin%2Fmappings'/><script>window.location.replace('/login?from=%2F__admin%2Fmappings');</script></head><body style='background-color:white; color:white;'>


Authentication required
<!--
-->

</body></html>
r
can you include the full docker compose file, including the configured ports for the mockserver service?
m
so my full compose file is:
Copy code
services:
  mockserver:
    image: "wiremock/wiremock:latest"
    container_name: mockserver
    volumes:
      - $PWD/tests:/home/wiremock
    entrypoint: [ "/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose" ]
r
are you running the curl command from within the docker container?
m
no
r
the service isnt exposing a port so it seems unlikely that your curl command is hitting the wiremock server. do you have another server running on that port on your machine?
m
no not as I'm aware off
I've changed my compose to this:
Copy code
mockserver:
    image: "wiremock/wiremock:latest"
    container_name: mockserver
    ports:
      - 8450:8080
    volumes:
      - $PWD/tests:/home/wiremock
    entrypoint: [ "/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose" ]
so this works now, but its not loading in my mappings file:
Copy code
curl <http://127.0.0.1:8450/__admin/mappings>

{
  "mappings" : [ ],
  "meta" : {
    "total" : 0
  }
}%
this is my mappings.json file in my /tests folder:
Copy code
{
  "mappings": [
    {
      "request": {
        "method": "POST",
        "url": "/api/auth/authorize/"
      },
      "response": {
        "status": 200,
        "body": {
          "ACK": {
            "authorisation_code": "blah",
            "grant_type": "authorisation_code"
          }
        }
      }
    },
    {
      "request": {
        "method": "POST",
        "url": "/api/auth/token/"
      },
      "response": {
        "status": 200,
        "body": {
          "ACK": {
            "origin": "blah",
            "message": "request accepted",
            "id": "blah",
            "access_token": "blah",
            "expires_in": 1800,
            "token_type": "bearer"
          }
        }
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/api/kas/heartbeat"
      },
      "response": {
        "status": 200,
        "body": "hello"
      }
    },
    {
      "request": {
        "method": "GET",
        "url": "/api/ido/healthcheck"
      },
      "response": {
        "status": 200,
        "body": "all ok"
      }
    }
  ],
  "importOptions": {
    "duplicatePolicy": "IGNORE",
    "deleteAllNotInImport": true
  }
}
r
@Lee Turner has a great example of using docker-compose and wiremock, including showing how to mount mappings into the container
m
ok thanks. let me try out a few things. cheers
It still does want to load in the mapping files for me:
Copy code
services:
  mockserver:
    image: "wiremock/wiremock:latest"
    container_name: mockserver
    ports:
      - 8450:8080
    volumes:
      - ./tests/mappings:/home/wiremock/mappings
    entrypoint: [ "/docker-entrypoint.sh", "--global-response-templating", "--disable-gzip", "--verbose" ]
This is my dir structure for tests
wiremock crashes when bringing the container up:
Copy code
2024-07-04 15:01:41 2024-07-04 14:01:41.843 Verbose logging enabled
2024-07-04 15:01:42 Exception in thread "main" com.github.tomakehurst.wiremock.standalone.MappingFileException: Error loading file /home/wiremock/./mappings/authorize.json:
2024-07-04 15:01:42 Cannot deserialize value of type `java.lang.String` from Object value (token `JsonToken.START_OBJECT`)
2024-07-04 15:01:42     at com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:126)
2024-07-04 15:01:42     at com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:288)
2024-07-04 15:01:42     at com.github.tomakehurst.wiremock.core.WireMockApp.loadDefaultMappings(WireMockApp.java:282)
2024-07-04 15:01:42     at com.github.tomakehurst.wiremock.core.WireMockApp.<init>(WireMockApp.java:143)
2024-07-04 15:01:42     at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:74)
2024-07-04 15:01:42     at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:71)
2024-07-04 15:01:42     at wiremock.Run.main(Run.java:23)
this is my authorize.json file
Copy code
{
  "request": {
    "method": "POST",
    "url": "/api/auth/authorize/"
  },
  "response": {
    "status": 200,
    "body": {
      "ACK": {
        "authorisation_code": "blah",
        "grant_type": "authorisation_code"
      }
    }
  }
}
r
"body" is expecting a string. try changing "body" to "jsonBody"
m
ok thanks thats working now. Although, it doesn;t quite work as expected when sending a request to wiremock using Python. When I send a request to /api/auth/authorize/ - I get back the following response:
Copy code
<ClientResponse(<http://127.0.0.1:8450/api/auth/authorize/>) [200 OK]>
<CIMultiDictProxy('Matched-Stub-Id': '21ffc625-98e0-417f-be34-b2f79d708dc5', 'Transfer-Encoding': 'chunked')>
It's not returning the actual jsonbody expected.
Is there a setting or something I need to change?
l
Can you send a request to the stub using
curl
or postman and see what you get back?
m
so curl returns the expected jsonbody.
l
Cool, so it looks like WireMock is working as expected. Might be something you need to change in your client code
m
dont think so. the same code works with mockserver ok
with mockserver using my python code returns a json body
but with wiremock its returning the above reponse
l
It is a little puzzling. Given your stub mapping, WireMock isn't changing its behaviour based on the client that is sending the request. If you see the json body using curl, it should be sending the json body when called from your code
m
yeah its weird. trying to access .json() on my response object returns None. So I'm not sure what's happening here, but thanks for your help.
l
Does your client know it is a json response? Can you add a header to the response that sets the content type to json?
m
I can try. do you have an example for the syntax I need to use for wiremock?
l
Copy code
{
  "request": {
    "method": "POST",
    "url": "/api/auth/authorize/"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": {
      "ACK": {
        "authorisation_code": "blah",
        "grant_type": "authorisation_code"
      }
    }
  }
}
m
great thanks, let me try this
Perfect. thank you. That was the issue, I wasn't defining the header type on the response.
l
Great stuff. Glad it is working
m
Is it possible to set on another mapping the header content-type for the request as application/x-www-form-urlencoded?
r
request headers are just key value pairs, so you can add a "Content-Type" header with any value
m
If I have this as a mapping, I get an error from wiremock:
Copy code
{
  "request": {
    "method": "POST",
    "url": "/api/auth/token/",
    "headers": {
      "Content-Type": "application/x-www-form-urlencoded"
    }
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "jsonBody": {
      "ACK": {
        "origin": "blah",
        "message": "request accepted",
        "id": "blah",
        "access_token": "blah",
        "expires_in": 1800,
        "token_type": "bearer"
      }
    }
  }
}
Copy code
2024-07-04 16:42:02 "application/x-www-form-urlencoded" is not a valid match operation
2024-07-04 16:42:02     at com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:126)
2024-07-04 16:42:02     at com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:288)
2024-07-04 16:42:02     at com.github.tomakehurst.wiremock.core.WireMockApp.loadDefaultMappings(WireMockApp.java:282)
2024-07-04 16:42:02     at com.github.tomakehurst.wiremock.core.WireMockApp.<init>(WireMockApp.java:143)
2024-07-04 16:42:02     at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:74)
2024-07-04 16:42:02     at com.github.tomakehurst.wiremock.standalone.WireMockServerRunner.run(WireMockServerRunner.java:71)
2024-07-04 16:42:02     at wiremock.Run.main(Run.java:23)
r
ah on the request
m
yeah
r
you want to match the stub mapping only if the request has a header of
application/x-www-form-urlencoded
?
m
yes. so in my code I'm sending a post request with application/x-www-form-urlencoded in the header.
r
Copy code
"headers": {
  "content-type": {
    "equalTo": "application/x-www-form-urlencoded"
  }
}
will work. i suggest looking at the docs for more information on request matching
m
ok thanks