[英]mocking a request with a payload using wiremock
我目前正在嘗試使用 Wiremock 模擬外部服務器。 我的一個外部服務器端點需要一個有效負載。 該端點定義如下:
def sendRequestToMockServer(payload: String) = {
for {
request_entity <- Marshal(payload).to[RequestEntity]
response <- Http().singleRequest(
HttpRequest(
method = HttpMethods.GET,
uri = "http://localhost:9090/login",
entity = request_entity
)
)
} yield {
response
}
}
為了使用 Wiremock 模擬這個端點,我編寫了以下代碼:
stubFor(
get(urlEqualTo("/login"))
.willReturn(
aResponse()
.withHeader("Content-Type","application/json")
.withBodyFile("wireMockResponse.json")
.withStatus(200)
)
.withRequestBody(matchingJsonPath("requestBody.json"))
)
我在 requestBody.json 文件中定義了請求正文。
但是當我運行測試時,我不斷收到一條錯誤消息,指出找不到請求的 Url。
我認為該錯誤與這一行有關withRequestBody(matchingJsonPath("requestBody.json"))
,因為當我評論它時,錯誤消失了。
關於如何解決這個問題的任何建議?
matchingJsonPath
不會在提供的文件路徑中填充文件,而是評估提供的JsonPath
。 請參閱文檔。
我不完全確定是否有辦法將請求正文作為 .json 文件提供。 如果將文件的內容復制到withRequest(equalToJson(_yourJsonHere_))
中,它可以工作嗎? If it does, you could get the file contents as a JSON string above the definition and provide it to the function (or I guess, make a function to return a JSON string from a.json file).
此外,您可以創建一個自定義請求匹配器來為您進行解析。 我想只有在上述方法不起作用時我才會推薦這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.