Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching data in JsonPath with wiremock

I'm trying to create mocks for my login procedure. I use POST method with a couple of fields and login object (with login, password, etc.) For that I'm using JsonPath. Code below:

{
"request": {
        "method": "POST",
        "url": "/login",
        "bodyPatterns" : [
                {"matchesJsonPath" : "$.method"},
                {"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},
                {"matchesJsonPath" : "$.params.login"},
                {"matchesJsonPath" : "$.params.password"}
         ]
    },
    "response": {
            "status": 200,
            "bodyFileName": "login.json"
    }
}

I'm checking the clientVersion because it's similar to the examples.

My problem is, that with te given POST JSON:

{
    "method": "login",
    "params": {
        "clientVersion": "1",
        "login": "[email protected]",
        "password": "681819535da188b6ef2"
    }
}

I receive 404. However, when I change

{"matchesJsonPath" : "$.params[?(@.clientVersion == "1")]"},

to normal

{"matchesJsonPath" : "$.params.clientVersion"},

everything works just fine.

So - how to check inside wiremock, using matchesJsonPath if given field equals some value? How to apply it to the root field like method in my case? And while we're at it - I had similar problems with checking if the value is not null. I tried to apply regular expressions and such - no luck.

like image 875
a_dzik Avatar asked Feb 23 '15 10:02

a_dzik


Video Answer


2 Answers

It's working in my case :

wiremock:

"request": {
"urlPathPattern": "/api/authins-portail-rs/authins/inscription/infosperso",
"bodyPatterns" : [
  {"matchesJsonPath" : "$[?(@.nir == '123456789')]"},
  {"matchesJsonPath" : "$[?(@.nomPatronyme == 'aubert')]"},
  {"matchesJsonPath" : "$[?(@.prenoms == 'christian')]"},
  {"matchesJsonPath" : "$[?(@.dateNaissance == '01/09/1952')]"}

],
"method": "POST"

}

Json:

{
    "nir": "123456789",
    "nomPatronyme": "aubert",
    "prenoms": "christian",
    "dateNaissance": "01/09/1952"
}
like image 59
BM Marwa Avatar answered Sep 21 '22 07:09

BM Marwa


Following worked for me.

"matchesJsonPath" : "$.rootItem.itemA[0].item..[?(@.fieldName=='file')]"

Json :

{
    "rootItem" :  {
          "itemA" : [
              {
                 "item" : {
                     "fieldName" : "file",
                     "name" : "test"
                 }
              }
          ]
    }
}

Wiremock

{
  "request" : {
    "urlPattern" : "/testjsonpath",
    "method" : "POST",
    "bodyPatterns" : [ {
      "matchesJsonPath" : "$.rootItem.itemA[0].item..[?(@.fieldName=='file')]"
    } ]
  },
  "response" : {
    "status" : 200,
    "body" : "{\"result\": \"success\"}",
    "headers" : {
      "Content-Type" : "application/json"
    }
  }
}
like image 22
user3485561 Avatar answered Sep 23 '22 07:09

user3485561