1 |
be58a50c
|
Assos Assos
|
<?php
|
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 |
|
|
namespace ReCaptcha\RequestMethod;
|
28 |
|
|
|
29 |
|
|
use ReCaptcha\RequestParameters;
|
30 |
|
|
|
31 |
|
|
class PostTest extends \PHPUnit_Framework_TestCase
|
32 |
|
|
{
|
33 |
|
|
public static $assert = null;
|
34 |
|
|
protected $parameters = null;
|
35 |
|
|
protected $runcount = 0;
|
36 |
|
|
|
37 |
|
|
public function setUp()
|
38 |
|
|
{
|
39 |
|
|
$this->parameters = new RequestParameters("secret", "response", "remoteip", "version");
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
public function tearDown()
|
43 |
|
|
{
|
44 |
|
|
self::$assert = null;
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
public function testHTTPContextOptions()
|
48 |
|
|
{
|
49 |
|
|
$req = new Post();
|
50 |
|
|
self::$assert = array($this, "httpContextOptionsCallback");
|
51 |
|
|
$req->submit($this->parameters);
|
52 |
|
|
$this->assertEquals(1, $this->runcount, "The assertion was ran");
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
public function testSSLContextOptions()
|
56 |
|
|
{
|
57 |
|
|
$req = new Post();
|
58 |
|
|
self::$assert = array($this, "sslContextOptionsCallback");
|
59 |
|
|
$req->submit($this->parameters);
|
60 |
|
|
$this->assertEquals(1, $this->runcount, "The assertion was ran");
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
public function httpContextOptionsCallback(array $args)
|
64 |
|
|
{
|
65 |
|
|
$this->runcount++;
|
66 |
|
|
$this->assertCommonOptions($args);
|
67 |
|
|
|
68 |
|
|
$options = stream_context_get_options($args[2]);
|
69 |
|
|
$this->assertArrayHasKey('http', $options);
|
70 |
|
|
|
71 |
|
|
$this->assertArrayHasKey('method', $options['http']);
|
72 |
|
|
$this->assertEquals("POST", $options['http']['method']);
|
73 |
|
|
|
74 |
|
|
$this->assertArrayHasKey('content', $options['http']);
|
75 |
|
|
$this->assertEquals($this->parameters->toQueryString(), $options['http']['content']);
|
76 |
|
|
|
77 |
|
|
$this->assertArrayHasKey('header', $options['http']);
|
78 |
|
|
$headers = array(
|
79 |
|
|
"Content-type: application/x-www-form-urlencoded",
|
80 |
|
|
);
|
81 |
|
|
foreach ($headers as $header) {
|
82 |
|
|
$this->assertContains($header, $options['http']['header']);
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
public function sslContextOptionsCallback(array $args)
|
87 |
|
|
{
|
88 |
|
|
$this->runcount++;
|
89 |
|
|
$this->assertCommonOptions($args);
|
90 |
|
|
|
91 |
|
|
$options = stream_context_get_options($args[2]);
|
92 |
|
|
$this->assertArrayHasKey('http', $options);
|
93 |
|
|
$this->assertArrayHasKey('verify_peer', $options['http']);
|
94 |
|
|
$this->assertTrue($options['http']['verify_peer']);
|
95 |
|
|
|
96 |
|
|
$key = version_compare(PHP_VERSION, "5.6.0", "<") ? "CN_name" : "peer_name";
|
97 |
|
|
|
98 |
|
|
$this->assertArrayHasKey($key, $options['http']);
|
99 |
|
|
$this->assertEquals("www.google.com", $options['http'][$key]);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
protected function assertCommonOptions(array $args)
|
103 |
|
|
{
|
104 |
|
|
$this->assertCount(3, $args);
|
105 |
|
|
$this->assertStringStartsWith("https://www.google.com/", $args[0]);
|
106 |
|
|
$this->assertFalse($args[1]);
|
107 |
|
|
$this->assertTrue(is_resource($args[2]), "The context options should be a resource");
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
function file_get_contents()
|
112 |
|
|
{
|
113 |
|
|
if (PostTest::$assert) {
|
114 |
|
|
return call_user_func(PostTest::$assert, func_get_args());
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
return call_user_func_array('file_get_contents', func_get_args());
|
118 |
|
|
} |