Skip to content

Commit

Permalink
Handle a case where google tells us the response is bad, but does not…
Browse files Browse the repository at this point in the history
… specify error code. For non-json errors throw a Malformed response exception instead of generic exception.
  • Loading branch information
james-turner committed Dec 16, 2014
1 parent 55b0c1f commit b68ab44
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/Google/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ public function validate($token, $ip = null)
return new ReCaptchaResponse(true);
}elseif(array_key_exists("error-codes", $json) && is_array($json['error-codes'])) {
return new ReCaptchaResponse(false, $json['error-codes']);
}elseif(array_key_exists("success", $json) && !$json['success']){
return new ReCaptchaResponse(false, []);
}else{
throw new MalformedResponseException($response);
}
}
throw new ReCaptchaException("Non-json response '$response'");
throw new MalformedResponseException($response);
}
}
19 changes: 19 additions & 0 deletions src/Google/ReCaptchaResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,39 @@ public function isFailure(){
return !$this->isSuccess();
}

/**
* @return bool
*/
public function isMissingInputSecret(){
return array_search(static::MISSING_INPUT_SECRET, $this->errors)!==false;
}

/**
* @return bool
*/
public function isMissingInputResponse(){
return array_search(static::MISSING_INPUT_RESPONSE, $this->errors)!==false;
}

/**
* @return bool
*/
public function isInvalidInputSecret(){
return array_search(static::INVALID_INPUT_SECRET, $this->errors)!==false;
}

/**
* @return bool
*/
public function isInvalidInputResponse(){
return array_search(static::INVALID_INPUT_RESPONSE, $this->errors)!==false;
}

/**
* @return bool
*/
public function isUnknownError(){
return $this->isFailure() && array_diff($this->errors, [static::INVALID_INPUT_RESPONSE, static::INVALID_INPUT_SECRET, static::MISSING_INPUT_SECRET, static::MISSING_INPUT_RESPONSE]) > 0;
}

}
37 changes: 26 additions & 11 deletions test/Google/Test/RecaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,6 @@ public function testInvalidJSON(){

}

public function testPartialJSON(){

$this->setExpectedException("Google\ReCaptchaException");

$httpAdapter = $this->getMock("Google\HttpClientGetAdapter");
$httpAdapter->expects($this->once())->method('get')->will($this->returnValue(json_encode(["success"=>false])));

$recaptcha = new ReCaptcha("good secret", $httpAdapter);
$recaptcha->validate("any token");
}

public function testCustomHttpClientExceptionHandling(){

$httpException = new \Exception();
Expand Down Expand Up @@ -155,6 +144,32 @@ public function testNonExpectedJSONResponse(){
$recaptcha->validate("any token");
}

/**
* This behaviour is given off by google for some reason...
*/
public function testMissingErrorCodes(){

$httpAdapter = $this->getMock("Google\HttpClientGetAdapter");
$httpAdapter->expects($this->once())->method('get')->will($this->returnValue(json_encode(["success"=>false])));

$recaptcha = new ReCaptcha("some secret", $httpAdapter);
$response = $recaptcha->validate("any token");

$this->assertThat($response->isFailure(), $this->isTrue());
$this->assertThat($response->isUnknownError(), $this->isTrue());
}

public function testUnknownErrorCodes(){
$httpAdapter = $this->getMock("Google\HttpClientGetAdapter");
$httpAdapter->expects($this->once())->method('get')->will($this->returnValue(json_encode(["success"=>false,"error-codes"=>["Something I made up"]])));

$recaptcha = new ReCaptcha("some secret", $httpAdapter);
$response = $recaptcha->validate("any token");

$this->assertThat($response->isFailure(), $this->isTrue());
$this->assertThat($response->isUnknownError(), $this->isTrue());
}


private function validUrl()
{
Expand Down

0 comments on commit b68ab44

Please sign in to comment.