1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LightSaml\Tests\Binding; |
4
|
|
|
|
5
|
|
|
use LightSaml\Binding\BindingFactory; |
6
|
|
|
use LightSaml\SamlConstants; |
7
|
|
|
use LightSaml\Tests\BaseTestCase; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
|
10
|
|
|
class BindingFactoryTest extends BaseTestCase |
11
|
|
|
{ |
12
|
|
|
public function test__create_http_redirect() |
13
|
|
|
{ |
14
|
|
|
$factory = new BindingFactory(); |
15
|
|
|
$binding = $factory->create(SamlConstants::BINDING_SAML2_HTTP_REDIRECT); |
16
|
|
|
$this->assertInstanceOf('LightSaml\Binding\HttpRedirectBinding', $binding); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function test__create_http_post() |
20
|
|
|
{ |
21
|
|
|
$factory = new BindingFactory(); |
22
|
|
|
$binding = $factory->create(SamlConstants::BINDING_SAML2_HTTP_POST); |
23
|
|
|
$this->assertInstanceOf('LightSaml\Binding\HttpPostBinding', $binding); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function test__create_throws_not_implemented_error_for_soap() |
27
|
|
|
{ |
28
|
|
|
$this->expectExceptionMessage("SOAP binding not implemented"); |
29
|
|
|
$this->expectException(\LogicException::class); |
30
|
|
|
$factory = new BindingFactory(); |
31
|
|
|
$factory->create(SamlConstants::BINDING_SAML2_SOAP); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function test__create_throws_not_implemented_error_for_artifact() |
35
|
|
|
{ |
36
|
|
|
$this->expectExceptionMessage("Artifact binding not implemented"); |
37
|
|
|
$this->expectException(\LogicException::class); |
38
|
|
|
$factory = new BindingFactory(); |
39
|
|
|
$factory->create(SamlConstants::BINDING_SAML2_HTTP_ARTIFACT); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function test__create_throws_for_unknown_binding() |
43
|
|
|
{ |
44
|
|
|
$this->expectExceptionMessage("Unknown binding type 'foo'"); |
45
|
|
|
$this->expectException(\LightSaml\Error\LightSamlBindingException::class); |
46
|
|
|
$factory = new BindingFactory(); |
47
|
|
|
$factory->create('foo'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function test__detect_http_redirect() |
51
|
|
|
{ |
52
|
|
|
$request = $this->createHttpRedirectRequest(); |
53
|
|
|
|
54
|
|
|
$factory = new BindingFactory(); |
55
|
|
|
|
56
|
|
|
$this->assertEquals(SamlConstants::BINDING_SAML2_HTTP_REDIRECT, $factory->detectBindingType($request)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function test__detect_http_post() |
60
|
|
|
{ |
61
|
|
|
$request = $this->createHttpPostRequest(); |
62
|
|
|
|
63
|
|
|
$factory = new BindingFactory(); |
64
|
|
|
|
65
|
|
|
$this->assertEquals(SamlConstants::BINDING_SAML2_HTTP_POST, $factory->detectBindingType($request)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function test__detect_artifact_post() |
69
|
|
|
{ |
70
|
|
|
$request = $this->createArtifactPostRequest(); |
71
|
|
|
|
72
|
|
|
$factory = new BindingFactory(); |
73
|
|
|
|
74
|
|
|
$this->assertEquals(SamlConstants::BINDING_SAML2_HTTP_ARTIFACT, $factory->detectBindingType($request)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function test__detect_artifact_get() |
78
|
|
|
{ |
79
|
|
|
$request = $this->createArtifactGetRequest(); |
80
|
|
|
|
81
|
|
|
$factory = new BindingFactory(); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(SamlConstants::BINDING_SAML2_HTTP_ARTIFACT, $factory->detectBindingType($request)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function test__detect_soap() |
87
|
|
|
{ |
88
|
|
|
$request = $this->createSoapRequest(); |
89
|
|
|
|
90
|
|
|
$factory = new BindingFactory(); |
91
|
|
|
|
92
|
|
|
$this->assertEquals(SamlConstants::BINDING_SAML2_SOAP, $factory->detectBindingType($request)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function test__detect_none_get() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$request = new Request(); |
98
|
|
|
$request->setMethod('GET'); |
99
|
|
|
|
100
|
|
|
$factory = new BindingFactory(); |
101
|
|
|
|
102
|
|
|
$this->assertNull($factory->detectBindingType($request)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function test__detect_none_post() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$request = new Request(); |
108
|
|
|
$request->setMethod('POST'); |
109
|
|
|
|
110
|
|
|
$factory = new BindingFactory(); |
111
|
|
|
|
112
|
|
|
$this->assertNull($factory->detectBindingType($request)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function test__get_binding_by_request_http_redirect() |
116
|
|
|
{ |
117
|
|
|
$request = $this->createHttpRedirectRequest(); |
118
|
|
|
$factory = new BindingFactory(); |
119
|
|
|
$this->assertInstanceOf('LightSaml\Binding\HttpRedirectBinding', $factory->getBindingByRequest($request)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function test__get_binding_by_request_http_post() |
123
|
|
|
{ |
124
|
|
|
$request = $this->createHttpPostRequest(); |
125
|
|
|
$factory = new BindingFactory(); |
126
|
|
|
$this->assertInstanceOf('LightSaml\Binding\HttpPostBinding', $factory->getBindingByRequest($request)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return Request |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
private function createHttpPostRequest() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$request = new Request(); |
135
|
|
|
$request->request->add(array('SAMLRequest' => 'request')); |
136
|
|
|
$request->setMethod('POST'); |
137
|
|
|
|
138
|
|
|
return $request; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return Request |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
private function createHttpRedirectRequest() |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$request = new Request(); |
147
|
|
|
$request->query->add(array('SAMLRequest' => 'request')); |
148
|
|
|
$request->setMethod('GET'); |
149
|
|
|
|
150
|
|
|
return $request; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return Request |
155
|
|
|
*/ |
156
|
|
View Code Duplication |
private function createArtifactPostRequest() |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$request = new Request(); |
159
|
|
|
$request->request->add(array('SAMLart' => 'request')); |
160
|
|
|
$request->setMethod('POST'); |
161
|
|
|
|
162
|
|
|
return $request; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return Request |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
private function createArtifactGetRequest() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
$request = new Request(); |
171
|
|
|
$request->query->add(array('SAMLart' => 'request')); |
172
|
|
|
$request->setMethod('GET'); |
173
|
|
|
|
174
|
|
|
return $request; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return Request |
179
|
|
|
*/ |
180
|
|
|
private function createSoapRequest() |
181
|
|
|
{ |
182
|
|
|
$request = new Request(); |
183
|
|
|
$request->setMethod('POST'); |
184
|
|
|
$request->headers->add(array('CONTENT_TYPE' => 'text/xml; charset=utf-8')); |
185
|
|
|
|
186
|
|
|
return $request; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.