当前位置: 首页>>代码示例>>Java>>正文


Java Response.body方法代码示例

本文整理汇总了Java中com.jcabi.http.Response.body方法的典型用法代码示例。如果您正苦于以下问题:Java Response.body方法的具体用法?Java Response.body怎么用?Java Response.body使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.jcabi.http.Response的用法示例。


在下文中一共展示了Response.body方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: canGetEspecificUser

import com.jcabi.http.Response; //导入方法依赖的package包/类
/**
 * 
 * Verifica si el sistema está retornardo la representación correcta de un
 * recurso.
 * 
 * @throws IOException
 *             Si no puede realizar la petición
 * 
 */
@Test
public void canGetEspecificUser() throws IOException {
	/* Se realiza la petición */
	Request request = new JdkRequest(buildUrl("/users/1"))
			.method(JdkRequest.GET);
	Response response = request.fetch();

	/* Se verifican las propiedades dadas por la respuesta */
	Assert.assertNotNull("El response es nulo", response);

	String body = response.body();
	Assert.assertNotNull("El cuerpo del response es nulo", body);
	Assert.assertTrue("El cuerpo del response está vacio",
			body.length() > 0);
	Assert.assertEquals("El código de status no es el esperado", 200,
			response.status());

	/* Se verifican los valores que retorna la respuesta */
	Gson gson = new Gson();
	/* Se parsea el contenido del body. */
	User user = gson.fromJson(body, User.class);
	Assert.assertEquals("El name del usuario no es el esperado", "Juan",
			user.getName());
	Assert.assertEquals("El lastName usuario no es el esperado", "Ramírez",
			user.getLastName());
	Assert.assertEquals("El document del usuario no es el esperado",
			"1094891516", user.getDocument());
}
 
开发者ID:juanprq,项目名称:spark-simple-api,代码行数:38,代码来源:AppTest.java

示例2: assertResponse

import com.jcabi.http.Response; //导入方法依赖的package包/类
/**
 * Assert that the response matched the expected.
 *
 * @param restTest
 * @param response
 */
private void assertResponse(RestTest restTest, Response response) {
    int actualStatusCode = response.status();
    try {
        Assert.assertEquals("Unexpected REST response status code.  Status message: " + response.reason(), restTest.getExpectedStatusCode(),
                actualStatusCode);
    } catch (Error e) {
        if (actualStatusCode >= 400) {
            InputStream content = null;
            try {
                String payload = response.body();
                System.out.println("------ START ERROR PAYLOAD ------");
                if (payload.startsWith("{")) {
                    payload = payload.replace("\\r\\n", "\r\n").replace("\\t", "\t");
                }
                System.out.println(payload);
                System.out.println("------ END   ERROR PAYLOAD ------");
            } catch (Exception e1) {
            } finally {
                IOUtils.closeQuietly(content);
            }
        }
        throw e;
    }
    for (Entry<String, String> entry : restTest.getExpectedResponseHeaders().entrySet()) {
        String expectedHeaderName = entry.getKey();
        if (expectedHeaderName.startsWith("X-RestTest-"))
            continue;
        String expectedHeaderValue = entry.getValue();
        List<String> headers = response.headers().get(expectedHeaderName);

        Assert.assertNotNull("Expected header to exist but was not found: " + expectedHeaderName, headers);
        Assert.assertEquals(expectedHeaderValue, headers.get(0));
    }
    List<String> ctValueList = response.headers().get("Content-Type");
    if (ctValueList == null) {
        assertNoPayload(restTest, response);
    } else {
        String ctValueFirst = ctValueList.get(0);
        if (ctValueFirst.startsWith("application/json")) {
            assertJsonPayload(restTest, response);
        } else if (ctValueFirst.startsWith("text/plain") || ctValueFirst.startsWith("text/html")) {
            assertTextPayload(restTest, response);
        } else if (ctValueFirst.startsWith("application/xml") || ctValueFirst.startsWith("application/wsdl+xml")) {
            assertXmlPayload(restTest, response);
        } else {
            Assert.fail("Unsupported response payload type: " + ctValueFirst);
        }
    }
}
 
开发者ID:apiman,项目名称:apiman,代码行数:56,代码来源:TestPlanRunner.java


注:本文中的com.jcabi.http.Response.body方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。