Groovy HttpBuilder - 获取失败的响应

时间:2013-11-13 23:24:32

标签: groovy httpbuilder

我正在尝试使用Groovy HTTPBuilder编写集成测试,该测试将验证正文中返回的正确错误消息以及HTTP 409状态消息。但是,我无法弄清楚如何在失败的情况下实际访问HTTP响应的主体。

http.request(ENV_URL, Method.POST, ContentType.TEXT) {
    uri.path = "/curate/${id}/submit"
    contentType = ContentType.JSON
    response.failure = { failresp_inner ->
        failresp = failresp_inner
    }
}

then:
assert failresp.status == 409
// I would like something like 
//assert failresp.data == "expected error message"

这是来自服务器的HTTP响应:

2013-11-13 18:17:58,726 DEBUG  wire - << "HTTP/1.1 409 Conflict[\r][\n]"
2013-11-13 18:17:58,726 DEBUG  wire - << "Date: Wed, 13 Nov 2013 23:17:58 GMT[\r][\n]"
2013-11-13 18:17:58,726 DEBUG  wire - << "Content-Type: text/plain[\r][\n]"
2013-11-13 18:17:58,726 DEBUG  wire - << "Transfer-Encoding: chunked[\r][\n]"
2013-11-13 18:17:58,727 DEBUG  wire - << "[\r][\n]"
2013-11-13 18:17:58,728 DEBUG  wire - << "E[\r][\n]"
2013-11-13 18:17:58,728 DEBUG  wire - << "expected error message"
2013-11-13 18:17:58,728 DEBUG  wire - << "[\r][\n]"
2013-11-13 18:17:58,728 DEBUG  wire - << "0[\r][\n]"
2013-11-13 18:17:58,728 DEBUG  wire - << "[\r][\n]"

3 个答案:

答案 0 :(得分:11)

我最近在尝试使用Spock集成测试我的REST端点时正在努力解决这个问题。我使用Sam的答案作为灵感并最终改进了它,以便继续利用HttpBuilder提供的自动转换。搞砸了一段时间之后,我明白了将成功处理程序闭包分配给失败处理程序以标准化行为,而不管返回什么状态代码。

client.handler.failure = client.handler.success

它的一个实例:

...

import static org.apache.http.HttpStatus.*

...

private RESTClient createClient(String username = null, String password = null) {
    def client = new RESTClient(BASE_URL)
    client.handler.failure = client.handler.success

    if(username != null)
        client.auth.basic(username, password)

    return client
}

...

def unauthenticatedClient = createClient()
def userClient = createClient(USER_USERNAME, USER_PASSWORD)
def adminClient = createClient(ADMIN_USERNAME, ADMIN_PASSWORD)

...

def 'get account'() {
    expect:
    // unauthenticated tries to get user's account
    unauthenticatedClient.get([path: "account/$USER_EMAIL"]).status == SC_UNAUTHENTICATED

    // user gets user's account
    with(userClient.get([path: "account/$USER_EMAIL"])) {
        status == SC_OK
        with(responseData) {
            email == USER_EMAIL
            ...
        }
    }

    // user tries to get user2's account
    with(userClient.get([path: "account/$USER2_EMAIL"])) {
        status == SC_FORBIDDEN
        with(responseData) {
            message.contains(USER_EMAIL)
            message.contains(USER2_EMAIL)
            ...
        }
    }

    // admin to get user's account
    with(adminClient.get([path: "account/$USER_EMAIL"])) {
        status == SC_OK
        with(responseData) {
            email == USER_EMAIL
            ...
        }
    }
}

答案 1 :(得分:7)

使用时是否有效:

response.failure = { resp, reader ->
    failstatus = resp.statusLine
    failresp   = reader.text
}

答案 2 :(得分:5)

当我开始使用HttpBuilder时,我也很挣扎。我想出的解决方案是定义HTTPBuilder成功和失败闭包,以返回如下所示的一致值:

HTTPBuilder http = new HTTPBuilder()
http.handler.failure = { resp, reader ->
    [response:resp, reader:reader]
}
http.handler.success = { resp, reader ->
    [response:resp, reader:reader]
}

如此定义,您的HTTPBuilder实例将始终返回包含响应对象(HttpResponseDecorator的实例)和 reader 对象的映射。您的请求将如下所示:

def map = http.request(ENV_URL, Method.POST, ContentType.TEXT) {
    uri.path = "/curate/${id}/submit"
    contentType = ContentType.JSON
}

def response = map['response']
def reader = map['reader']

assert response.status == 409

读者将是某种对象,它可以让您访问响应主体,您可以通过调用getClass()方法确定其类型:

println "reader type: ${reader.getClass()}"

读者对象的类型将由响应中的Content-Type标头确定。您可以通过在请求中添加“Accept”标头来具体告诉服务器您要返回的内容。