Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Changed errorCodes to a Collection and adding now all error codes ins…
Browse files Browse the repository at this point in the history
…tead of the first available
  • Loading branch information
pascal-zarrad committed Jan 2, 2017
1 parent e7cd5bd commit d8d333d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion GCaptchaValidator.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<groupId>com.github.playerforcehd</groupId>
<artifactId>gcaptchavalidator</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<packaging>jar</packaging>

<name>GCaptchaValidator</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public enum CaptchaValidationError {
* @param errorCode The error code which should be grabbed
* @return The enum which represents the errorCode string
*/
public static CaptchaValidationError parseGoogleJSonErrorCoede(String errorCode) {
public static CaptchaValidationError parseGoogleJSonErrorCode(String errorCode) {
for (CaptchaValidationError captchaValidationError : values()) {
if (captchaValidationError.getOriginalJSon().equals(errorCode)) {
return captchaValidationError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import com.google.gson.JsonParser;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collection;

/**
* The response from the Google SiteKey servers.
* Variables are named like the original JSon values but in a Java convention conform format.
Expand Down Expand Up @@ -60,18 +63,18 @@ public class CaptchaValidationResult {
private String hostName;

/**
* When success is false, error codes are available in this arrays.
* When success is false, error codes are available in this collection.
*
* @see <a href="https://developers.google.com/recaptcha/docs/verify#error-code-reference</a>
*/
@Getter
private CaptchaValidationError errorCode;
private Collection<CaptchaValidationError> errorCodes;

public CaptchaValidationResult(boolean success, String challengeTS, String hostName, CaptchaValidationError errorCode) {
public CaptchaValidationResult(boolean success, String challengeTS, String hostName, Collection<CaptchaValidationError> errorCode) {
this.success = success;
this.challengeTS = challengeTS;
this.hostName = hostName;
this.errorCode = errorCode;
this.errorCodes = errorCode;
}

/**
Expand All @@ -92,15 +95,14 @@ public static CaptchaValidationResult deserializeJSon(String json) {
if (jsonObject.get("hostname") != null) {
hostName = jsonObject.get("hostname").getAsString();
}
String errorCode = null;
Collection<CaptchaValidationError> errorCodes = new ArrayList<>();
if (jsonObject.get("error-codes") != null) {
errorCode = jsonObject.get("error-codes").getAsString();
}
CaptchaValidationError captchaValidationError = null;
if (errorCode != null) {
captchaValidationError = CaptchaValidationError.parseGoogleJSonErrorCoede(errorCode);
JsonElement errorList = jsonObject.get("error-codes");
for (int i = errorList.getAsJsonArray().size() - 1; i >= 0; i--) {
errorCodes.add(CaptchaValidationError.parseGoogleJSonErrorCode(errorList.getAsJsonArray().get(i).getAsString()));
}
}
return new CaptchaValidationResult(success, challengeTS, hostName, captchaValidationError);
return new CaptchaValidationResult(success, challengeTS, hostName, errorCodes);
}

/**
Expand Down
48 changes: 43 additions & 5 deletions src/test/java/GCaptchaValidatorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.playerforcehd.gcaptchavalidator.CaptchaValidationException;
import com.github.playerforcehd.gcaptchavalidator.GCaptchaValidator;
import com.github.playerforcehd.gcaptchavalidator.captchaconfiguration.CaptchaValidationConfiguration;
import com.github.playerforcehd.gcaptchavalidator.captchaverification.CaptchaValidationError;
import com.github.playerforcehd.gcaptchavalidator.captchaverification.CaptchaValidationRequest;
import com.github.playerforcehd.gcaptchavalidator.captchaverification.CaptchaValidationResult;
import com.github.playerforcehd.gcaptchavalidator.util.Callback;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void testGCaptchaValidationSynchronousWithRemoteIPSet() {
assertTrue("True request was made but success is false", result.isSuccess());
assertNotNull("ChallengeTS is null, but true request was made.", result.getChallengeTS());
assertNotNull("HostName is not null, but true request was made.", result.getHostName());
assertNull("ErrorCode is not null, but true request was made.", result.getErrorCode());
assertTrue("ErrorCodes is not empty, but true request was made.", result.getErrorCodes().isEmpty());
} catch (IOException e) {
e.printStackTrace();
fail("Failed with an IOException");
Expand Down Expand Up @@ -112,7 +113,7 @@ public void testGCaptchaValidationSynchronousWithoutRemoteIPSet() {
assertTrue("True request was made but success is false", result.isSuccess());
assertNotNull("ChallengeTS is null, but true request was made.", result.getChallengeTS());
assertNotNull("HostName is not null, but true request was made.", result.getHostName());
assertNull("ErrorCode is not null, but true request was made.", result.getErrorCode());
assertTrue("ErrorCodes is not empty, but true request was made.", result.getErrorCodes().isEmpty());
} catch (IOException e) {
e.printStackTrace();
fail("Failed with an IOException");
Expand All @@ -139,7 +140,7 @@ public void testGCaptchaValidationSynchronousWithoutRemoteIPSetAndWrongResponse(
CaptchaValidationResult result = request.fetchSync();
assertNotNull("CaptchaValidationResult (result) is null, but validation has runned!", result);
assertFalse("True request was made but success is false", result.isSuccess());
assertNotNull("ErrorCode is null, but false request was made.", result.getErrorCode());
assertFalse("ErrorCodes is empty, but false request was made.", result.getErrorCodes().isEmpty());
} catch (IOException e) {
e.printStackTrace();
fail("Failed with an IOException");
Expand Down Expand Up @@ -169,7 +170,7 @@ public void invoke(CaptchaValidationResult result) {
assertTrue("True request was made but success is false", result.isSuccess());
assertNotNull("ChallengeTS is null, but true request was made.", result.getChallengeTS());
assertNotNull("HostName is not null, but true request was made.", result.getHostName());
assertNull("ErrorCode is not null, but true request was made.", result.getErrorCode());
assertTrue("ErrorCodes is not empty, but true request was made.", result.getErrorCodes().isEmpty());
countDownLatch.countDown();
}

Expand Down Expand Up @@ -205,7 +206,7 @@ public void testGCaptchaValidationAsynchronousWithoutRemoteIPSetAndWrongResponse
public void invoke(CaptchaValidationResult result) {
assertNotNull("CaptchaValidationResult (result) is null, but validation has runned!", result);
assertFalse("True request was made but success is false", result.isSuccess());
assertNotNull("ErrorCode is null, but false request was made.", result.getErrorCode());
assertFalse("ErrorCodes is empty, but false request was made.", result.getErrorCodes().isEmpty());
countDownLatch.countDown();
}

Expand All @@ -222,4 +223,41 @@ public void fail(Throwable throwable) {
e.printStackTrace();
}
}

@Test
public void testGCaptchaValidationSynchronousWithoutAnything() {
CaptchaValidationConfiguration configuration = GCaptchaValidator.createConfigurationBuilder()
.withSecret("")
.withResponse("")
.build();
assertNotNull("CaptchaValidationConfiguration (configuration) is null, but has been initialized through builder!", configuration);
CaptchaValidationRequest request = GCaptchaValidator.createRequest(configuration);
assertNotNull("CaptchaValidationRequest (request) is null, but has been initialized through GCaptchaValidator.createRequest(configuration)", request);
try {
CaptchaValidationResult result = request.fetchSync();
boolean missingSecret = false;
boolean missingResponse = false;
for (CaptchaValidationError captchaValidationError : result.getErrorCodes()) {
if (captchaValidationError.equals(CaptchaValidationError.MISSING_INPUT_SECRET)) {
missingSecret = true;
continue;
}
if (captchaValidationError.equals(CaptchaValidationError.MISSING_INPUT_RESPONSE)) {
missingResponse = true;
}
}
if (missingSecret) {
assertTrue("Result does not include '" + CaptchaValidationError.INVALID_INPUT_SECRET.toString() + "', but the response was empty", result.getErrorCodes().contains(CaptchaValidationError.MISSING_INPUT_SECRET));
}
if (missingResponse) {
assertTrue("Result does not include '" + CaptchaValidationError.INVALID_INPUT_RESPONSE.toString() + "', but the response was empty", result.getErrorCodes().contains(CaptchaValidationError.MISSING_INPUT_RESPONSE));
}
} catch (IOException e) {
e.printStackTrace();
fail("Failed with an IOException");
} catch (CaptchaValidationException e) {
e.printStackTrace();
fail("Failed with an CaptchaValidationException");
}
}
}

0 comments on commit d8d333d

Please sign in to comment.