Skip to content

Commit 49a7830

Browse files
authored
Merge pull request google#125 from xthiago/hostname
Add hostname attribute to Response object to allow server-side validation
2 parents 29feb2a + 2905b65 commit 49a7830

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/ReCaptcha/Response.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ class Response
4444
private $errorCodes = array();
4545

4646
/**
47-
* the hostname of the site where the reCAPTCHA was solved
47+
* The hostname of the site where the reCAPTCHA was solved.
4848
* @var string
4949
*/
50-
private $hostName = '';
50+
private $hostname;
5151

5252
/**
5353
* Build the response from the expected JSON returned by the service.
@@ -63,15 +63,17 @@ public static function fromJson($json)
6363
return new Response(false, array('invalid-json'));
6464
}
6565

66+
$hostname = isset($responseData['hostname']) ? $responseData['hostname'] : null;
67+
6668
if (isset($responseData['success']) && $responseData['success'] == true) {
67-
return new Response(true, array(), isset($responseData['hostname']) ? $responseData['hostname'] : '');
69+
return new Response(true, array(), $hostname);
6870
}
6971

7072
if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
71-
return new Response(false, $responseData['error-codes']);
73+
return new Response(false, $responseData['error-codes'], $hostname);
7274
}
7375

74-
return new Response(false);
76+
return new Response(false, array(), $hostname);
7577
}
7678

7779
/**
@@ -81,11 +83,11 @@ public static function fromJson($json)
8183
* @param array $errorCodes
8284
* @param string $hostname
8385
*/
84-
public function __construct($success, array $errorCodes = array(), $hostname = '')
86+
public function __construct($success, array $errorCodes = array(), $hostname = null)
8587
{
8688
$this->success = $success;
8789
$this->errorCodes = $errorCodes;
88-
$this->hostName = $hostname;
90+
$this->hostname = $hostname;
8991
}
9092

9193
/**
@@ -109,10 +111,12 @@ public function getErrorCodes()
109111
}
110112

111113
/**
114+
* Get hostname.
115+
*
112116
* @return string
113117
*/
114-
public function getHostName()
118+
public function getHostname()
115119
{
116-
return $this->hostName;
120+
return $this->hostname;
117121
}
118122
}

tests/ReCaptcha/ResponseTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,26 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
3232
/**
3333
* @dataProvider provideJson
3434
*/
35-
public function testFromJson($json, $success, $errorCodes)
35+
public function testFromJson($json, $success, $errorCodes, $hostname)
3636
{
3737
$response = Response::fromJson($json);
3838
$this->assertEquals($success, $response->isSuccess());
3939
$this->assertEquals($errorCodes, $response->getErrorCodes());
40+
$this->assertEquals($hostname, $response->getHostname());
4041
}
4142

4243
public function provideJson()
4344
{
4445
return array(
45-
array('{"success": true}', true, array()),
46-
array('{"success": false, "error-codes": ["test"]}', false, array('test')),
47-
array('{"success": true, "error-codes": ["test"]}', true, array()),
48-
array('{"success": false}', false, array()),
49-
array('BAD JSON', false, array('invalid-json')),
46+
array('{"success": true}', true, array(), null),
47+
array('{"success": true, "hostname": "google.com"}', true, array(), 'google.com'),
48+
array('{"success": false, "error-codes": ["test"]}', false, array('test'), null),
49+
array('{"success": false, "error-codes": ["test"], "hostname": "google.com"}', false, array('test'), 'google.com'),
50+
array('{"success": true, "error-codes": ["test"]}', true, array(), null),
51+
array('{"success": true, "error-codes": ["test"], "hostname": "google.com"}', true, array(), 'google.com'),
52+
array('{"success": false}', false, array(), null),
53+
array('{"success": false, "hostname": "google.com"}', false, array(), 'google.com'),
54+
array('BAD JSON', false, array('invalid-json'), null),
5055
);
5156
}
5257

@@ -68,4 +73,12 @@ public function testGetErrorCodes()
6873
$response = new Response(true, $errorCodes);
6974
$this->assertEquals($errorCodes, $response->getErrorCodes());
7075
}
76+
77+
public function testGetHostname()
78+
{
79+
$hostname = 'google.com';
80+
$errorCodes = array();
81+
$response = new Response(true, $errorCodes, $hostname);
82+
$this->assertEquals($hostname, $response->getHostname());
83+
}
7184
}

0 commit comments

Comments
 (0)