Skip to content

Commit

Permalink
Item2321: perltidy
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/RestPlugin@10135 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Dec 1, 2010
1 parent f7bfa9c commit c30641a
Showing 1 changed file with 56 additions and 40 deletions.
96 changes: 56 additions & 40 deletions test/unit/RestPlugin/RestPluginCurlTests.pm
Expand Up @@ -3,7 +3,6 @@ use FoswikiFnTestCase;
our @ISA = qw( FoswikiFnTestCase );
use strict;


use Foswiki ();
use Foswiki::Func();
use Foswiki::Meta ();
Expand All @@ -15,13 +14,16 @@ sub new {
return $self;
}


sub list_tests {
my ( $this, $suite ) = @_;
my @set = $this->SUPER::list_tests($suite);

my $curlError = `curl 2>&1`;
if (not( $curlError =~ /curl: try 'curl --help' or 'curl --manual' for more information/)) {
if (
not( $curlError =~
/curl: try 'curl --help' or 'curl --manual' for more information/ )
)
{
print STDERR
"curl not installed and in path, skipping curl based tests\n";
return;
Expand Down Expand Up @@ -59,72 +61,86 @@ typically, a spade made with a thorny handle is functional, but not ideal.
}

sub callCurl {
my $self = shift;
my $httpRequest = shift;
my $self = shift;
my $httpRequest = shift;
my $requestContentType = shift;
my $requestContent = shift;
my $url = shift;

#curl -X PATCH -H "Content-Type:text/json" -d '{"_text": "set the topic text to something new again"}' http://x61/f/bin/query/Main/SvenDowideit/topic.json
#curl -X PATCH -H "Content-Type:text/json" -d '{"_text": "set the topic text to something new again"}' http://x61/f/bin/query/Main/SvenDowideit/topic.json
my $curlCommand = 'curl -s --trace-time -v -X '.$httpRequest.' -H "Content-Type:'.$requestContentType.'" -d \''.$requestContent.'\' '.$url;
my $result = `$curlCommand 2>&1`; # grrrr, aparently they output the header info into stderr

my $requestContent = shift;
my $url = shift;

#curl -X PATCH -H "Content-Type:text/json" -d '{"_text": "set the topic text to something new again"}' http://x61/f/bin/query/Main/SvenDowideit/topic.json
#curl -X PATCH -H "Content-Type:text/json" -d '{"_text": "set the topic text to something new again"}' http://x61/f/bin/query/Main/SvenDowideit/topic.json
my $curlCommand =
'curl -s --trace-time -v -X '
. $httpRequest
. ' -H "Content-Type:'
. $requestContentType
. '" -d \''
. $requestContent . '\' '
. $url;
my $result = `$curlCommand 2>&1`
; # grrrr, aparently they output the header info into stderr

#now separate into headers and content.
#14:48:54.853392 * Connected to x61 (127.0.1.1) port 80 (#0)
#14:48:54.853517 > GET

my $data = {'*' => '','>' => '','<' => '','{' => ''};
$result =~ s/(\d\d:\d\d:\d\d\.\d\d\d\d\d\d) ([<*>{]) (.*)(\r\n)/appendToSection($1, $2, $3, $data)/gem;
#argh. \r?\n isn't working for me, so i'll just run the regex twice until i get enough sleep to have a clue.
$result =~ s/(\d\d:\d\d:\d\d\.\d\d\d\d\d\d) ([<*>{]) (.*)(\n)/appendToSection($1, $2, $3, $data)/gem;


my $data = { '*' => '', '>' => '', '<' => '', '{' => '' };
$result =~
s/(\d\d:\d\d:\d\d\.\d\d\d\d\d\d) ([<*>{]) (.*)(\r\n)/addToResultHash($1, $2, $3, $data)/gem;

#argh. \r?\n isn't working for me, so i'll just run the regex twice until i get enough sleep to have a clue.
$result =~
s/(\d\d:\d\d:\d\d\.\d\d\d\d\d\d) ([<*>{]) (.*)(\n)/addToResultHash($1, $2, $3, $data)/gem;

#print STDERR "\n================\n$result\n==========\n";
#print STDERR join("\n-----------------\n", ($result, $data->{'*'}, $data->{'>'}, $data->{'<'}));
return ($result, $data);

#print STDERR join("\n-----------------\n", ($result, $data->{'*'}, $data->{'>'}, $data->{'<'}));

return ( $result, $data );
}
sub appendToSection{
my ($timestamp, $type, $text, $hash) = @_;


sub addToResultHash {
my ( $timestamp, $type, $text, $hash ) = @_;

$hash->{$type} .= "$timestamp $type $text\n";
if ($type eq '<') { #response header
#HTTP/1.1 200 OK
if ($text =~ /HTTP\/1.1\s(\d*)\s(.*)/) {
$hash->{HTTP_RESPONSE_STATUS} = $1;
if ( $type eq '<' ) { #response header
#HTTP/1.1 200 OK
if ( $text =~ /HTTP\/1.1\s(\d*)\s(.*)/ ) {
$hash->{HTTP_RESPONSE_STATUS} = $1;
$hash->{HTTP_RESPONSE_STATUS_TEXT} = $2;
}

#X-Foswiki-Rest-Query: 'Main.SvenDowideit'/topic etc
if ($text =~ /(.*):\s*(.*)/) {
if ( $text =~ /(.*):\s*(.*)/ ) {
$hash->{$1} = $2;
}
}

return '';
}


sub testGET {
my $this = shift;

{

#/Main/WebHome/topic.json
my ( $replytext, $extraHash ) =
$this->callCurl('GET', 'text/json', '', Foswiki::Func::getScriptUrl( undef, undef, 'query' ).'/Main/WebHome/topic.json' );
my ( $replytext, $extraHash ) = $this->callCurl( 'GET', 'text/json', '',
Foswiki::Func::getScriptUrl( undef, undef, 'query' )
. '/Main/WebHome/topic.json' );

#print STDERR $replytext;
my $fromJSON = JSON::from_json( $replytext, { allow_nonref => 1 } );
my ( $meta, $text ) = Foswiki::Func::readTopic( 'Main', 'WebHome' );
$this->assert_deep_equals( $fromJSON,
Foswiki::Serialise::convertMeta($meta) );
$this->assert_equals('200',$extraHash->{HTTP_RESPONSE_STATUS});
$this->assert_equals('OK',$extraHash->{HTTP_RESPONSE_STATUS_TEXT});
$this->assert_equals("'Main.WebHome'/topic",$extraHash->{'X-Foswiki-Rest-Query'});
$this->assert_equals( '200', $extraHash->{HTTP_RESPONSE_STATUS} );
$this->assert_equals( 'OK', $extraHash->{HTTP_RESPONSE_STATUS_TEXT} );
$this->assert_equals( "'Main.WebHome'/topic",
$extraHash->{'X-Foswiki-Rest-Query'} );

#TODO: test the other values we're returning
}
}


1;

0 comments on commit c30641a

Please sign in to comment.