In previous blogs, we have touched on JSON messages processing. Here we want to talk more about it partly because JSON has become the standard messaging format between clients and servers, you will be guaranteed see more of it in performance testing.
We showed how easy it is to parse JSON message and access any of its fields on netgend platform . What about JSONP message?
First let's find out what JSONP message is. JSONP message can be seen as a workaround for browsers' single origin policy (SOP): to get some data from another web server (which is prohibited by SOP), client pretends to ask the web server for a piece of javascript code (not restricted by the SOP), the web server will "pretent" to send you a piece of javascript code which is actually a JSON message wrapped in a simple javascript statement or function call. Clever, isn't it?
A simple JSONP message looks like the following. In practice, the JSONP message can be much more complex.
parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
Now we know a JSONP message is closely related to a JSON message, how do we process an incoming JSONP message in performance testing? Of course, you can use some regexp kungfu to extract the JSON message from a JSONP message and call "fromJson(..)" function on it. It's not necessary though, the implementation of the function "fromJson(..)" will detect whether the message is a JSONP message, if so, it will extract the JSON part and parse it. In another word, "fromJson(..)" function works both on JSON messages and JSONP messages.
action(http, "http://www.example2.com/hotels/Home/CityAddress?IsUseNewStyle=T&keyword=shanghai");
a = fromJson(http.replyBody);
//the http.replyBody may be a JSONP message
//now you can access fields in the JSONP message and use it to fill a html form
form.destinationCity.value = a.suggestion.fullName;
So far, we have talked about processing incoming JSON message, what about generating a JSON message to send to a server? In that case, we can use toJson() function.
a.city = "Chicago";
a.name = "Mary Dole";
a.age = "23";
msg = toJson(a);
//msg will contain string
//{"city": "Chicago", "name": "Mary Dole", "age": 23}
toJson() function is nice, but sometimes we need to generate a JSON message based on a JSON message template, like the following.
{
"city":"${city}",
"name":"${name}",
"age":${age}
}
Many testing platform supports this. However, you have to escape all the double quotes in the template: {
\"city\":\"${city}\",
\"name\":\"${name}\",
\"age\":23
}
This JSON template is very short and it's already tedious to escape all the double quotes. You will be delighted to know that on netgend platform, you don't have to do that. city = "Chicago";
name = "Mary Dole";
age = 23;
str = q|{
"city":"${city}",
"name":"${name}",
"age":${age}
}|;
http.POSTData = str;
action(http, "http://www.example.com/registration?")
In the above, all the variable substitutions between "q|" and "|" will happen as expected, all space, newline and double quotes are preserved,Processing incoming JSON message and generating outgoing JSON message can be a headache on other testing platforms, but on netgend platform, it's a pleasure.
No comments:
Post a Comment