在Wiremock中向Jetty添加标头 [英] Adding headers to Jetty in Wiremock

查看:175
本文介绍了在Wiremock中向Jetty添加标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Wiremock独立jar遇到CORS问题.我使用jQuery ajax调用模拟服务.启动服务器时是否可以添加必需的"Access-Control-Allow-Origin"标头?

I'm running into CORS issues using the Wiremock standalone jar. I call my mock service using jQuery ajax. Is it possible to add the required "Access-Control-Allow-Origin" header when starting up the server?

推荐答案

我通过在我的映射文件夹中为CORS预检请求添加options.json文件来使其工作

I got it to work by adding an options.json file in my mappings folder for the CORS preflight request

{
  "request" : {
    "url" : "/myurl",
    "method" : "OPTIONS"    
  },
  "response" : {
    "status" : 200,
    "headers" : {
      "Access-Control-Allow-Origin" : "http://myorigin",
      "Access-Control-Allow-Headers": "accept, content-type",
      "Access-Control-Allow-Methods": "GET, POST"
    }
  }
}

我所有其他映射都像这样

and all my other mappings look like this

{
  "request" : {
    "urlPattern" : "/myurl",
    "method" : "POST",
    "bodyPatterns" : [ {
      "equalToJson" : "{\"foo\":0}",
      "jsonCompareMode" : "LENIENT"
    } ]
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "body-file.json",
    "headers" : {
      "Access-Control-Allow-Origin" : "*"
    }
  }
}

希望有帮助

这篇关于在Wiremock中向Jetty添加标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆