diff --git a/spec/EcPhp/CasLib/CasSpec.php b/spec/EcPhp/CasLib/CasSpec.php index e53b269..1d1daf4 100644 --- a/spec/EcPhp/CasLib/CasSpec.php +++ b/spec/EcPhp/CasLib/CasSpec.php @@ -131,7 +131,14 @@ public function it_can_authenticate_a_request(ClientInterface $client, CacheItem $this ->authenticate($request) - ->shouldBeArray(); + ->shouldBeEqualTo([ + 'serviceResponse' => [ + 'authenticationSuccess' => [ + 'user' => 'username', + 'proxyGrantingTicket' => 'pgtId', + ], + ], + ]); $request = new ServerRequest('GET', UtilsUri::withParams(new Uri('http://from'), ['ticket' => 'ST-ticket-pgt-pgtiou-not-found'])); diff --git a/spec/EcPhp/CasLib/Response/Type/ProxyFailureSpec.php b/spec/EcPhp/CasLib/Response/Type/ProxyFailureSpec.php index cf4c81f..5f26564 100644 --- a/spec/EcPhp/CasLib/Response/Type/ProxyFailureSpec.php +++ b/spec/EcPhp/CasLib/Response/Type/ProxyFailureSpec.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace spec\EcPhp\CasLib\Introspection; +namespace spec\EcPhp\CasLib\Response\Type; use Nyholm\Psr7\Response; use PhpSpec\ObjectBehavior; @@ -18,7 +18,19 @@ class ProxyFailureSpec extends ObjectBehavior { public function it_can_detect_a_proxy_failure_response() { - $response = (new Response(200)); + $body = [ + 'serviceResponse' => [ + 'proxyFailure' => "unrecognized pgt: 'PGT-123'", + ], + ]; + + $response = new Response( + 200, + [ + 'Content-Type' => 'application/json', + ], + json_encode($body) + ); $parsed = [ 'serviceResponse' => [ @@ -27,14 +39,14 @@ public function it_can_detect_a_proxy_failure_response() ]; $this - ->beConstructedWith($parsed, 'XML', $response); + ->beConstructedWith($response); $this ->getMessage() ->shouldReturn("unrecognized pgt: 'PGT-123'"); $this - ->getResponse() - ->shouldReturn($response); + ->toArray() + ->shouldReturn($parsed); } } diff --git a/spec/EcPhp/CasLib/Response/Type/ProxySpec.php b/spec/EcPhp/CasLib/Response/Type/ProxySpec.php index 230def5..493e321 100644 --- a/spec/EcPhp/CasLib/Response/Type/ProxySpec.php +++ b/spec/EcPhp/CasLib/Response/Type/ProxySpec.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace spec\EcPhp\CasLib\Introspection; +namespace spec\EcPhp\CasLib\Response\Type; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Response; @@ -23,11 +23,9 @@ public function it_can_detect_a_proxy_response() $body = <<< 'EOF' - + - PT-214-A3OoEPNr4Q9kNNuYzmfN8azU31aDUsuW8nk380k7wDExT5PFJpxR1TrNI3q3VGzyDdi0DpZ1LKb8IhPKZKQvavW-8hnfexYjmLCx7qWNsLib1W-DCzzoLVTosAUFzP3XDn5dNzoNtxIXV9KSztF9fYhwHvU0 + PGT-TICKET EOF; @@ -39,24 +37,20 @@ public function it_can_detect_a_proxy_response() $parsed = [ 'serviceResponse' => [ 'proxySuccess' => [ - 'proxyTicket' => 'PT-214-A3OoEPNr4Q9kNNuYzmfN8azU31aDUsuW8nk380k7wDExT5PFJpxR1TrNI3q3VGzyDdi0DpZ1LKb8IhPKZKQvavW-8hnfexYjmLCx7qWNsLib1W-DCzzoLVTosAUFzP3XDn5dNzoNtxIXV9KSztF9fYhwHvU0', + 'proxyTicket' => 'PGT-TICKET', ], ], ]; $this - ->beConstructedWith($parsed, 'XML', $response); + ->beConstructedWith($response); $this ->getProxyTicket() - ->shouldReturn('PT-214-A3OoEPNr4Q9kNNuYzmfN8azU31aDUsuW8nk380k7wDExT5PFJpxR1TrNI3q3VGzyDdi0DpZ1LKb8IhPKZKQvavW-8hnfexYjmLCx7qWNsLib1W-DCzzoLVTosAUFzP3XDn5dNzoNtxIXV9KSztF9fYhwHvU0'); + ->shouldReturn('PGT-TICKET'); $this - ->getFormat() - ->shouldReturn('XML'); - - $this - ->getResponse() - ->shouldReturn($response); + ->toArray() + ->shouldReturn($parsed); } } diff --git a/spec/EcPhp/CasLib/Response/Type/ServiceValidateSpec.php b/spec/EcPhp/CasLib/Response/Type/ServiceValidateSpec.php index 82d0b6c..96e397e 100644 --- a/spec/EcPhp/CasLib/Response/Type/ServiceValidateSpec.php +++ b/spec/EcPhp/CasLib/Response/Type/ServiceValidateSpec.php @@ -9,7 +9,7 @@ declare(strict_types=1); -namespace spec\EcPhp\CasLib\Introspection; +namespace spec\EcPhp\CasLib\Response\Type; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Response; @@ -21,55 +21,45 @@ public function it_can_detect_a_proxy_service_validate_response() { $psr17Factory = new Psr17Factory(); - $body = <<< 'EOF' - Useless stuff here. - EOF; - - $response = (new Response(200)) - ->withHeader('Content-Type', 'application/xml') - ->withBody($psr17Factory->createStream($body)); - - $credentials = [ - 'user' => 'user', - 'proxyGrantingTicket' => 'proxyGrantingTicket', - 'proxies' => [ - 'proxy' => [ - 'http://proxy1', - 'http://proxy2', - ], - ], - 'extendedAttributes' => [ - 'extendedAttribute' => [ - 'attributeValue' => [ - 0 => 'rex', - 1 => 'snoopy', + $bodyArray = [ + 'serviceResponse' => [ + 'authenticationSuccess' => [ + 'user' => 'user', + 'proxyGrantingTicket' => 'proxyGrantingTicket', + 'proxies' => [ + 'proxy' => [ + 'http://proxy1', + 'http://proxy2', + ], ], - '@attributes' => [ - 'name' => 'http://stork.eu/motherInLawDogName', + 'extendedAttributes' => [ + 'extendedAttribute' => [ + 'attributeValue' => [ + 0 => 'rex', + 1 => 'snoopy', + ], + '@attributes' => [ + 'name' => 'http://stork.eu/motherInLawDogName', + ], + ], ], ], ], ]; - $parsed = [ - 'serviceResponse' => [ - 'authenticationSuccess' => $credentials, - ], - ]; + $response = (new Response(200)) + ->withHeader('Content-Type', 'application/json') + ->withBody($psr17Factory->createStream(json_encode($bodyArray))); $this - ->beConstructedWith($parsed, 'XML', $response); + ->beConstructedWith($response); $this ->getCredentials() ->shouldReturn( - $credentials + $bodyArray['serviceResponse']['authenticationSuccess'] ); - $this - ->getFormat() - ->shouldReturn('XML'); - $this ->getProxies() ->shouldReturn([ @@ -80,12 +70,8 @@ public function it_can_detect_a_proxy_service_validate_response() ]); $this - ->getResponse() - ->shouldReturn($response); - - $this - ->getParsedResponse() - ->shouldReturn($parsed); + ->toArray() + ->shouldReturn($bodyArray); } public function it_can_detect_a_service_validate_response() @@ -105,11 +91,6 @@ public function it_can_detect_a_service_validate_response() ->withHeader('Content-Type', 'application/xml') ->withBody($psr17Factory->createStream($body)); - $credentials = [ - 'user' => 'user', - 'proxyGrantingTicket' => 'proxyGrantingTicket', - ]; - $parsed = [ 'serviceResponse' => [ 'authenticationSuccess' => [ @@ -120,22 +101,18 @@ public function it_can_detect_a_service_validate_response() ]; $this - ->beConstructedWith($parsed, 'XML', $response); - - $this - ->getCredentials() - ->shouldReturn($credentials); + ->beConstructedWith($response); $this - ->getFormat() - ->shouldReturn('XML'); + ->toArray() + ->shouldReturn($parsed); $this ->getProxies() ->shouldReturn([]); $this - ->getResponse() - ->shouldReturn($response); + ->toArray() + ->shouldReturn($parsed); } } diff --git a/spec/EcPhp/CasLib/Utils/ResponseSpec.php b/spec/EcPhp/CasLib/Utils/ResponseSpec.php new file mode 100644 index 0000000..c5b5826 --- /dev/null +++ b/spec/EcPhp/CasLib/Utils/ResponseSpec.php @@ -0,0 +1,100 @@ + 'application/json', + ], + $body + ); + + $this + ->toArray($response) + ->shouldReturn([ + 'serviceResponse' => [ + 'authenticationSuccess' => [ + 'user' => 'username', + ], + ], + ]); + } + + public function it_convert_a_xml_response() + { + $body = <<< 'EOF' + + + username + + + EOF; + + $response = new Response( + 200, + [ + 'Content-Type' => 'application/xml', + ], + $body + ); + + $this + ->toArray($response) + ->shouldReturn([ + 'serviceResponse' => [ + 'authenticationSuccess' => [ + 'user' => 'username', + ], + ], + ]); + } + + public function it_throws_when_content_type_header_is_missing() + { + $body = <<< 'EOF' + + + username + + + EOF; + + $response = new Response( + 200, + [], + $body + ); + + $this + ->shouldThrow(CasExceptionInterface::class) + ->during('toArray', [$response]); + } +}