Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Support config for network timeouts #27

Merged
merged 1 commit into from
May 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
build
.gradle
*.iml
out/
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Usage - ReCaptcha](#usage---recaptcha)
- [Edit the Configuration](#edit-the-configuration)
- [Proxy Server Configuration](#proxy-server-configuration)
- [Timeout Configuration](#timeout-configuration)
- [Use the Tag Library](#use-the-tag-library)
- [`<recaptcha:ifEnabled>`](#recaptchaifenabled)
- [`<recaptcha:ifDisabled>`](#recaptchaifdisabled)
Expand Down Expand Up @@ -93,6 +94,19 @@ Only the `server` property is required. The `port` will default to `80` if not s

Like other configurations, this can be placed at the top-level `recaptcha` entry, or it can be specified on a per-environment basis.

### Timeout Configuration

If there are issues connecting to Google for verifying the captcha, some of the network timeouts can be changed.

recaptcha:
timeoutConfig:
connectTimeout: 10000 // Timeout for making the network connection in millis. Defaults to 10000
readTimeout: 1000 // Timeout for waiting on the network response in millis. Defaults to 1000

`connectTimeout` and `readTimeout` can be specified together or independently of each other.

Like other configurations, this can be placed at the top-level `recaptcha` entry, or it can be specified on a per-environment basis.

## Use the Tag Library

### `<recaptcha:ifEnabled>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class RecaptchaService {
privateKey: config.privateKey,
includeNoScript: safeGetConfigValue('includeNoScript', true),
includeScript: safeGetConfigValue('includeScript', true),
timeoutConfig: config.timeoutConfig,
proxy: proxy)
}
recap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class ReCaptcha {
Boolean includeNoScript = false
Boolean includeScript = true

def timeoutConfig

AuthenticatorProxy proxy = null

/**
Expand Down Expand Up @@ -173,6 +175,9 @@ public class ReCaptcha {
post.queryParams.add("response", response)
post.queryParams.add("remoteip", remoteAddr)

post.connectTimeout = timeoutConfig?.connectTimeout ?: post.connectTimeout
post.readTimeout = timeoutConfig?.readTimeout ?: post.readTimeout

def responseObject = post.response

if (!responseObject) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/groovy/com/megatome/grails/recaptcha/net/Post.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ public class Post {
private static final log = LogFactory.getLog(this)
String url
QueryParams queryParams = new QueryParams(null)
int connectTimeout = 10000
int readTimeout = 1000
AuthenticatorProxy proxy = null
RestBuilder rest = null

public Post(Map options) {
options.each { k,v -> if (this.hasProperty(k)) { this."$k" = v} }
if (null == rest) {
if (proxy?.isConfigured()) {
rest = new RestBuilder(connectTimeout: 10000, readTimeout: 1000, proxy: proxy.proxy)
rest = new RestBuilder(connectTimeout: connectTimeout, readTimeout: readTimeout, proxy: proxy.proxy)
} else {
rest = new RestBuilder(connectTimeout: 10000, readTimeout: 1000)
rest = new RestBuilder(connectTimeout: connectTimeout, readTimeout: readTimeout)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ class ReCaptchaTests extends Specification {
stub.demand.setUrl() {}
stub.demand.setProxy() {}
stub.demand.getQueryParams(3..3) { new QueryParams(null) }
stub.demand.setConnectTimeout() {}
stub.demand.getConnectTimeout() { 10000 }
stub.demand.setReadTimeout() {}
stub.demand.getReadTimeout() { 1000 }
stub.demand.getResponse() { postText == null ? null : new JsonSlurper().parseText(postText) }

stub.use {
Expand Down
30 changes: 30 additions & 0 deletions src/test/groovy/com/megatome/grails/recaptcha/net/PostTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,34 @@ class PostTests extends Specification {
then:
post.response == null
}

def "Test with overridden timeouts"() {
when:
def post = new Post(url: "http://www.google.com", connectTimeout: 1234, readTimeout: 5678)

then:
post.url == "http://www.google.com"
post.rest.restTemplate.requestFactory?.connectTimeout == 1234
post.rest.restTemplate.requestFactory?.readTimeout == 5678
}

def "Test with overridden connect timeout"() {
when:
def post = new Post(url: "http://www.google.com", connectTimeout: 1234)

then:
post.url == "http://www.google.com"
post.rest.restTemplate.requestFactory?.connectTimeout == 1234
post.rest.restTemplate.requestFactory?.readTimeout == 1000
}

def "Test with overridden read timeout"() {
when:
def post = new Post(url: "http://www.google.com", readTimeout: 5678)

then:
post.url == "http://www.google.com"
post.rest.restTemplate.requestFactory?.connectTimeout == 10000
post.rest.restTemplate.requestFactory?.readTimeout == 5678
}
}