99use SimpleSAML \Assert \AssertionFailedException ;
1010use SimpleSAML \SAML2 \XML \idpdisc \DiscoveryResponse ;
1111use SimpleSAML \Test \SAML2 \Constants as C ;
12+ use SimpleSAML \XML \Attribute as XMLAttribute ;
13+ use SimpleSAML \XML \Chunk ;
1214use SimpleSAML \XML \DOMDocumentFactory ;
1315use SimpleSAML \XML \Exception \InvalidDOMElementException ;
1416use SimpleSAML \XML \Exception \MissingAttributeException ;
17+ use SimpleSAML \XML \TestUtils \ArrayizableElementTestTrait ;
1518use SimpleSAML \XML \TestUtils \SchemaValidationTestTrait ;
1619use SimpleSAML \XML \TestUtils \SerializableElementTestTrait ;
1720
2831 */
2932final class DiscoveryResponseTest extends TestCase
3033{
34+ use ArrayizableElementTestTrait;
3135 use SchemaValidationTestTrait;
3236 use SerializableElementTestTrait;
3337
38+ /** @var \SimpleSAML\XML\Chunk */
39+ protected Chunk $ ext ;
40+
41+ /** @var \SimpleSAML\XML\Attribute */
42+ protected XMLAttribute $ attr ;
43+
3444
3545 /**
3646 */
@@ -40,6 +50,22 @@ protected function setUp(): void
4050
4151 $ this ->testedClass = DiscoveryResponse::class;
4252
53+ $ this ->attr = new XMLAttribute ('urn:x-simplesamlphp:namespace ' , 'ssp ' , 'attr1 ' , 'testval1 ' );
54+
55+ $ this ->ext = new Chunk (DOMDocumentFactory::fromString (
56+ '<some:Ext xmlns:some="urn:mace:some:metadata:1.0">SomeExtension</some:Ext> '
57+ )->documentElement );
58+
59+ $ this ->arrayRepresentation = [
60+ 'index ' => 1 ,
61+ 'Binding ' => C::BINDING_HTTP_POST ,
62+ 'Location ' => 'https://whatever/ ' ,
63+ 'isDefault ' => true ,
64+ //'ResponseLocation' => null,
65+ 'Extensions ' => [$ this ->ext ],
66+ 'attributes ' => [$ this ->attr ->toArray ()],
67+ ];
68+
4369 $ this ->xmlRepresentation = DOMDocumentFactory::fromFile (
4470 dirname (__FILE__ , 4 ) . '/resources/xml/idpdisc_DiscoveryResponse.xml ' ,
4571 );
@@ -54,7 +80,15 @@ protected function setUp(): void
5480 */
5581 public function testMarshalling (): void
5682 {
57- $ discoResponse = new DiscoveryResponse (43 , C::BINDING_HTTP_POST , C::LOCATION_A , false );
83+ $ discoResponse = new DiscoveryResponse (
84+ 43 ,
85+ C::BINDING_HTTP_POST ,
86+ C::LOCATION_A ,
87+ false ,
88+ null ,
89+ [$ this ->attr ],
90+ [$ this ->ext ],
91+ );
5892
5993 $ this ->assertEquals (
6094 $ this ->xmlRepresentation ->saveXML ($ this ->xmlRepresentation ->documentElement ),
@@ -64,17 +98,15 @@ public function testMarshalling(): void
6498
6599
66100 /**
67- * Test that creating a DiscoveryResponse from scratch without specifying isDefault works .
101+ * Test that creating a DiscoveryResponseService from scratch with a ResponseLocation fails .
68102 */
69- public function testMarshallingWithoutIsDefault (): void
103+ public function testMarshallingWithResponseLocation (): void
70104 {
71- $ discoResponse = new DiscoveryResponse (43 , C::BINDING_HTTP_POST , C::LOCATION_A );
72- $ this ->xmlRepresentation ->documentElement ->removeAttribute ('isDefault ' );
73- $ this ->assertEquals (
74- $ this ->xmlRepresentation ->saveXML ($ this ->xmlRepresentation ->documentElement ),
75- strval ($ discoResponse ),
105+ $ this ->expectException (AssertionFailedException::class);
106+ $ this ->expectExceptionMessage (
107+ 'The \'ResponseLocation \' attribute must be omitted for idpdisc:DiscoveryResponse. ' ,
76108 );
77- $ this -> assertNull ( $ discoResponse -> getIsDefault () );
109+ new DiscoveryResponse ( 42 , C:: BINDING_HTTP_ARTIFACT , C:: LOCATION_A , false , ' https://response.location/ ' );
78110 }
79111
80112
@@ -93,4 +125,23 @@ public function testUnmarshalling(): void
93125 strval ($ discoResponse ),
94126 );
95127 }
128+
129+
130+ /**
131+ * Test that creating a DiscoveryResponse from XML fails when ResponseLocation is present.
132+ */
133+ public function testUnmarshallingWithResponseLocation (): void
134+ {
135+ $ this ->expectException (AssertionFailedException::class);
136+ $ this ->expectExceptionMessage (
137+ 'The \'ResponseLocation \' attribute must be omitted for idpdisc:DiscoveryResponse. ' ,
138+ );
139+ $ this ->xmlRepresentation ->documentElement ->setAttribute ('ResponseLocation ' , 'https://response.location/ ' );
140+
141+ DiscoveryResponse::fromXML ($ this ->xmlRepresentation ->documentElement );
142+ DiscoveryResponse::fromArray (array_merge (
143+ $ this ->arrayRepresentation ,
144+ ['ResponseLocation ' , 'https://response.location ' ],
145+ ));
146+ }
96147}
0 commit comments