Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How to enforce all JSON values of a request payload at once.

masdeval
masdeval Posts: 3

Hi guys.

I want to create a responder that will fail if any of the fields or values that come in a request is different from what was defined as the Request Template. How can I achieve this?

I have been trying using HTTP Simple Mock and JSON Assertors but I couldn`t get this behavior yet.

Best Answer

  • OmarR
    OmarR Posts: 233 admin
    Answer ✓

    An alternative would be to use scripting in custom correlation and compare the entire request payload as a single string.

    Below is an example (Right-click>View Image):

    import com.parasoft.api.CorrelationScriptingHookConstants
    def correlateOperation(context) {
        requestBodyStr = context.get(CorrelationScriptingHookConstants.MESSAGE_STR)     // String
        str1=requestBodyStr.replaceAll("[\\s]","")
        return str1.contains('{"glossary":{"title":"exampleglossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"StandardGeneralizedMarkupLanguage","Acronym":"SGML","Abbrev":"ISO8879:1986","GlossDef":{"para":"Ameta-markuplanguage,usedtocreatemarkuplanguagessuchasDocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}')
    }
    

    For simplicity, I remove all white space characters using regex before comparison. Give this a try.

Answers

  • OmarR
    OmarR Posts: 233 admin

    How big is the request payload?
    One way to do this is to use responder correlation or data source correlation and add each field individually to request body correlation. However, this may be tedious for larger payloads.

  • masdeval
    masdeval Posts: 3

    Hi Omar. This is the point, the payload is big. Configuring individually is exactly what I am trying to avoid. I would expect that what I need would be a common requirement but still did not find a solution. Any other lead?

  • masdeval
    masdeval Posts: 3

    Yeah, it worked! Thank you.