Monday, 29 September 2014

Test 2: Check content type and charset of webpage

Test:
Check content type and charset of webpage and display result

Learning: 
Use LWP::Simple
Use head($url) to get document headers. Returns the following 5 values if successful: ($content_type, $document_length, $modified_time, $expires, $server)


Solution:

#!/usr/bin/perl

use Test::More;
use LWP::Simple;

#define number of planned test cases
plan tests => 2; 

#URL under test
my $url = "http://www.google.com";

#getting header of webpage
my @content = head($url); 

#get content type
(my $content_type)= $content[0] =~ /(.*)\;/;

ok($content_type eq "text/html", "Content type showed $content_type");

#get charset
(my $charset)= $content[0] =~ /charset=(.*)/;

ok($charset eq "ISO-8859-1", "Charset showed $charset");


output snapshot:

No comments:

Post a Comment