Working on extending the functionality and test suite.
authorChris Cormack <chrisc@catalyst.net.nz>
Wed, 18 Sep 2013 01:30:20 +0000 (13:30 +1200)
committerChris Cormack <chrisc@catalyst.net.nz>
Wed, 18 Sep 2013 01:30:20 +0000 (13:30 +1200)
Parsing the xml using XML::Libxml and trapping errors

Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
lib/NCIP.pm
t/NCIP.t

index 6bd9c58..b8bc2ce 100644 (file)
@@ -1,34 +1,79 @@
 package NCIP;
 use NCIP::Configuration;
 use Modern::Perl;
+use XML::LibXML;
+use Try::Tiny;
+
 use base qw(Class::Accessor);
 
+our $VERSION = '0.01';
+
+=head1 NAME
+  
+    NCIP
+
+=head1 SYNOPSIS
+
+    use NCIP;
+    my $nicp = NCIP->new($config_dir);
+
+=head1 FUNCTIONS
+
+=cut
+
 sub new {
-    my $proto = shift;
-    my $class = ref $proto || $proto;
+    my $proto      = shift;
+    my $class      = ref $proto || $proto;
     my $config_dir = shift;
-    my $self = {};
-    my $config = NCIP::Configuration->new($config_dir);
+    my $self       = {};
+    my $config     = NCIP::Configuration->new($config_dir);
     $self->{config} = $config;
     return bless $self, $class;
 
 }
 
+=head2 process_request()
+
+ my $response = $ncip->process_request($xml);
+
+=cut
+
 sub process_request {
     my $self = shift;
-    my $xml = shift;
-    
+    my $xml  = shift;
+
     my $request_type = $self->handle_initiation($xml);
-    my $response = "<HTML> <HEAD> <TITLE>Hello There</TITLE> </HEAD> <BODY> <H1>Hello You Big JERK!</H1> Who would take this book seriously if the first eaxample didn't say \"hello world\"?  </BODY> </HTML>";
+    unless ($request_type) {
+
+      # We have invalid xml, or we can't figure out what kind of request this is
+      # Handle error here
+    }
+    my $response =
+"<HTML> <HEAD> <TITLE>Hello There</TITLE> </HEAD> <BODY> <H1>Hello You Big JERK!</H1> Who would take this book seriously if the first eaxample didn't say \"hello world\"?  </BODY> </HTML>";
 
     return $response;
 }
 
+=head2 handle_initiation
+
+=cut
+
 sub handle_initiation {
     my $self = shift;
-    my $xml = shift;
-
-    return('lookup_item');
+    my $xml  = shift;
+    my $dom;
+    try {
+        $dom = XML::LibXML->load_xml( string => $xml );
+    }
+    catch {
+        warn "Invalid xml, caught error: $_";
+    };
+    if ($dom) {
+        return ('lookup_item');
+    }
+    else {
+        return;
+    }
 }
 
 1;
index fd18414..74422cb 100644 (file)
--- a/t/NCIP.t
+++ b/t/NCIP.t
@@ -18,7 +18,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 3;    # last test to print
+use Test::More tests => 5;    # last test to print
 
 use lib 'lib';
 
@@ -32,3 +32,14 @@ EOT
 
 ok( my $response = $ncip->process_request($xml), 'Process a request' );
 
+my $xmlbad = <<'EOT';
+<xml>
+this is bad
+<xml>
+</xml>
+EOT
+
+# handle_initiation is called as part of the process_request, but best to test
+# anyway
+ok( !$ncip->handle_initiation($xmlbad), 'Bad xml' );
+ok( $ncip->handle_initiation($xml),     'Good XML' );