Groovy HttpBuilder - 获取失败响应的正文 [英] Groovy HttpBuilder - Get Body Of Failed Response

查看:284
本文介绍了Groovy HttpBuilder - 获取失败响应的正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use the Groovy HTTPBuilder to write an integration test that will verify a correct error message is returned in the body along with an HTTP 409 status message. However, I can't figure out how to actually access the body of the HTTP response in failure cases.

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

This is what the HTTP response from the server looks like:

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]"


推荐答案

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

I was recently just struggling with this while trying to integration test my REST endpoints using Spock. I used Sam's answer as inspiration and ended up improving on it so as to continue leveraging the auto-casting that HttpBuilder provides. After messing around for a while, I had the bright idea of just assigning the success handler closure to the failure handler to standardize the behavior regardless of what status code is returned.

client.handler.failure = client.handler.success

An它的实例:

...

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
            ...
        }
    }
}

这篇关于Groovy HttpBuilder - 获取失败响应的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆