Skip to content

Commit

Permalink
Item13897: Intermidiate commit
Browse files Browse the repository at this point in the history
- Converted Foswiki::Engine, Foswiki::Engine::CGI, Foswiki::Engine::CLI,
Foswiki::Request::Cache, Foswiki::Request::Upload, Foswiki::Request,
Foswiki::Response.

- Bug fixes.
  • Loading branch information
vrurg committed Feb 21, 2016
1 parent 6af4d5c commit 6dcd00a
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 512 deletions.
28 changes: 8 additions & 20 deletions UnitTestContrib/lib/Unit/Request.pm
Expand Up @@ -12,34 +12,22 @@ use Assert;
# SMELL: this package should not be in Unit; it is a Foswiki class and
# should be in test/unit

use Foswiki::Request;
our @ISA = qw( Foswiki::Request );
use Moo;
use namespace::clean;
extends qw( Foswiki::Request );

sub new {
my $class = shift;
my $this = $class->SUPER::new(@_);
sub BUILD {
my $this = shift;

# Taint everything
foreach my $k ( @{ $this->{param_list} } ) {
foreach my $k ( @{ $this->{param_list} } ) {
foreach ( @{ $this->{param}{$k} } ) {
$_ = TAINT($_) if defined $_;
}
foreach my $k ( @{ $this->param_list } ) {
foreach ( @{ $this->param->{$k} } ) {
$_ = TAINT($_) if defined $_;
}
}
return $this;
}

sub finish {
my ($this) = @_;

if ( $this->SUPER::can('finish') ) {
$this->SUPER::finish();
}

return;
}

sub setUrl {
my ( $this, $queryString ) = @_;

Expand Down
2 changes: 1 addition & 1 deletion UnitTestContrib/test/unit/FoswikiFnTestCase.pm
Expand Up @@ -117,7 +117,7 @@ around set_up => sub {

$orig->( $this, @_ );

my $query = Unit::Request->new("");
my $query = Unit::Request->new( initializer => "" );
$query->path_info( "/" . $this->test_web . "/" . $this->test_topic );

# Note: some tests are testing Foswiki::UI which also creates a session
Expand Down
58 changes: 29 additions & 29 deletions UnitTestContrib/test/unit/InitFormTests.pm
@@ -1,6 +1,5 @@
package InitFormTests;
use strict;
use warnings;
use v5.14;

# TODO: Should we check untitled labels? There is some special processing there.

Expand Down Expand Up @@ -30,13 +29,13 @@ The testcases below assume that the correct interpretation is the one used in Ed
=cut

use FoswikiTestCase();
our @ISA = qw( FoswikiTestCase );
use Error qw( :try );

use Foswiki::UI::Edit();
use Unit::Request();
use Error qw( :try );
use Try::Tiny;

use Moo;
use namespace::clean;
extends qw( FoswikiTestCase );

my $testweb = "TemporaryTestWeb";
my $testtopic1 = "InitTestTopic1";
Expand Down Expand Up @@ -99,24 +98,25 @@ my $edittmpl1 = <<'HERE';
HERE

# Set up the test fixture
sub set_up {
around set_up => sub {
my $orig = shift;
my $this = shift;
$this->SUPER::set_up();
$orig->( $this, @_ );

$this->createNewFoswikiSession( $Foswiki::cfg{AdminUserLogin},
$this->{request} );
$this->request );
Foswiki::Func::createWeb($testweb);
$this->createNewFoswikiSession( undef, $this->{request} );
if ( $this->{session}->can('getPubURL') ) {
$this->createNewFoswikiSession( undef, $this->request );
if ( $this->session->can('getPubURL') ) {

# FW 1.2 and later
$aurl =
$this->{session}
->getPubURL( $testweb, $testform, undef, absolute => 1 );
$this->session->getPubURL( $testweb, $testform, undef,
absolute => 1 );
}
else {
# up to FW 1.1.9
$aurl = $this->{session}->getPubUrl( 1, $testweb, $testform );
$aurl = $this->session->getPubUrl( 1, $testweb, $testform );
}

my ($to) = Foswiki::Func::readTopic( $testweb, $testtopic1 );
Expand Down Expand Up @@ -340,19 +340,20 @@ sub set_up {

Foswiki::Func::saveTopic( $testweb, "MyeditTemplate", undef, $edittmpl1 );

$this->{session}->enterContext('edit');
$this->session->enterContext('edit');

return;
}
};

sub tear_down {
around tear_down => sub {
my $orig = shift;
my $this = shift;

$this->removeWebFixture( $this->{session}, $testweb );
$this->SUPER::tear_down();
$this->removeWebFixture( $this->session, $testweb );
$orig->($this);

return;
}
};

# The right form values are created

Expand All @@ -370,8 +371,8 @@ sub setup_formtests {

$q->path_info("/$web/$topic");

#$this->{session}->{webName} = $web;
#$this->{session}->{topicName} = $topic;
#$this->session->webName = $web;
#$this->session->topicName = $topic;

require Foswiki::Attrs;
my $attr = Foswiki::Attrs->new($params);
Expand All @@ -380,12 +381,12 @@ sub setup_formtests {
$q->param( -name => $k, -value => $attr->{$k} );
}
$this->createNewFoswikiSession( undef, $q );
$this->{session}->enterContext('edit');
$this->session->enterContext('edit');

# Now generate the form. We pass a template which throws everything away
# but the form to allow for simpler analysis.
my ( $text, $tmpl ) =
Foswiki::UI::Edit::init_edit( $this->{session}, 'myedit' );
Foswiki::UI::Edit::init_edit( $this->session, 'myedit' );

return $tmpl;
}
Expand Down Expand Up @@ -832,15 +833,14 @@ sub test_unsavedtopic_rendersform {
);
$query->path_info("/$testweb/MissingTopic");
$query->method('POST');
my $fatwilly =
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
my $fatwilly = $this->createNewFoswikiSession( undef, $query );
my ($text) = $this->capture(
sub {
no strict 'refs';
&{ $this->getUIFn('edit') }($fatwilly);
use strict 'refs';
$Foswiki::engine->finalize( $fatwilly->{response},
$fatwilly->{request} );
$Foswiki::engine->finalize( $fatwilly->response,
$fatwilly->request );
}
);
$this->assert_html_matches(
Expand Down
101 changes: 51 additions & 50 deletions core/lib/Foswiki/Engine.pm
Expand Up @@ -13,14 +13,17 @@ to achieve correct behavior.
=cut

package Foswiki::Engine;
use v5.14;

use strict;
use warnings;
use Error qw( :try );
use Try::Tiny;
use Assert;
use Scalar::Util ();
use Unicode::Normalize;

use Moo;
use namespace::clean;
extends qw(Foswiki::Object);

BEGIN {
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
Expand All @@ -36,13 +39,6 @@ Constructs an engine object.
=cut

sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $this = {};
return bless $this, $class;
}

=begin TML
---++ ObjectMethod run()
Expand Down Expand Up @@ -109,50 +105,55 @@ sub prepare {
$this->prepareBodyParameters($req);
$this->prepareUploads($req);
}
catch Foswiki::EngineException with {
my $e = shift;
my $res = $e->{response};
unless ( defined $res ) {
$res = new Foswiki::Response();
$res->header( -type => 'text/html', -status => $e->{status} );
my $html = CGI::start_html( $e->{status} . ' Bad Request' );
$html .= CGI::h1( {}, 'Bad Request' );
$html .= CGI::p( {}, $e->{reason} );
$html .= CGI::end_html();
$res->print($html);
catch {
my $e = $_;
unless ( ref($e) ) {
Foswiki::Exception::Fatal->rethrow($e);
}
$this->finalizeError( $res, $req );
return $e->{status};
}
otherwise {
my $e = shift;
my $res = Foswiki::Response->new();
my $mess =
$e->can('stringify')
? $e->stringify()
: 'Unknown ' . ref($e) . ' exception: ' . $@;
$res->header( -type => 'text/plain', -status => '500' );
if (DEBUG) {

# output the full message and stacktrace to the browser
$res->print($mess);
}
else {
print STDERR $mess;

# tell the browser where to look for more help
my $text =
if ( $e->isa('Foswiki::EngineException') ) {
my $res = $e->response;
unless ( defined $res ) {
$res = Foswiki::Response->new;
$res->header( -type => 'text/html', -status => $e->status );
my $html = CGI::start_html( $e->status . ' Bad Request' );
$html .= CGI::h1( {}, 'Bad Request' );
$html .= CGI::p( {}, $e->reason );
$html .= CGI::end_html();
$res->print($html);
}
$this->finalizeError( $res, $req );
return $e->status;
}
else { # Not Foswiki::EngineException
my $res = Foswiki::Response->new();
my $mess =
$e->can('stringify')
? $e->stringify()
: 'Unknown ' . ref($e) . ' exception: ' . $@;
$res->header( -type => 'text/plain', -status => '500' );
if (DEBUG) {

# output the full message and stacktrace to the browser
$res->print($mess);
}
else {
print STDERR $mess;

# tell the browser where to look for more help
my $text =
'Foswiki detected an internal error - please check your Foswiki logs and webserver logs for more information.'
. "\n\n";
$mess =~ s/ at .*$//s;

# cut out pathnames from public announcement
$mess =~ s#/[\w./]+#path#g;
$text .= $mess;
$res->print($text);
. "\n\n";
$mess =~ s/ at .*$//s;

# cut out pathnames from public announcement
$mess =~ s#/[\w./]+#path#g;
$text .= $mess;
$res->print($text);
}
$this->finalizeError( $res, $req );
return 500; # Internal server error
}
$this->finalizeError( $res, $req );
return 500; # Internal server error
};
return $req;
}
Expand Down

0 comments on commit 6dcd00a

Please sign in to comment.