2017-02-23 93 views
2

我已经有效地使用了wiremock一段时间了,我想让CORS访问模拟的API。Wiremock CORS不工作

我试着在响应头中设置Access-Control-Allow-Origin:*和其他头文件,都无济于事。

下面是我有一个映射的例子:

{ 
    "request": { 
     "method": "POST", 
     "urlPattern": "/api/v2/general/login[\\/\\&\\?]*", 
     "bodyPatterns": [{ 
      "equalToJson": "{\"password\":\"password\",\"username\":\"[email protected]\"} ", 
      "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_johncougar.json" 
    } 
} 

我在做什么错在这里是造成CORS不行?

在此先感谢。

+0

你正在做什么样的要求?如果它不是GET,你需要实现一个带有一些CORS头的OPTIONS存根。 – Tom

+0

@Tom,我正在做GET和POST请求。 你的意思是“用一些CORS头实现一个OPTIONS存根”? – johncougar

+0

CORS协议要求浏览器在发出OPTIONS“印前检查”请求之前,检查它们在做出符合特定条件的请求之前可以执行的操作。值得一看https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests – Tom

回答

1

这里是一个样本,这个工程

{ 
"request": 
{ 
"urlPattern": "/country/([a-z]*)", 
"method": "GET" 
}, 

"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" 
}, 
"body": "{ \"statusCode\" : \"S1000\", \"statusDescription\" : \"Success\", \"content\" : [ { \"id\" : \"1111\", \"name\" : \"aaaa\"}, { \"id\" : \"2222\", \"name\" : \"asd\" } ] }" 
} 
} 

使用这个,因为它是,wiremock是奇特的,当涉及到的间距,在这里我用一个空格代替标签,希望它帮助。