Skip to content

Commit

Permalink
REST test: output response body when an error is not expected
Browse files Browse the repository at this point in the history
  • Loading branch information
tsegismont committed Mar 24, 2015
1 parent 5d7f0d1 commit ac3ca3b
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import java.util.concurrent.atomic.AtomicInteger

import org.junit.BeforeClass

import com.google.common.base.Charsets

import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient

Expand All @@ -33,10 +35,21 @@ class RESTTest {
static final String TENANT_PREFIX = UUID.randomUUID().toString()
static final AtomicInteger TENANT_ID_COUNTER = new AtomicInteger(0)
static RESTClient hawkularMetrics
static defaultFailureHandler

@BeforeClass
static void initClient() {
hawkularMetrics = new RESTClient("http://$baseURI/", ContentType.JSON)
defaultFailureHandler = hawkularMetrics.handler.failure
hawkularMetrics.handler.failure = { resp ->
if (resp.entity != null && resp.entity.contentLength != 0) {
println "Got error response: ${resp.statusLine}"
def baos = new ByteArrayOutputStream()
resp.entity.writeTo(baos)
println new String(baos.toByteArray(), Charsets.UTF_8)
}
defaultFailureHandler(resp)
}
}

static String nextTenantId() {
Expand All @@ -57,22 +70,30 @@ class RESTTest {
}

static def badPost(args, errorHandler) {
def originalFailureHandler = hawkularMetrics.handler.failure;
hawkularMetrics.handler.failure = defaultFailureHandler;
try {
def object = hawkularMetrics.post(args)
fail("Expected exception to be thrown")
return object
} catch (e) {
errorHandler(e)
} finally {
hawkularMetrics.handler.failure = originalFailureHandler;
}
}

static def badGet(args, errorHandler) {
def originalFailureHandler = hawkularMetrics.handler.failure;
hawkularMetrics.handler.failure = defaultFailureHandler;
try {
def object = hawkularMetrics.get(args)
fail("Expected exception to be thrown")
return object
} catch (e) {
errorHandler(e)
} finally {
hawkularMetrics.handler.failure = originalFailureHandler;
}
}
}

0 comments on commit ac3ca3b

Please sign in to comment.