第三节 wireMock的json的URL的匹配方式、传参方式、header和返回值

本文详细介绍了WireMock中URL的匹配方式,包括绝对匹配、正则匹配以及带参数的情况。同时,讲解了POST请求的多种传参方式,如Body方式1至8。此外,还讨论了header的设置以及返回值的处理,包括返回字符串、文件内容等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、URL的匹配方式

1)绝对匹配带参数:http://localhost:9999/your/url?and=query

{
    "request": {
        "method": "GET",
        "url": "/your/url?and=query"
    },
    "response": {
        "status": 200,
        "body": "/your/url?and=query\n"
    }
}

2)绝对匹配不带参数:http://localhost:9999/your/urlPath

{
    "request": {
        "method": "GET",
        "urlPath": "/your/urlPath"
    },
    "response": {
        "status": 200,
        "body": " urlPath: /your/urlPath"
    }
}

3)带参数,正则匹配:http://localhost:9999/your/urlPattern/jhkj?and=query

{
    "request": {
        "method": "GET",
        "urlPattern": "/your/urlPattern/([a-z]*)\\?and=query"
    },
    "response": {
        "status": 200,
        "body": "/your/urlPattern/([a-z]*)\\?and=query"
    }
}

4)不带参数,正则匹配:http://localhost:9999/your/urlPathPattern/sdfdsf

{
    "request": {
        "method": "GET",
       "urlPathPattern": "/your/urlPathPattern/([a-z]*)"
    },
    "response": {
        "status": 200,
        "body": "/your/urlPathPattern/([a-z]*)"
    }
}

5)单独传参数:http://localhost:9999/api/products/queryParameters/NoRegular?search=chin

{
"priority": 1,
    "request": {
        "method": "GET",
        "urlPath": "/api/products/queryParameters/NoRegular",
        "queryParameters": {
            "search": {
                "contains": "chin"
            }
 	
        }
    },
    "response": {
        "status": 200,
        "headers": {
            "Content-Type": "application/json"
        },
 	"body": "{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China\" }"
    
}
}

6)单独传参数,正则参数:http://localhost:9999/api/products/queryParameters/Regular?search_term=aswiremocka

{
    "request": {
     	   "method": "GET",
      	      "url": "/your/queryParameters",
 "queryParameters" : {
     "search_term" : {
     		   "matches" : "^(.*)wiremock([A-Za-z]+)$"
     		 }
   	 }
    },
    "response": {
        "status": 200,
        "body": "dsfsdsdfsdf"
    }
}

二、POST的传参方式

1)Body方式1:

接口:http://localhost:9999/api/products/imput/body/JSON

Body:{"total_results": 4}

返回:/api/products/imput/body/JSON.</

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值