Projet

Général

Profil

Paste
Télécharger (5,35 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / recaptcha / recaptcha-php / tests / ReCaptcha / RequestMethod / SocketPostTest.php @ e326068a

1
<?php
2
/**
3
 * This is a PHP library that handles calling reCAPTCHA.
4
 *
5
 * @copyright Copyright (c) 2015, Google Inc.
6
 * @link      https://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\ReCaptcha;
30
use ReCaptcha\RequestParameters;
31
use PHPUnit\Framework\TestCase;
32

    
33
class SocketPostTest extends TestCase
34
{
35
    public function testSubmitSuccess()
36
    {
37
        $socket = $this->getMockBuilder(\ReCaptcha\RequestMethod\Socket::class)
38
            ->disableOriginalConstructor()
39
            ->setMethods(array('fsockopen', 'fwrite', 'fgets', 'feof', 'fclose'))
40
            ->getMock();
41
        $socket->expects($this->once())
42
                ->method('fsockopen')
43
                ->willReturn(true);
44
        $socket->expects($this->once())
45
                ->method('fwrite');
46
        $socket->expects($this->once())
47
                ->method('fgets')
48
                ->willReturn("HTTP/1.1 200 OK\n\nRESPONSEBODY");
49
        $socket->expects($this->exactly(2))
50
                ->method('feof')
51
                ->will($this->onConsecutiveCalls(false, true));
52
        $socket->expects($this->once())
53
                ->method('fclose')
54
                ->willReturn(true);
55

    
56
        $ps = new SocketPost($socket);
57
        $response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version"));
58
        $this->assertEquals('RESPONSEBODY', $response);
59
    }
60

    
61
    public function testOverrideSiteVerifyUrl()
62
    {
63
        $socket = $this->getMockBuilder(\ReCaptcha\RequestMethod\Socket::class)
64
            ->disableOriginalConstructor()
65
            ->setMethods(array('fsockopen', 'fwrite', 'fgets', 'feof', 'fclose'))
66
            ->getMock();
67
        $socket->expects($this->once())
68
                ->method('fsockopen')
69
                ->with('ssl://over.ride', 443, 0, '', 30)
70
                ->willReturn(true);
71
        $socket->expects($this->once())
72
                ->method('fwrite')
73
                ->with($this->matchesRegularExpression('/^POST \/some\/path.*Host: over\.ride/s'));
74
        $socket->expects($this->once())
75
                ->method('fgets')
76
                ->willReturn("HTTP/1.1 200 OK\n\nRESPONSEBODY");
77
        $socket->expects($this->exactly(2))
78
                ->method('feof')
79
                ->will($this->onConsecutiveCalls(false, true));
80
        $socket->expects($this->once())
81
                ->method('fclose')
82
                ->willReturn(true);
83

    
84
        $ps = new SocketPost($socket, 'https://over.ride/some/path');
85
        $response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version"));
86
        $this->assertEquals('RESPONSEBODY', $response);
87
    }
88

    
89
    public function testSubmitBadResponse()
90
    {
91
        $socket = $this->getMockBuilder(\ReCaptcha\RequestMethod\Socket::class)
92
            ->disableOriginalConstructor()
93
            ->setMethods(array('fsockopen', 'fwrite', 'fgets', 'feof', 'fclose'))
94
            ->getMock();
95
        $socket->expects($this->once())
96
                ->method('fsockopen')
97
                ->willReturn(true);
98
        $socket->expects($this->once())
99
                ->method('fwrite');
100
        $socket->expects($this->once())
101
                ->method('fgets')
102
                ->willReturn("HTTP/1.1 500 NOPEn\\nBOBBINS");
103
        $socket->expects($this->exactly(2))
104
                ->method('feof')
105
                ->will($this->onConsecutiveCalls(false, true));
106
        $socket->expects($this->once())
107
                ->method('fclose')
108
                ->willReturn(true);
109

    
110
        $ps = new SocketPost($socket);
111
        $response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version"));
112
        $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}', $response);
113
    }
114

    
115
    public function testConnectionFailureReturnsError()
116
    {
117
        $socket = $this->getMockBuilder(\ReCaptcha\RequestMethod\Socket::class)
118
            ->disableOriginalConstructor()
119
            ->setMethods(array('fsockopen'))
120
            ->getMock();
121
        $socket->expects($this->once())
122
                ->method('fsockopen')
123
                ->willReturn(false);
124
        $ps = new SocketPost($socket);
125
        $response = $ps->submit(new RequestParameters("secret", "response", "remoteip", "version"));
126
        $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}', $response);
127
    }
128
}