2017-02-23 94 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是奇特的,當涉及到的間距,在這裏我用一個空格代替標籤,希望它幫助。