Mojolicious-Plugin-SecureOnly

 view release on metacpan or  search on metacpan

lib/Mojolicious/Plugin/SecureOnly.pm  view on Meta::CPAN

    my $url = $c->req->url->to_abs;
    $url->scheme('https');
    $url->port($self->conf->{secureport}) if $self->conf->{secureport};
    $c->app->log->debug("SecureOnly enabled; Request for insecure resource, redirecting to $url");
    $c->redirect_to($url);
  });
}

sub detect_proxy {
  my $c = shift;
  return $c->tx->req->headers->header('X-Forwarded-For') || $c->tx->req->headers->header('X-Forwarded-Proto')
}

1;

__END__

=encoding utf8

=head1 NAME

t/basic.t  view on Meta::CPAN

  $c->render(text => "$url-$address-$num");
};

my $t = Test::Mojo->new;

# Behind a proxy, reverse_proxy correctly enabled
#   Redirect to https
{
  local $ENV{MOJO_REVERSE_PROXY} = 1;
  $t->ua->server->restart;
  $t->get_ok('/0' => {'X-Forwarded-For' => '192.0.2.2, 192.0.2.1'})
    ->status_is(302)->header_unlike('X-Original' => qr/192\.0\.2\.1/)
    ->header_like(Location => qr!https://127\.0\.0\.1:\d+/0$!)
}

# Behind a proxy, reverse_proxy incorrectly disabled
#   No redirect or else get infinite redirects
{
  local $ENV{MOJO_REVERSE_PROXY} = 0;
  $t->ua->server->restart;
  $t->get_ok('/0' => {'X-Forwarded-For' => '192.0.2.2, 192.0.2.1'})
    ->status_is(200)->header_unlike('X-Original' => qr/192\.0\.2\.1/)
    ->content_like(qr!http://127\.0\.0\.1:\d+/0-127\.0\.0\.1-0$!);
}

# Not behind a proxy, reverse_proxy incorrectly enabled
#   Redirect to https
{
  local $ENV{MOJO_REVERSE_PROXY} = 1;
  $t->ua->server->restart;
  $t->get_ok('/0')

t/modes.t  view on Meta::CPAN

};

my $t = Test::Mojo->new;

# Behind a proxy, reverse_proxy correctly enabled
#   Redirect to https (right mode)
{
  local $ENV{MOJO_REVERSE_PROXY} = 1;
  $t->ua->server->restart;
  $t->ua->server->app->mode('production');
  $t->get_ok('/0' => {'X-Forwarded-For' => '192.0.2.2, 192.0.2.1'})
    ->status_is(302)->header_unlike('X-Original' => qr/192\.0\.2\.1/)
    ->header_like(Location => qr!https://127\.0\.0\.1:\d+/0$!)
}

# Behind a proxy, reverse_proxy correctly enabled
#   Don't redirect to https (wrong mode)
{
  local $ENV{MOJO_REVERSE_PROXY} = 1;
  $t->ua->server->restart;
  $t->ua->server->app->mode('development');
  $t->get_ok('/0' => {'X-Forwarded-For' => '192.0.2.2, 192.0.2.1'})
    ->status_is(200)->header_unlike('X-Original' => qr/192\.0\.2\.1/)
    ->header_unlike(Location => qr!https://127\.0\.0\.1:\d+/0$!)
}

done_testing;

t/not_modes.t  view on Meta::CPAN

};

my $t = Test::Mojo->new;

# Behind a proxy, reverse_proxy correctly enabled
#   Redirect to https (right mode)
{
  local $ENV{MOJO_REVERSE_PROXY} = 1;
  $t->ua->server->restart;
  $t->ua->server->app->mode('production');
  $t->get_ok('/0' => {'X-Forwarded-For' => '192.0.2.2, 192.0.2.1'})
    ->status_is(302)->header_unlike('X-Original' => qr/192\.0\.2\.1/)
    ->header_like(Location => qr!https://127\.0\.0\.1:\d+/0$!)
}

# Behind a proxy, reverse_proxy correctly enabled
#   Don't redirect to https (wrong mode)
{
  local $ENV{MOJO_REVERSE_PROXY} = 1;
  $t->ua->server->restart;
  $t->ua->server->app->mode('development');
  $t->get_ok('/0' => {'X-Forwarded-For' => '192.0.2.2, 192.0.2.1'})
    ->status_is(200)->header_unlike('X-Original' => qr/192\.0\.2\.1/)
    ->header_unlike(Location => qr!https://127\.0\.0\.1:\d+/0$!)
}

done_testing;



( run in 0.485 second using v1.01-cache-2.11-cpan-4e96b696675 )