简体   繁体   English

如何指定可选的查询参数

[英]How can I specify optional Query Parameters

I defined query parameter in my contract. 我在合同中定义了查询参数。 I need this parameter to be optional: 我需要这个参数是可选的:

method 'GET'
    url($(regex(urlRegex))) {
        queryParameters {
            parameter 'fitler': $(stub(regex(filterRegex)))
        }
}

I want this contract to be suitable for the both URLs with filter like /my/sample/url?fitler=some-filter-expression and without the filter param like /my/sample/url . 我希望这个合约适用于带有像/my/sample/url?fitler=some-filter-expression这样的过滤器的两个URL,而不需要像/my/sample/url这样的过滤器参数。

How can I achieve this? 我怎样才能做到这一点? Is this even possible? 这甚至可能吗?

So far, this has no explicit way defined in WireMock spec. 到目前为止,这在WireMock规范中没有明确的定义方式。 However, you have a workaround using regex, by specifying the URL using urlPathPattern property (in JSON stubbing). 但是,您可以使用正则表达式进行解决方法,方法是使用urlPathPattern属性指定URL(在JSON存根中)。 Refer to the example below. 请参阅下面的示例。

{
    "request": {
        "method": "GET",
        "urlPathPattern": "/myapp/users(\\?((a-zA-Z\\d\\_\\-)+\\=(a-zA-Z\\d\\_\\-)+)(\\&(a-zA-Z\\d\\_\\-)+\\=(a-zA-Z\\d\\_\\-)+)+)?"
    },
    "response": {
        "status": 200,
            "bodyFileName": "users.json",
            "headers": {
            "Content-Type": "application/json"
        }
    }
}

Observe the optional portion at the end of the URL, which looks for the typical URL query structure. 观察URL末尾的可选部分,该部分查找典型的URL查询结构。 This, I have tried out in wiremock and it runs smoothly. 这个,我已经尝试过在线索中运行顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM