From 6586b684ddbcabae76cbeac056111c08ef9fbcf6 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Tue, 10 Apr 2018 06:54:08 +0200 Subject: [PATCH] Use inc::LWP::Protocol::PSGI in tests instead of redefining LWP::UserAgent methods (#595) --- ...h-and-issuer-SAML-Artifact-with-SOAP-SLO.t | 2 - .../t/33-Auth-and-issuer-OpenID2.t | 68 +++++------ .../t/34-Auth-Proxy-and-REST-Server.t | 108 ++++++++--------- .../t/34-Auth-Proxy-and-SOAP-Server.t | 77 ++++++------ .../t/35-REST-config-backend.t | 109 ++++++++--------- .../t/35-REST-sessions-with-REST-server.t | 107 ++++++++--------- .../t/35-SOAP-config-backend.t | 73 ++++++------ .../t/35-SOAP-sessions-with-SOAP-server.t | 73 ++++++------ .../t/37-Logout-from-OIDC-RP-to-SAML-SP.t | 112 +++++++++--------- .../t/37-OIDC-RP-to-SAML-IdP-GET.t | 112 +++++++++--------- .../t/37-OIDC-RP-to-SAML-IdP-POST.t | 112 +++++++++--------- .../t/37-SAML-SP-GET-to-OIDC-OP.t | 112 +++++++++--------- .../t/37-SAML-SP-POST-to-OIDC-OP.t | 112 +++++++++--------- .../t/40-Notifications-XML-Server.t | 68 +++++------ 14 files changed, 591 insertions(+), 654 deletions(-) diff --git a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t index dd6f536b4..e922af640 100644 --- a/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t +++ b/lemonldap-ng-portal/t/30-Auth-and-issuer-SAML-Artifact-with-SOAP-SLO.t @@ -219,8 +219,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t b/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t index 039fe63fe..3a09605a2 100644 --- a/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t +++ b/lemonldap-ng-portal/t/33-Auth-and-issuer-OpenID2.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -12,6 +14,36 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.idp.com(.*)#, + ' Request from SP to IdP' ); + my $url = $1; + my ($res); + count(1); + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $issuer->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + accept => 'text/plain', + ), + ' Execute request' + ); + } + else { + ok( $res = $issuer->_get( $url, accept => 'text/plain', ), + ' Execute post request' ); + } + expectOK($res); + count(1); + return $res; + } +); + SKIP: { eval { require Net::OpenID::Consumer; require Net::OpenID::Server; }; if ($@) { @@ -105,42 +137,6 @@ sub switch { }; } -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.idp.com(.*)#, ' Request from SP to IdP' ); - my $url = $1; - my ($res); - count(1); - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $issuer->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - accept => 'text/plain', - ), - ' Execute request' - ); - } - else { - ok( $res = $issuer->_get( $url, accept => 'text/plain', ), - ' Execute post request' ); - } - expectOK($res); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->request($req); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(1); - return $httpResp; -} - sub issuer { return LLNG::Manager::Test->new( { diff --git a/lemonldap-ng-portal/t/34-Auth-Proxy-and-REST-Server.t b/lemonldap-ng-portal/t/34-Auth-Proxy-and-REST-Server.t index 8fc3a69d4..9f380c5f7 100644 --- a/lemonldap-ng-portal/t/34-Auth-Proxy-and-REST-Server.t +++ b/lemonldap-ng-portal/t/34-Auth-Proxy-and-REST-Server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -10,6 +12,55 @@ my $debug = 'error'; my ( $issuer, $sp, $res, $spId, $idpId ); my %handlerOR = ( issuer => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( + $req->uri =~ m#http://auth.idp.com(.*)#, + ' @ REST request (' . $req->method . " $1)" + ); + count(1); + my $url = $1; + my $res; + my $s = $req->content; + if ( $req->method =~ /^(post|put)$/i ) { + my $mth = '_' . lc($1); + my $s = $req->content; + ok( + $res = $issuer->$mth( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Post request' + ); + count(1); + expectOK($res); + $idpId = expectCookie($res); + } + elsif ( $req->method =~ /^(get|delete)$/i ) { + my $mth = '_' . lc($1); + ok( + $res = $issuer->$mth( + $url, + accept => $req->header('Accept'), + cookie => $req->header('Cookie') + ), + ' Execute request' + ); + count(1); + expectOK($res); + } + ok( + getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/json#, + 'Content is JSON' + ) or explain( $res->[1], 'Content-Type => application/json' ); + count(1); + return $res; + } +); + ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; switch ('sp'); @@ -175,63 +226,6 @@ clean_sessions(); done_testing( count() ); # Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( - $req->uri =~ m#http://auth.idp.com(.*)#, - ' @ REST request (' . $req->method . " $1)" - ); - count(1); - my $url = $1; - my $res; - my $s = $req->content; - if ( $req->method =~ /^(post|put)$/i ) { - my $mth = '_' . lc($1); - my $s = $req->content; - ok( - $res = $issuer->$mth( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Post request' - ); - count(1); - expectOK($res); - $idpId = expectCookie($res); - } - elsif ( $req->method =~ /^(get|delete)$/i ) { - my $mth = '_' . lc($1); - ok( - $res = $issuer->$mth( - $url, - accept => $req->header('Accept'), - cookie => $req->header('Cookie') - ), - ' Execute request' - ); - count(1); - expectOK($res); - } - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/json#, - 'Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - count(1); - my $httpResp; - $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF REST REQUEST'); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/34-Auth-Proxy-and-SOAP-Server.t b/lemonldap-ng-portal/t/34-Auth-Proxy-and-SOAP-Server.t index 7cac5df8e..987407c30 100644 --- a/lemonldap-ng-portal/t/34-Auth-Proxy-and-SOAP-Server.t +++ b/lemonldap-ng-portal/t/34-Auth-Proxy-and-SOAP-Server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -11,6 +13,41 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, + ' @ SOAP REQUEST @' ); + my $host = $1; + my $url = $2; + my $res; + my $s = $req->content; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + switch ( $host eq 'idp' ? 'issuer' : 'sp' ); + ok( + $res = $client->_post( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + custom => { + HTTP_SOAPACTION => $req->header('Soapaction'), + }, + ), + ' Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, + ' Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + pass(' @ END OF SOAP REQUEST @'); + count(4); + switch ( $host eq 'idp' ? 'sp' : 'issuer' ); + return $res; + } +); + SKIP: { eval 'use SOAP::Lite'; if ($@) { @@ -96,46 +133,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, ' @ SOAP REQUEST @' ); - my $host = $1; - my $url = $2; - my $res; - my $s = $req->content; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - switch ( $host eq 'idp' ? 'issuer' : 'sp' ); - ok( - $res = $client->_post( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - custom => { - HTTP_SOAPACTION => $req->header('Soapaction'), - }, - ), - ' Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, - ' Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF SOAP REQUEST @'); - count(4); - switch ( $host eq 'idp' ? 'sp' : 'issuer' ); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/35-REST-config-backend.t b/lemonldap-ng-portal/t/35-REST-config-backend.t index 51c269c65..5a69b25f7 100644 --- a/lemonldap-ng-portal/t/35-REST-config-backend.t +++ b/lemonldap-ng-portal/t/35-REST-config-backend.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -10,6 +12,56 @@ my $debug = 'error'; my ( $issuer, $sp, $res, $spId ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( + $req->uri =~ m#http://auth.idp.com(.*?)(?:\?(.*))?$#, + ' @ REST request (' . $req->method . " $1)" + ); + count(1); + my $url = $1; + my $query = $2; + my $res; + my $s = $req->content; + if ( $req->method =~ /^(post|put)$/i ) { + my $mth = '_' . lc($1); + my $s = $req->content; + ok( + $res = $issuer->$mth( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Post request' + ); + count(1); + expectOK($res); + } + elsif ( $req->method =~ /^(get|delete)$/i ) { + my $mth = '_' . lc($1); + ok( + $res = $issuer->$mth( + $url, + accept => $req->header('Accept'), + cookie => $req->header('Cookie'), + query => $query, + ), + ' Execute request' + ); + ok( ( $res->[0] == 200 or $res->[0] == 400 ), + ' Response is 200 or 400' ) + or explain( $res->[0], '200 or 400' ); + count(2); + } + pass(' @ END OF REST REQUEST'); + count(1); + return $res; + } +); + ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; @@ -103,63 +155,6 @@ expectReject($res); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( - $req->uri =~ m#http://auth.idp.com(.*?)(?:\?(.*))?$#, - ' @ REST request (' . $req->method . " $1)" - ); - count(1); - my $url = $1; - my $query = $2; - my $res; - my $s = $req->content; - if ( $req->method =~ /^(post|put)$/i ) { - my $mth = '_' . lc($1); - my $s = $req->content; - ok( - $res = $issuer->$mth( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Post request' - ); - count(1); - expectOK($res); - } - elsif ( $req->method =~ /^(get|delete)$/i ) { - my $mth = '_' . lc($1); - ok( - $res = $issuer->$mth( - $url, - accept => $req->header('Accept'), - cookie => $req->header('Cookie'), - query => $query, - ), - ' Execute request' - ); - ok( ( $res->[0] == 200 or $res->[0] == 400 ), - ' Response is 200 or 400' ) - or explain( $res->[0], '200 or 400' ); - count(2); - } - my $httpResp; - $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF REST REQUEST'); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t b/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t index e21b97d6e..d46230509 100644 --- a/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t +++ b/lemonldap-ng-portal/t/35-REST-sessions-with-REST-server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -10,6 +12,56 @@ my $debug = 'error'; my ( $issuer, $sp, $res, $spId ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( + $req->uri =~ m#http://auth.idp.com(.*?)(?:\?(.*))?$#, + ' @ REST request (' . $req->method . " $1)" + ); + count(1); + my $url = $1; + my $query = $2; + my $res; + my $s = $req->content; + if ( $req->method =~ /^(post|put)$/i ) { + my $mth = '_' . lc($1); + my $s = $req->content; + ok( + $res = $issuer->$mth( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Post request' + ); + count(1); + expectOK($res); + } + elsif ( $req->method =~ /^(get|delete)$/i ) { + my $mth = '_' . lc($1); + ok( + $res = $issuer->$mth( + $url, + accept => $req->header('Accept'), + cookie => $req->header('Cookie'), + query => $query, + ), + ' Execute request' + ); + ok( ( $res->[0] == 200 or $res->[0] == 400 ), + ' Response is 200 or 400' ) + or explain( $res->[0], '200 or 400' ); + count(2); + } + pass(' @ END OF REST REQUEST'); + count(1); + return $res; + } +); + ok( $issuer = issuer(), 'Issuer portal' ); $handlerOR{issuer} = \@Lemonldap::NG::Handler::Main::_onReload; switch ('sp'); @@ -99,61 +151,6 @@ expectReject($res); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( - $req->uri =~ m#http://auth.idp.com(.*)#, - ' @ REST request (' . $req->method . " $1)" - ); - count(1); - my $url = $1; - my $res; - my $s = $req->content; - if ( $req->method =~ /^(post|put)$/i ) { - my $mth = '_' . lc($1); - my $s = $req->content; - ok( - $res = $issuer->$mth( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Post request' - ); - count(1); - expectOK($res); - } - elsif ( $req->method =~ /^(get|delete)$/i ) { - my $mth = '_' . lc($1); - ok( - $res = $issuer->$mth( - $url, - accept => $req->header('Accept'), - cookie => $req->header('Cookie') - ), - ' Execute request' - ); - ok( ( $res->[0] == 200 or $res->[0] == 400 ), - ' Response is 200 or 400' ) - or explain( $res->[0], '200 or 400' ); - count(2); - } - my $httpResp; - $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF REST REQUEST'); - count(1); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/35-SOAP-config-backend.t b/lemonldap-ng-portal/t/35-SOAP-config-backend.t index 6255d479b..396f23dad 100644 --- a/lemonldap-ng-portal/t/35-SOAP-config-backend.t +++ b/lemonldap-ng-portal/t/35-SOAP-config-backend.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -11,6 +13,39 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, + ' @ SOAP REQUEST @' ); + my $host = $1; + my $url = $2; + my $res; + my $s = $req->content; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + ok( + $res = $client->_post( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + custom => { + HTTP_SOAPACTION => $req->header('Soapaction'), + }, + ), + ' Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, + ' Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + pass(' @ END OF SOAP REQUEST @'); + count(4); + return $res; + } +); + SKIP: { eval 'use SOAP::Lite'; if ($@) { @@ -89,44 +124,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, ' @ SOAP REQUEST @' ); - my $host = $1; - my $url = $2; - my $res; - my $s = $req->content; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - ok( - $res = $client->_post( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - custom => { - HTTP_SOAPACTION => $req->header('Soapaction'), - }, - ), - ' Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, - ' Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF SOAP REQUEST @'); - count(4); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/35-SOAP-sessions-with-SOAP-server.t b/lemonldap-ng-portal/t/35-SOAP-sessions-with-SOAP-server.t index 76f615f3b..2ed411f1b 100644 --- a/lemonldap-ng-portal/t/35-SOAP-sessions-with-SOAP-server.t +++ b/lemonldap-ng-portal/t/35-SOAP-sessions-with-SOAP-server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -11,6 +13,39 @@ my $debug = 'error'; my ( $issuer, $sp, $res ); my %handlerOR = ( issuer => [], sp => [] ); +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, + ' @ SOAP REQUEST @' ); + my $host = $1; + my $url = $2; + my $res; + my $s = $req->content; + my $client = ( $host eq 'idp' ? $issuer : $sp ); + ok( + $res = $client->_post( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + custom => { + HTTP_SOAPACTION => $req->header('Soapaction'), + }, + ), + ' Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, + ' Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + pass(' @ END OF SOAP REQUEST @'); + count(4); + return $res; + } +); + SKIP: { eval 'use SOAP::Lite'; if ($@) { @@ -77,44 +112,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:id|s)p).com(.*)#, ' @ SOAP REQUEST @' ); - my $host = $1; - my $url = $2; - my $res; - my $s = $req->content; - my $client = ( $host eq 'idp' ? $issuer : $sp ); - ok( - $res = $client->_post( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - custom => { - HTTP_SOAPACTION => $req->header('Soapaction'), - }, - ), - ' Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, - ' Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF SOAP REQUEST @'); - count(4); - return $httpResp; -} - sub switch { my $type = shift; @Lemonldap::NG::Handler::Main::_onReload = @{ diff --git a/lemonldap-ng-portal/t/37-Logout-from-OIDC-RP-to-SAML-SP.t b/lemonldap-ng-portal/t/37-Logout-from-OIDC-RP-to-SAML-SP.t index b1bcf82e5..37318b0eb 100644 --- a/lemonldap-ng-portal/t/37-Logout-from-OIDC-RP-to-SAML-SP.t +++ b/lemonldap-ng-portal/t/37-Logout-from-OIDC-RP-to-SAML-SP.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $op, $rp, $sp, $res ); my %handlerOR = ( op => [], rp => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -285,65 +338,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.((?:o|r)p).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-GET.t b/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-GET.t index 34c0730d3..7ecc96b76 100644 --- a/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-GET.t +++ b/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-GET.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $idp, $sp, $rp, $res ); my %handlerOR = ( idp => [], sp => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.(rp|sp).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'sp' ) { + pass(" Request from RP to OP(sp), endpoint $url"); + $client = $sp; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP(proxy)'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -231,65 +284,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.(rp|sp).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'sp' ) { - pass(" Request from RP to OP(sp), endpoint $url"); - $client = $sp; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP(proxy)'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-POST.t b/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-POST.t index 8e5ecfd6f..1832a07e0 100644 --- a/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-POST.t +++ b/lemonldap-ng-portal/t/37-OIDC-RP-to-SAML-IdP-POST.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $idp, $sp, $rp, $res ); my %handlerOR = ( idp => [], sp => [], rp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.(rp|sp).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'sp' ) { + pass(" Request from RP to OP(sp), endpoint $url"); + $client = $sp; + } + elsif ( $host eq 'rp' ) { + pass(' Request from OP to RP(proxy)'); + $client = $rp; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -233,65 +286,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.(rp|sp).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'sp' ) { - pass(" Request from RP to OP(sp), endpoint $url"); - $client = $sp; - } - elsif ( $host eq 'rp' ) { - pass(' Request from OP to RP(proxy)'); - $client = $rp; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/37-SAML-SP-GET-to-OIDC-OP.t b/lemonldap-ng-portal/t/37-SAML-SP-GET-to-OIDC-OP.t index 334f8158b..3fb5024b5 100644 --- a/lemonldap-ng-portal/t/37-SAML-SP-GET-to-OIDC-OP.t +++ b/lemonldap-ng-portal/t/37-SAML-SP-GET-to-OIDC-OP.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $op, $proxy, $sp, $res ); my %handlerOR = ( op => [], proxy => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.(op|proxy).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP(proxy) to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'proxy' ) { + pass(' Request from OP to RP(proxy)'); + $client = $proxy; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -205,65 +258,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.(op|proxy).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP(proxy) to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'proxy' ) { - pass(' Request from OP to RP(proxy)'); - $client = $proxy; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/37-SAML-SP-POST-to-OIDC-OP.t b/lemonldap-ng-portal/t/37-SAML-SP-POST-to-OIDC-OP.t index 09f2d81f4..abea7f93c 100644 --- a/lemonldap-ng-portal/t/37-SAML-SP-POST-to-OIDC-OP.t +++ b/lemonldap-ng-portal/t/37-SAML-SP-POST-to-OIDC-OP.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; use MIME::Base64; BEGIN { @@ -13,6 +15,57 @@ my $debug = 'error'; my ( $op, $proxy, $sp, $res ); my %handlerOR = ( op => [], proxy => [], sp => [] ); +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.(op|proxy).com(.*)#, ' REST request' ); + my $host = $1; + my $url = $2; + my ( $res, $client ); + count(1); + if ( $host eq 'op' ) { + pass(" Request from RP(proxy) to OP, endpoint $url"); + $client = $op; + } + elsif ( $host eq 'proxy' ) { + pass(' Request from OP to RP(proxy)'); + $client = $proxy; + } + else { + fail(' Aborting REST request (external)'); + return HTTP::Response->new(500); + } + if ( $req->method =~ /^post$/i ) { + my $s = $req->content; + ok( + $res = $client->_post( + $url, IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + ), + ' Execute request' + ); + } + else { + ok( + $res = $client->_get( + $url, + custom => { + HTTP_AUTHORIZATION => $req->header('Authorization'), + } + ), + ' Execute request' + ); + } + ok( $res->[0] == 200, ' Response is 200' ); + ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, + ' Content is JSON' ) + or explain( $res->[1], 'Content-Type => application/json' ); + count(4); + return $res; + } +); + SKIP: { eval "use Lasso"; if ($@) { @@ -205,65 +258,6 @@ count($maintests); clean_sessions(); done_testing( count() ); -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.(op|proxy).com(.*)#, ' REST request' ); - my $host = $1; - my $url = $2; - my ( $res, $client ); - count(1); - if ( $host eq 'op' ) { - pass(" Request from RP(proxy) to OP, endpoint $url"); - $client = $op; - } - elsif ( $host eq 'proxy' ) { - pass(' Request from OP to RP(proxy)'); - $client = $proxy; - } - else { - fail(' Aborting REST request (external)'); - return HTTP::Response->new(500); - } - if ( $req->method =~ /^post$/i ) { - my $s = $req->content; - ok( - $res = $client->_post( - $url, IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - ), - ' Execute request' - ); - } - else { - ok( - $res = $client->_get( - $url, - custom => { - HTTP_AUTHORIZATION => $req->header('Authorization'), - } - ), - ' Execute request' - ); - } - ok( $res->[0] == 200, ' Response is 200' ); - ok( getHeader( $res, 'Content-Type' ) =~ m#^application/json#, - ' Content is JSON' ) - or explain( $res->[1], 'Content-Type => application/json' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - - #print STDERR Dumper($res->[2]); - $httpResp->content( join( '', @{ $res->[2] } ) ); - count(4); - return $httpResp; -} - sub switch { my $type = shift; pass( '==> Switching to ' . uc($type) . ' <==' ); diff --git a/lemonldap-ng-portal/t/40-Notifications-XML-Server.t b/lemonldap-ng-portal/t/40-Notifications-XML-Server.t index 0485223e3..1ea784aa4 100644 --- a/lemonldap-ng-portal/t/40-Notifications-XML-Server.t +++ b/lemonldap-ng-portal/t/40-Notifications-XML-Server.t @@ -1,6 +1,8 @@ use Test::More; use strict; use IO::String; +use LWP::UserAgent; +use inc::LWP::Protocol::PSGI; BEGIN { require 't/test-lib.pm'; @@ -10,6 +12,36 @@ my $maintests = 3; my $debug = 'error'; my $client; +# Redefine LWP methods for tests +LWP::Protocol::PSGI->register( + sub { + my $req = Plack::Request->new(@_); + ok( $req->uri =~ m#http://auth.example.com(.*)#, ' @ SOAP REQUEST @' ); + my $url = $1; + my $res; + my $s = $req->content; + ok( + $res = $client->_post( + $url, + IO::String->new($s), + length => length($s), + type => $req->header('Content-Type'), + custom => { + HTTP_SOAPACTION => $req->header('Soapaction'), + }, + ), + ' Execute request' + ); + expectOK($res); + ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, + ' Content is XML' ) + or explain( $res->[1], 'Content-Type => application/xml' ); + pass(' @ END OF SOAP REQUEST @'); + count(4); + return $res; + } +); + eval { unlink 't/20160530_dwho_dGVzdHJlZg==.xml' }; my $xml = ' @@ -76,39 +108,3 @@ eval { unlink 't/20160530_dwho_dGVzdHJlZg==.xml' }; count($maintests); clean_sessions(); done_testing( count() ); - -# Redefine LWP methods for tests -no warnings 'redefine'; - -sub LWP::UserAgent::request { - my ( $self, $req ) = @_; - ok( $req->uri =~ m#http://auth.example.com(.*)#, ' @ SOAP REQUEST @' ); - my $url = $1; - my $res; - my $s = $req->content; - ok( - $res = $client->_post( - $url, - IO::String->new($s), - length => length($s), - type => $req->header('Content-Type'), - custom => { - HTTP_SOAPACTION => $req->header('Soapaction'), - }, - ), - ' Execute request' - ); - expectOK($res); - ok( getHeader( $res, 'Content-Type' ) =~ m#^(?:text|application)/xml#, - ' Content is XML' ) - or explain( $res->[1], 'Content-Type => application/xml' ); - my $httpResp = HTTP::Response->new( $res->[0], 'OK' ); - - while ( my $name = shift @{ $res->[1] } ) { - $httpResp->header( $name, shift( @{ $res->[1] } ) ); - } - $httpResp->content( join( '', @{ $res->[2] } ) ); - pass(' @ END OF SOAP REQUEST @'); - count(4); - return $httpResp; -}