/
DiscoveryResponseTest.php
147 lines (118 loc) · 4.43 KB
/
DiscoveryResponseTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\SAML2\XML\idpdisc;
use DOMDocument;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\SAML2\XML\idpdisc\DiscoveryResponse;
use SimpleSAML\Test\SAML2\Constants as C;
use SimpleSAML\XML\Attribute as XMLAttribute;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\TestUtils\ArrayizableElementTestTrait;
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
use function dirname;
use function strval;
/**
* Class \SAML2\XML\idpdisc\DiscoveryResponseTest
*
* @covers \SimpleSAML\SAML2\XML\idpdisc\DiscoveryResponse
* @covers \SimpleSAML\SAML2\XML\md\AbstractIndexedEndpointType
* @covers \SimpleSAML\SAML2\XML\md\AbstractMdElement
* @package simplesamlphp/saml2
*/
final class DiscoveryResponseTest extends TestCase
{
use ArrayizableElementTestTrait;
use SchemaValidationTestTrait;
use SerializableElementTestTrait;
/** @var \SimpleSAML\XML\Chunk */
protected Chunk $ext;
/** @var \SimpleSAML\XML\Attribute */
protected XMLAttribute $attr;
/**
*/
protected function setUp(): void
{
$this->schema = dirname(__FILE__, 5) . '/resources/schemas/sstc-saml-idp-discovery.xsd';
$this->testedClass = DiscoveryResponse::class;
$this->attr = new XMLAttribute('urn:x-simplesamlphp:namespace', 'ssp', 'attr1', 'testval1');
$this->ext = new Chunk(DOMDocumentFactory::fromString(
'<some:Ext xmlns:some="urn:mace:some:metadata:1.0">SomeExtension</some:Ext>'
)->documentElement);
$this->arrayRepresentation = [
'index' => 1,
'Binding' => C::BINDING_HTTP_POST,
'Location' => 'https://whatever/',
'isDefault' => true,
//'ResponseLocation' => null,
'Extensions' => [$this->ext],
'attributes' => [$this->attr->toArray()],
];
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
dirname(__FILE__, 4) . '/resources/xml/idpdisc_DiscoveryResponse.xml',
);
}
// test marshalling
/**
* Test creating a DiscoveryResponse from scratch.
*/
public function testMarshalling(): void
{
$discoResponse = new DiscoveryResponse(
43,
C::BINDING_HTTP_POST,
C::LOCATION_A,
false,
null,
[$this->attr],
[$this->ext],
);
$this->assertEquals(
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
strval($discoResponse),
);
}
/**
* Test that creating a DiscoveryResponseService from scratch with a ResponseLocation fails.
*/
public function testMarshallingWithResponseLocation(): void
{
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage(
'The \'ResponseLocation\' attribute must be omitted for idpdisc:DiscoveryResponse.',
);
new DiscoveryResponse(42, C::BINDING_HTTP_ARTIFACT, C::LOCATION_A, false, 'https://response.location/');
}
// test unmarshalling
/**
* Test creating a DiscoveryResponse from XML.
*/
public function testUnmarshalling(): void
{
$discoResponse = DiscoveryResponse::fromXML($this->xmlRepresentation->documentElement);
$this->assertEquals(
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
strval($discoResponse),
);
}
/**
* Test that creating a DiscoveryResponse from XML fails when ResponseLocation is present.
*/
public function testUnmarshallingWithResponseLocation(): void
{
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage(
'The \'ResponseLocation\' attribute must be omitted for idpdisc:DiscoveryResponse.',
);
$this->xmlRepresentation->documentElement->setAttribute('ResponseLocation', 'https://response.location/');
DiscoveryResponse::fromXML($this->xmlRepresentation->documentElement);
DiscoveryResponse::fromArray(array_merge(
$this->arrayRepresentation,
['ResponseLocation', 'https://response.location'],
));
}
}