Forum Discussion

Behdad's avatar
Behdad
Contributor
4 years ago

Control request characters

Hi 

I am new user using readyapi 3.5.0   πŸ™‚

I have some rest apis with Josn request to test , i need to control the format of every keys value (with string type) in Json request with two main concern:

1- make sure that all the values meet UTF8 encoding standard

2- Check if the characters on the key is only contain these characters

a-zA-Z0-9@+_-.:!#$%&*/=?^_{|}~;

 

in this case i am going to create test step but i don't know how can i control above concerns ?

  • Hey Behdad,

    My answer to the first part is a bit of a guess, but asserting the charset is kindof like asserting the response IS json.
    If i can remember correctly, i believe the RFCs that mention json indicate that all json decoders must support utf-8, so i suggest utf-8 is the default. If it deviates from this i'd hope that the Content-Type header would include some additional detail to the standard 'application/json', so it it was iso-8859-1, i think youd get 'application/json; iso-8859-1' or something equivalent. As far as i'm aware the charset isnt included in the json payload body unlike xml which can include the char encoding in the xml declaration (the first line at the top).


    In regards to asserting every attribute value can only have characters made from a list of characters (that you specified in your message), I think the contains assertion is just for xml, not json so i think you need to use groovy to assert a json attribute has a specific pattern/character list.

    Doing that isnt too difficult except the variation in length. I can assert in groovy that a certain attribute value has a specific pattern and length, i can assert multiple instances of the same attribute has the same pattern and length, but i dont know how to assert the pattern content without specifying the length.
    Even if i got passed the length issue, i would have to include 1 assertion for each uniquely named attribute, cos i dont know how to assert every single attribute value contains only specific characters.

    Perhaps one of the coders (@HimanshuTayal?, ChrisA?, etc.) can help?

    Ta

    Rich
  • richie's avatar
    richie
    Community Hero
    Hey Behdad,

    My answer to the first part is a bit of a guess, but asserting the charset is kindof like asserting the response IS json.
    If i can remember correctly, i believe the RFCs that mention json indicate that all json decoders must support utf-8, so i suggest utf-8 is the default. If it deviates from this i'd hope that the Content-Type header would include some additional detail to the standard 'application/json', so it it was iso-8859-1, i think youd get 'application/json; iso-8859-1' or something equivalent. As far as i'm aware the charset isnt included in the json payload body unlike xml which can include the char encoding in the xml declaration (the first line at the top).


    In regards to asserting every attribute value can only have characters made from a list of characters (that you specified in your message), I think the contains assertion is just for xml, not json so i think you need to use groovy to assert a json attribute has a specific pattern/character list.

    Doing that isnt too difficult except the variation in length. I can assert in groovy that a certain attribute value has a specific pattern and length, i can assert multiple instances of the same attribute has the same pattern and length, but i dont know how to assert the pattern content without specifying the length.
    Even if i got passed the length issue, i would have to include 1 assertion for each uniquely named attribute, cos i dont know how to assert every single attribute value contains only specific characters.

    Perhaps one of the coders (@HimanshuTayal?, ChrisA?, etc.) can help?

    Ta

    Rich
    • Behdad's avatar
      Behdad
      Contributor

      Hi 

      Many thanks for your detail answer & descriptions

      1- Concerning the first issue (UTF8) i believe on your thought too , but using non UTF8 characters in string values would not harm the Json format and it is still valid Json (like: "org_name": "‑₧Β₯ΒΊ")

      So i need to figure out how can i handle this issue and send proper response as invalid characters !

       

      2- Refer to second issue if i want to do the controlling  assertion one by one , that would be so long ! so  there should be a way and if you can handle it with groovy script with length limitation , that is super and i would be appreciate if you gave me  sample script ?