root / drupal7 / sites / all / modules / recaptcha / recaptcha-php / tests / ReCaptcha / RequestMethod / PostTest.php @ be58a50c
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* This is a PHP library that handles calling reCAPTCHA.
|
4 |
*
|
5 |
* @copyright Copyright (c) 2015, Google Inc.
|
6 |
* @link http://www.google.com/recaptcha
|
7 |
*
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 |
* of this software and associated documentation files (the "Software"), to deal
|
10 |
* in the Software without restriction, including without limitation the rights
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 |
* copies of the Software, and to permit persons to whom the Software is
|
13 |
* furnished to do so, subject to the following conditions:
|
14 |
*
|
15 |
* The above copyright notice and this permission notice shall be included in
|
16 |
* all copies or substantial portions of the Software.
|
17 |
*
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24 |
* THE SOFTWARE.
|
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 |
// Since we can't represent maxlen in userland...
|
117 |
return call_user_func_array('file_get_contents', func_get_args()); |
118 |
} |