Skip to content

Commit 91bb1b9

Browse files
committed
Added Basic Headers tests
1 parent 9b0cf54 commit 91bb1b9

File tree

4 files changed

+154
-41
lines changed

4 files changed

+154
-41
lines changed

library/SecurityMultiTool/Common/AbstractOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public function setOptions(array $options)
2121
}
2222
}
2323

24+
public function getOptions()
25+
{
26+
return $this->options;
27+
}
28+
2429
public abstract function setOption($key, $value);
2530

2631
public function getOption($key)

library/SecurityMultiTool/Common/OptionsInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ interface OptionsInterface
77

88
public function setOptions(array $options);
99

10+
public function getOptions();
11+
1012
public function setOption($key, $value);
1113

1214
public function getOption($key);

library/SecurityMultiTool/Http/Headers.php

Lines changed: 86 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,49 @@ public function __construct(array $options = null)
2020
}
2121
}
2222

23+
public function send($replace = false)
24+
{
25+
ksort($this->headers, \SORT_STRING);
26+
foreach ($this->headers as $value) {
27+
$value->send($replace);
28+
}
29+
}
30+
31+
public function getHeaders()
32+
{
33+
return $this->headers;
34+
}
35+
36+
public function toArray()
37+
{
38+
$headers = array();
39+
foreach ($this->headers as $value) {
40+
$headers = $value->getHeader();
41+
}
42+
asort($headers, \SORT_STRING);
43+
return $headers;
44+
}
45+
46+
public function toString()
47+
{
48+
$string = '';
49+
$headers = $this->toArray();
50+
foreach ($headers as $header) {
51+
$string .= sprintf('%s\r\n', $header);
52+
}
53+
return $string;
54+
}
55+
56+
public function __toString()
57+
{
58+
return $this->toString();
59+
}
60+
61+
public function count()
62+
{
63+
return count($this->headers);
64+
}
65+
2366
public function setOptions(array $options)
2467
{
2568
foreach ($options as $key => $value) {
@@ -33,8 +76,15 @@ public function setOption($key, $options)
3376
case 'strict_transport_security':
3477
case 'sts':
3578
try {
36-
$this->headers['strict_transport_security']
37-
= new Header\StrictTransportSecurity($options);
79+
if (!isset($this->headers['strict_transport_security'])) {
80+
$this->headers['strict_transport_security']
81+
= new Header\StrictTransportSecurity($options);
82+
} else {
83+
foreach ($options as $key => $value) {
84+
$this->headers['strict_transport_security']
85+
->setOption($key, $value);
86+
}
87+
}
3888
} catch (\Exception $e) {
3989
throw $e;
4090
}
@@ -43,8 +93,15 @@ public function setOption($key, $options)
4393
case 'csrf_token':
4494
case 'csrf':
4595
try {
46-
$this->headers['x_csrftoken']
47-
= new Header\CsrfToken($options);
96+
if (!isset($this->headers['csrf_token'])) {
97+
$this->headers['csrf_token']
98+
= new Header\CsrfToken($options);
99+
} else {
100+
foreach ($options as $key => $value) {
101+
$this->headers['csrf_token']
102+
->setOption($key, $value);
103+
}
104+
}
48105
} catch (\Exception $e) {
49106
throw $e;
50107
}
@@ -60,52 +117,40 @@ public function setOption($key, $options)
60117

61118
public function getOption($key)
62119
{
63-
if (isset($this->options[$key])) {
64-
return $this->options[$key];
65-
}
66-
}
67-
68-
public function send($replace = false)
69-
{
70-
ksort($this->headers, \SORT_STRING);
71-
foreach ($this->headers as $value) {
72-
$value->send($replace);
73-
}
74-
}
75-
76-
public function getHeaders()
77-
{
78-
return $this->headers;
79-
}
120+
switch ($key) {
121+
case 'strict_transport_security':
122+
case 'sts':
123+
return $this->headers['strict_transport_security']->getOptions();
124+
break;
80125

81-
public function toArray()
82-
{
83-
$headers = array();
84-
foreach ($this->headers as $value) {
85-
$headers = $value->getHeader();
126+
case 'csrf_token':
127+
case 'csrf':
128+
return $this->headers['csrf_token']->getOptions();
129+
break;
130+
131+
default:
132+
return null;
133+
break;
86134
}
87-
asort($headers, \SORT_STRING);
88-
return $headers;
89135
}
90136

91-
public function toString()
137+
public function getOptions()
92138
{
93-
$string = '';
94-
$headers = $this->toArray();
95-
foreach ($headers as $header) {
96-
$string .= sprintf('%s\r\n', $header);
139+
$return = array();
140+
ksort($this->headers, \SORT_STRING);
141+
foreach ($this->headers as $key => $value) {
142+
$return[$key] = $value->getOptions();
97143
}
98-
return $string;
144+
return $return;
99145
}
100146

101-
public function __toString()
147+
public function addHeader(Header\HeaderInterface $header)
102148
{
103-
return $this->toString();
104-
}
105-
106-
public function count()
107-
{
108-
return count($this->headers);
149+
$class = get_class($header);
150+
$parts = explode('\\', $class);
151+
$name = strtolower(array_shift($parts));
152+
$this->headers[$name] = $header;
153+
return $this;
109154
}
110155

111156
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* SecurityMultiTool
4+
*
5+
* LICENSE
6+
*
7+
* This source file is subject to the new BSD license that is bundled
8+
* with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* https://github.com/padraic/SecurityMultiTool/blob/master/LICENSE
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to padraic@php.net so we can send you a copy immediately.
14+
*
15+
* @category SecurityMultiTool
16+
* @package SecurityMultiTool
17+
* @subpackage UnitTests
18+
* @copyright Copyright (c) 2013 Pádraic Brady (http://blog.astrumfutura.com)
19+
* @license http://github.com/padraic/SecurityMultiTool/blob/master/LICENSE New BSD License
20+
*/
21+
22+
use SecurityMultiTool\Http\Headers;
23+
use SecurityMultiTool\Http\Header;
24+
use Mockery as M;
25+
26+
class HeadersTest extends \PHPUnit_Framework_TestCase
27+
{
28+
29+
public function testOptionSetting()
30+
{
31+
$headers = new Headers(
32+
array(
33+
'sts' => array('max_age'=>100),
34+
'strict_transport_security' => array('include_subdomains'=>true),
35+
'csrf' => array('token'=>'foo'),
36+
'csrf_token' => array('token'=>'bar')
37+
)
38+
);
39+
$expected1 = array('max_age'=>100,'include_subdomains'=>true);
40+
$expected2 = array('token'=>'bar');
41+
$expected3 = array(
42+
'csrf_token' => array('token'=>'bar'),
43+
'strict_transport_security' => array('max_age'=>100,'include_subdomains'=>true),
44+
);
45+
$this->assertSame($expected1, $headers->getOption('sts'));
46+
$this->assertSame($expected2, $headers->getOption('csrf'));
47+
$this->assertSame($expected3, $headers->getOptions());
48+
}
49+
50+
public function testHeadersAreSent()
51+
{
52+
$h1 = M::mock('SecurityMultiTool\Http\Header\HeaderInterface');
53+
$h2 = M::mock('SecurityMultiTool\Http\Header\HeaderInterface');
54+
$h1->shouldReceive('send')->once()->with(false);
55+
$h2->shouldReceive('send')->once()->with(false);
56+
$headers = new Headers;
57+
$headers->addHeader($h1)->addHeader($h2);
58+
$headers->send();
59+
}
60+
61+
}

0 commit comments

Comments
 (0)