Enabling CORS doesn't seem to work

1,200 views
Skip to first unread message

Kunal Ashar

unread,
Feb 23, 2017, 12:16:02 PM2/23/17
to wiremock-user
Hi,
I've been used wiremock effectively for some time now, and I wanted to enable CORS access to the mocked APIs.
I've tried setting Access-Control-Allow-Origin: * and other headers in the response header, both to no avail.
Here's an example of a mapping that I have:
{
"request": {
"method": "POST",
"urlPattern": "/api/v2/general/login[\\/\\&\\?]*",
"bodyPatterns": [{
"equalToJson": "{\"password\":\"password\",\"username\":\"ku...@ashar.com\"} ",
"jsonCompareMode": "LENIENT",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
}]
},
"response": {
"status": 200,
"headers": {
             "Content-Type": "application/json",
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Methods" : "*",
"Access-Control-Allow-Headers": "Accept, Content-Type, Content-Encoding, Server, Transfer-Encoding",
"X-Content-Type-Options" : "nosniff",
"x-frame-options" : "DENY",
"x-xss-protection" : "1; mode=block"
        },
"bodyFileName": "/login_response_kunalashar.json"
}
}

What am I doing wrong here that's causing CORS to not work?
Thanks in advance.
Kunal Ashar

Larry Presswood

unread,
Sep 18, 2017, 2:44:58 PM9/18/17
to wiremock-user
I have seen a few proposed solutions on the we that look like what you were trying to do.  Anyone how can we fix this?

Tom Akehurst

unread,
Sep 18, 2017, 3:55:36 PM9/18/17
to wiremock-user
If you're trying to do a CORS POST, the browser will always make an OPTIONS call first. It's this that needs to return the Access-Control-* headers, so I suggest creating an additional stub for this.

Kunal Ashar

unread,
Sep 18, 2017, 3:59:25 PM9/18/17
to wiremock-user
Since this post was pretty old and no one had responded, I already tried the OPTIONS method (posted here: https://groups.google.com/forum/#!topic/wiremock-user/xsUvCzg5DeM), and it too didn't work.

P Singh

unread,
Sep 28, 2018, 7:57:00 AM9/28/18
to wiremock-user
Hi,

Any solution found to this?
I too have landed to the same issue .

Kunal Ashar

unread,
Sep 28, 2018, 8:21:00 AM9/28/18
to wiremock-user
Not really. Try this, though:

{
    "request":
    {
        "method": "POST",
        "urlPattern": "/api/v2/general/someapi[\\/\\&\\?]*",
        "bodyPatterns": [
        {
"equalToJson": "{\"password\":\"password\",\"username\":\"ku...@ashar.com\"} ",
"jsonCompareMode": "LENIENT",
"ignoreArrayOrder" : true,
"ignoreExtraElements" : true
        }]
    },
    "response":
    {
        "status": 200,
        "headers":
        {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, DELETE",
            "Access-Control-Allow-Credentials": "true",
            "Access-Control-Allow-Headers": "Content-Type, X-customheader1, X-customheader2, Origin, Accept",
            "Access-Control-Expose-Headers": "Content-Type, X-customheader1, X-customheader2, Origin, Accept"
        },
        "bodyFileName": "/response.json"

Richie Loco

unread,
Sep 28, 2018, 8:41:57 AM9/28/18
to wiremock-user
I got around this by writing an extension class.  I also needed to create a mapping file expecting request method of OPTIONS. The class I then invoke with Wiremock standalone. 

Richie Loco

unread,
Sep 28, 2018, 9:09:36 AM9/28/18
to wiremock-user
Following on from my last reply (apologies that it was brief, I am swamped at work).  Please see https://github.com/RichieLoco/WiremockCorsExtension
Reply all
Reply to author
Forward
0 new messages