Wiremock响应模板以使用作为请求查询参数传递的JSON。

huangapple go评论91阅读模式
英文:

Wiremock response template to use json passed in as request query parameter

问题

以下是翻译的部分:

{
  "response": {
    "jsonBody": {
      "logins": "{{request.query.Logins}}",
      "jsonPathLogins": "{{jsonPath request.query.Logins '$'}}",
      "jsonPathLogins0": "{{jsonPath request.query.Logins '$.[0]'}}",
      "jsonPathQueryLogins": "{{jsonPath request.query '$.Logins'}}",
      "jsonPathQueryLogins0": "{{jsonPath request.query '$.Logins.[0]'}}",
      "loginsCut": "{{cut request.query.Logins.[0] '\"'}}",
      "loginsCutQuot": "{{cut request.query.Logins.[0] '"'}}",
      "loginsReplace": "{{replace request.query.Logins.[0] '\"' '*'}}",
      "loginsReplaceQuot": "{{replace request.query.Logins.[0] '"' '*'}}"
    }
  }
}

注意:翻译中的双引号已经修复为正常的 JSON 双引号。

英文:

Provided with the following request:

http://myserver:8080/api/userinfo?Logins=%5B%22admin%40my.domain%22%5D

How would I get the first value in urlencoded-json array of values found in Logins query parameter to output in Wiremock response templating?

Following are my attempts:

{
  "response": {
    "jsonBody": {
      "logins": "{{request.query.Logins}}",
      "jsonPathLogins": "{{jsonPath request.query.Logins '$'}}",
      "jsonPathLogins0": "{{jsonPath request.query.Logins '$.[0]'}}",
      "jsonPathQueryLogins": "{{jsonPath request.query '$.Logins'}}",
      "jsonPathQueryLogins0": "{{jsonPath request.query '$.Logins.[0]'}}",
      "loginsCut": "{{cut request.query.Logins.[0] '\"'}}",
      "loginsCutQuot": "{{cut request.query.Logins.[0] '"'}}",
      "loginsReplace": "{{replace request.query.Logins.[0] '\"' '*'}}",
      "loginsReplaceQuot": "{{replace request.query.Logins.[0] '"' '*'}}"
    }
  }
}

and my response is always the same:

{
  "logins": "["admin@my.domain"]",
  "jsonPathLogins": "["admin@my.domain"]",
  "jsonPathLogins0": "["admin@my.domain"]",
  "jsonPathQueryLogins": "["admin@my.domain"]",
  "jsonPathQueryLogins0": "["admin@my.domain"]",
  "logins": "["admin@my.domain"]",
  "loginsCut": "["admin@my.domain"]",
  "loginsCutQuot": "["admin@my.domain"]",
  "loginsReplace": "["admin@my.domain"]",
  "loginsReplaceQuot": "["admin@my.domain"]"
}

What am I missing? How do I combine those methods to unwrap my value from that double JSON encoding (or somehow strip wrapping symbols)?

答案1

得分: 1

以下是翻译好的代码部分:

{
  "response": {
    "jsonBody": {
      "logins": "{{replace(replace(replace(replace(urlEncode request.query.Logins decode=True) '%22' '') '%5B' '') '%5D' '') '%40' '@'}}"
    }
  }
}
{
  "response": {
    "jsonBody": {
      "login": "{{regexExtract request.query.Logins '\\b(.+?@.+)\\b' 'parts'}}{{join parts ' '}}"
    }
  }
}
英文:

So far what I've come up with is the following wrapper:

{
  "response": {
    "jsonBody": {
      "logins": "{{replace(replace (replace (replace (urlEncode request.query.Logins decode=True) '%22' '') '%5B' '') '%5D' '') '%40' '@'}}"
    }
  }
}

It looks awful and prone to edge cases, but work for some simple ones. I would gladly accept an answer which does not rely on replacing urlEncoded sequences directly the way I did it.

EDIT:
Here's another regex-based solution which works in my email-like case and looks a bit cleaner, but would fail in any other case:


{
  "response": {
    "jsonBody": {
      "login": "{{regexExtract request.query.Logins '\b(.+?@.+)\b' 'parts'}}{{join parts ' '}}",
    }
  }
}

huangapple
  • 本文由 发表于 2023年7月11日 01:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定