本文整理汇总了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());
}
示例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);
}
}
}