Monday, August 3, 2009

PERL: Testing a Web Service

Problem Statement: Testing a Web Service

Here's a small script that you can use for testing a Web Service.

#!/usr/bin/perl


use LWP::UserAgent;
use HTTP::Request;


TestWS();

sub TestWS
{

my $message = "Your Request Message";

my $userAgent = LWP::UserAgent->new();
my $request = HTTP::Request->new(POST => "Link of the WebService");
$request->header(SOAPAction => '"Soap Header that is being used"');
$request->content($message);
$request->content_type("text/xml; charset=utf-8");
my $response = $userAgent->request($request);

if($response->code == 200)
{
print "Web Service works fine";
}

else
{
print "Looks like there is a bug in the web service";
}
}


By posting the request message and getting the HTTP response back with the help of useragent would help us test the Web Service.


Getting performance of Web Service:

This can be done by keeping the method TestWS() in a for loop and logging the response in a log file along with the date.

So, keeping everything same as above, we create a file C:\WSlog.log file and run a while loop for TestWS method. Also we log the time every time we get a response as depicted in the change script below.


open (FP, ">C:\\WSlog.log");

while(1)
{
&TestWS();
}

Change in the script:
if($response->code == 200)
{
$x = gmtime();
print FP "$x";
print FP $response->as_string;

}

Analyzing the log file:
Finding the no. of requests and responses taht we have received, we can get the time frame in which a response is made from the Web Service.

Also while running this script, after sometime we would observe 500 Internal Server Error. That is the point we say teh Web Service has run out of sockets for communication. Hence we get a limit on teh no. of continous requests that can be made to a Web Service.