2 Handling JSON Payloads - essenius/FitNesseFitSharpRest GitHub Wiki

On the previous page we discussed the different REST Request Types. Now we're going to discuss the capabitities to create, examine and manipulate JSON payloads.

Creating Json Payloads from .Net Assemblies

We already saw how we can hand-craft a payload and provide that to a service in the request. But especially for larger objects with lots of data, that would quickly become tedious and error prone to manage. Therefore, there is also a fixture that facilitates handling of objects, called Content Handler. The first thing we will show is how to create a Json object from a .Net assembly using the fixture function Create Object From Type.

This function will execute the constructor of an object with specified parameters (can be strings, integers, floats or Booleans, but no objects) and transform it into a Json object, which you can assign to a symbol. Then, you can use decision tables, table tables or query tables to examine the values of the object. Let’s have a look at the C# object:

    public class ManyTypes
    {
        public byte ByteValue ;
        public sbyte SByteValue;
        public short ShortValue;
        public ushort UShortValue;
        public int IntValue;
        public uint UIntValue;
        public long LongValue;
        public ulong ULongValue;
        public decimal DecimalValue;
        public bool BoolValue;
        public float FloatValue;
        public double DoubleValue;
        public string StringValue;
        public char CharValue;
        public string[] StringArray;
        public DateTime DateValue;
        public Uri UriValue;
        public Guid GuidValue;
        public TimeSpan TimeSpanValue;

        public ManyTypes() {}

        public ManyTypes(bool init)
        {
            if (!init) return;
            ByteValue = 255;
            SByteValue = -128;
            ShortValue = 32767;
            UShortValue = 65525;
            IntValue = 2147483647;
            UIntValue = 4294967295;
            LongValue = -223372036854775808;
            ULongValue = 8446744073709551615;
            DecimalValue = 300.5m;
            BoolValue = true;
            DoubleValue = 3.1415926535;
            FloatValue = 2.71828f;
            StringValue = "string value";
            CharValue = 'c';
            StringArray = new [] { "hi", "there"};
            DateValue = new DateTime(2015,1,1);
            UriValue = new Uri("http://localhost");
            GuidValue = new Guid();
            TimeSpanValue = new TimeSpan(2,30,0);
        }
    }

Here is how to create a Json object from this:

!|script     |Content Handler                                                                              |
|$testobject=|Create|json|object from type|RestTests.ManyTypes|in assembly|RestTests.dll|with params|[true]|

The command will instantiate a ManyTypes object by calling its constructor with a bool parameter, and will then convert that into a Json object, including all assigned values. Let’s have a look what we can do with this.

Using Decision Tables to Examine and Change Json Payloads

Here is an example of a decision table based on the Properties for Object fixture. It asserts whether the StringArray property is of type Array and contains the JSON array ["Hi","There"], whether DecimalValue is a Float with value 300.5, whether StringValue is a String with value string value, and whether DoubleValue is a Float with value of about (~) 3.14159.

!|decision: properties for object|$testobject          |
|Property                        |Type? |Value?        |
|StringArray                     |Array |["hi","there"]|
|DecimalValue                    |Float |300.5         |
|StringValue                     |String|string value  |
|DoubleValue                     |Float |~=3.14159     |

Properties For Object as decision table

You can also change the values of existing properties with this decision table. To set the value, include the Value column, but do not add the question mark.

!|decision: properties for object|$testobject     |
|Property                        |Value           |
|LongValue                       |9007199254740992|
|DecimalValue                    |450.3           |
|StringValue                     |clean code      |

Save Properties For Object as decision table

Notice how the values are no longer green - it is used as input, not as output.

Creating Json Payloads with FitNesse REST Services

FitNesse itself supports REST services as well, so you can call FitNesse services from within the REST tester fixture. One of the neat features is that you can convert a FitNesse wiki table into a Json object. Here is an example test case that uses this functionality.

First, we show the definition of the table (which we store into page RestSuite.RestTest.BodyPage):

!|Customer                        |
||name |Bob Someone               |
||id   |8402                      |
||addr                            |
||     |Street|555 Witherspoon st.|
||     |city  |Anyville           |
||     |state |IL                 |
||     |zip   |60083              |
||phone|555-1212                  |

Next, we create a Json object from that:

!|script|RestTester|http://localhost:8080                                |
|Send   |Get       |to|RestSuite.RestTest.BodyPage?packet|expect code|200|
|$body= |Response Object                                                 |

Load FitNesse Table

Using Table Tables to View Responses

Now we have created an object, we want to check whether it indeed contains the right value. To show it, you can create a table table in the following manner:

!|table: properties for object|$body|tables[0].Customer|

This will show all properties of the Json object $body under property tables[0].Customer:

Properties For Object As Table Table

Notice how we are using the same fixture Properties For Object, that we used as a Decision Table above, now as a Table Table. If the return type is Object or Array, the return value will be a Json payload. Others are string values (even if they contain numbers) without quotes. This is because the payload originated from a FitNesse page, and that conversion process does not distinguish strings or numerical values.

Using Query and Subset Query Tables to Check Responses

If you want to check values rather than show them, you can use a decision table as shown above, but you can also use a query table. The difference is that a query table returns all properties that meet the given criteria (which you can then check against) and a decision table only checks the properties that you specify. Also, you cannot use query tables to assign values, only retrieve them.

Here is an example that retrieves all fields under Customer.addr and checks them against expected values:

!|query: properties for object |$body |tables[0].Customer.addr.*|
|Property                      |Type  |Value                    |
|tables[0].Customer.addr.zip   |String|60083                    |
|tables[0].Customer.addr.Street|String|555 Witherspoon st.      |
|tables[0].Customer.addr.state |String|IL                       |
|tables[0].Customer.addr.city  |String|Anyville                 |

Properties For Object As Query Table

You can also use queries to check for omissions. E.g. if you delete the line with addr.zip from the query table, then you get:

Properties For Object As Query Table with Surplus

In other words, FitNesse discovered that there is a property that you didn’t specify, and it fails the test because of that. If you don’t want to fail the test in case of surpluses, you use the subset query:

Properties For Object As Subset Query Table

Now the test passes, despite that it didn’t specify the zip property.

We now know how we can handle Json payloads. Let's have a look how it works with XML.