Showing posts with label soap. Show all posts
Showing posts with label soap. Show all posts

Saturday, June 11, 2016

How to web service client built in Perl for testing purpose?

Sometimes we are in a server terminal and we need to test webservice behavior, so based in what we have running scripts in perl. 

Option 1: One file of test


1. The client program: 

#!/usr/bin/perl -w
#author:Sanchez Octavio
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
my $datestring = localtime();
print("1.SOAP CLIENT TEST - SERVICIO HR EMPLOYEES\n");
print("2.Time test:" , $datestring , "\n");
my $userAgent = LWP::UserAgent->new(agent => 'perl post');
my $endpoint = 'http://127.0.0.1:8080/employees-hr-ws-web/EmployeesHRServiceHttpSoap11Endpoint';
my $soapaction = '\"urn:consultarSalariesPorRango\"';
my $message = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:fac=\"http://facade.web.ws.hr.employees.osanchezhuerta.org/\" xmlns:xsd=\"http://domain.web.ws.hr.employees.osanchezhuerta.org/xsd/\">
<soapenv:Header/>
<soapenv:Body>
<fac:consultarSalariesPorRango>
<fac:governanceHeaderRequest>
<xsd:hrHeader>
<xsd:contextoEntidad></xsd:contextoEntidad>
<xsd:fechaPeticion></xsd:fechaPeticion>
<xsd:folio>1</xsd:folio>
<xsd:idCanal>1</xsd:idCanal>
<xsd:idEntidad>1</xsd:idEntidad>
<xsd:idOperacion>1</xsd:idOperacion>
<xsd:idOrganizacion>1</xsd:idOrganizacion>
<xsd:idServicio>1</xsd:idServicio>
<xsd:idSistema>1</xsd:idSistema>
<xsd:idZona>1</xsd:idZona>
</xsd:hrHeader>
</fac:governanceHeaderRequest>
<fac:consultarSalariesPorRangoInput>
<fac:lstEmpNo>
<xsd:empNo>10001</xsd:empNo>
</fac:lstEmpNo>
<fac:pageNumber>2</fac:pageNumber>
<fac:pageSize>2</fac:pageSize>
</fac:consultarSalariesPorRangoInput>
</fac:consultarSalariesPorRango>
</soapenv:Body>
</soapenv:Envelope>";
print "3. Web service Request:" , $message , "\n";
my $response = $userAgent->request(POST $endpoint,
Content_Type => 'text/xml;charset=UTF-8',
SOAP_Action=> $soapaction,
Accept_Encoding => 'gzip,deflate',
Content => $message);
print "4. Web service Fault:" , $response->error_as_HTML unless $response->is_success , "\n";
print "5. Web service Response:" , $response->as_string , "\n";
print "6. End of test" , "\n";

2. Execution line: 

$ perl soap_action_ws_test.pl > soap_action_ws_test.log

Option 2: With external file:

1. Request XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fac="http://facade.web.ws.hr.employees.osanchezhuerta.org/" xmlns:xsd="http://domain.web.ws.hr.employees.osanchezhuerta.org/xsd/">
<soapenv:Header/>
<soapenv:Body>
<fac:consultarSalariesPorRango>
<fac:governanceHeaderRequest>
<xsd:hrHeader>
<xsd:contextoEntidad></xsd:contextoEntidad>
<xsd:fechaPeticion></xsd:fechaPeticion>
<xsd:folio>1</xsd:folio>
<xsd:idCanal>1</xsd:idCanal>
<xsd:idEntidad>1</xsd:idEntidad>
<xsd:idOperacion>1</xsd:idOperacion>
<xsd:idOrganizacion>1</xsd:idOrganizacion>
<xsd:idServicio>1</xsd:idServicio>
<xsd:idSistema>1</xsd:idSistema>
<xsd:idZona>1</xsd:idZona>
</xsd:hrHeader>
</fac:governanceHeaderRequest>
<fac:consultarSalariesPorRangoInput>
<fac:lstEmpNo>
<xsd:empNo>10001</xsd:empNo>
</fac:lstEmpNo>
<fac:pageNumber>2</fac:pageNumber>
<fac:pageSize>2</fac:pageSize>
</fac:consultarSalariesPorRangoInput>
</fac:consultarSalariesPorRango>
</soapenv:Body>
</soapenv:Envelope>

2. The client program:

#!/usr/bin/perl -w
#author:Sanchez Octavio
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
my $datestring = localtime();
print("0. Begin test");
print("1. SOAP CLIENT TEST - SERVICIO HR EMPLOYEES\n");
print("2. Time test:" , $datestring , "\n");
#Required input data
my $filename = 'consultarSalariesPorRangoRequest.xml';
my $endpoint = 'http://127.0.0.1:8080/employees-hr-ws-web/EmployeesHRServiceHttpSoap11Endpoint';
my $soapaction = '\"urn:consultarSalariesPorRango\"';
open(my $fileopenrequest, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
my $data = do { local $/; <$fileopenrequest> };
my $userAgent = LWP::UserAgent->new(agent => 'perl post');
my $message = $data;
print "3. Web service Request:" , $message , "\n";
my $response = $userAgent->request(POST $endpoint,
Content_Type => 'text/xml;charset=UTF-8',
SOAP_Action=> $soapaction,
Accept_Encoding => 'gzip,deflate',
Content => $message);
print "4. Web service Fault:" , $response->error_as_HTML unless $response->is_success , "\n";
print "5. Web service Response:" , $response->as_string , "\n";
print "6. End of test" , "\n";

3. Execution line: 

$ perl soap_action_ws_test_2.pl > soap_action_ws_test_2.log

Option 3: Load external file by command line

1. The client program:


#!/usr/bin/perl -w
#author:Sanchez Octavio
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
my $datestring = localtime();
print("0. Begin test");
print("1. SOAP CLIENT TEST - SERVICIO HR EMPLOYEES\n");
print("2. Time test:" , $datestring , "\n");
my $num_args = $#ARGV+1;
if ($num_args < 1) {
print "\n2.1. Usage: soap_action_ws_test_3.pl (complete location)\n";
exit 0;
}
my $filename = $ARGV[0];
print "2.2. Loading file:" , $filename , "\n";
#Required input data
my $endpoint = 'http://127.0.0.1:8080/employees-hr-ws-web/EmployeesHRServiceHttpSoap11Endpoint';
my $soapaction = '\"urn:consultarSalariesPorRango\"';
open(my $fileopenrequest, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!";
my $data = do { local $/; <$fileopenrequest> };
my $userAgent = LWP::UserAgent->new(agent => 'perl post');
my $message = $data;
print "3. Web service Request:" , $message , "\n";
my $response = $userAgent->request(POST $endpoint,
Content_Type => 'text/xml;charset=UTF-8',
SOAP_Action=> $soapaction,
Accept_Encoding => 'gzip,deflate',
Content => $message);
print "4. Web service Fault:" , $response->error_as_HTML unless $response->is_success , "\n";
print "5. Web service Response:" , $response->as_string , "\n";
print "6. End of test" , "\n";
 

2. Execution line: 

$ perl soap_action_ws_test.pl  /home/user/code/perl/consultarSalariesPorRangoRequest.xml > soap_action_ws_test.log

Important sources:
Tested on:

  • Centos
  • HP-UX
  • Ubuntu 
Best regards,