本文整理汇总了Java中org.springframework.mock.web.MockHttpServletResponse.getContentAsByteArray方法的典型用法代码示例。如果您正苦于以下问题:Java MockHttpServletResponse.getContentAsByteArray方法的具体用法?Java MockHttpServletResponse.getContentAsByteArray怎么用?Java MockHttpServletResponse.getContentAsByteArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.mock.web.MockHttpServletResponse
的用法示例。
在下文中一共展示了MockHttpServletResponse.getContentAsByteArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTransformationProperties
import org.springframework.mock.web.MockHttpServletResponse; //导入方法依赖的package包/类
@Test
public void getTransformationProperties() throws Exception {
preInitNonCreationTests();
MvcResult result = mvc.perform(
get(GET_PROPERTIES_VALID_URL)
).andDo(print())
.andExpect(status().is(200))
.andExpect(content().contentType(DEFAULT_CHARSET_HAL_JSON))
.andExpect(jsonPath("$.properties").isArray())
.andExpect(jsonPath("$.properties").isNotEmpty())
.andExpect(jsonPath("$.properties[0].key").isString())
.andExpect(jsonPath("$.properties[0].type").isString())
.andExpect(jsonPath("$.properties[0].description").isString())
.andExpect(jsonPath("$.properties[0].required").isBoolean())
.andExpect(jsonPath("$.links[0].rel").value("self"))
.andExpect(jsonPath("$.links[0].href")
.value("http://localhost/api/csars/kubernetes-cluster/transformations/p-a/properties"))
.andReturn();
MockHttpServletResponse response = result.getResponse();
String responseJson = new String(response.getContentAsByteArray());
String[] values = JsonPath.parse(responseJson).read("$.properties[*].value", String[].class);
long nullCount = Arrays.asList(values).stream().filter(Objects::isNull).count();
long testCount = Arrays.asList(values).stream().filter(e -> e != null && e.equals(PROPERTY_TEST_DEFAULT_VALUE)).count();
assertEquals(7, nullCount);
assertEquals(1, testCount);
}
示例2: makeHttpRequest
import org.springframework.mock.web.MockHttpServletResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse makeHttpRequest(HttpBody entity, long startTime) {
logger.info("making mock http client request: {} - {}", request.getMethod(), getRequestUri());
MockHttpServletRequest req = requestBuilder.buildRequest(getServletContext());
byte[] bytes;
if (entity != null) {
bytes = entity.getBytes();
req.setContentType(entity.getContentType());
if (entity.isMultiPart()) {
for (MultiPartItem item : entity.getParts()) {
MockMultiPart part = new MockMultiPart(item);
req.addPart(part);
if (!part.isFile()) {
req.addParameter(part.getName(), part.getValue());
}
}
} else if (entity.isUrlEncoded()) {
req.addParameters(entity.getParameters());
} else {
req.setContent(bytes);
}
} else {
bytes = null;
}
MockHttpServletResponse res = new MockHttpServletResponse();
logRequest(req, bytes);
try {
getServlet(request).service(req, res);
} catch (Exception e) {
throw new RuntimeException(e);
}
long responseTime = getResponseTime(startTime);
bytes = res.getContentAsByteArray();
logResponse(res, bytes);
HttpResponse response = new HttpResponse(responseTime);
response.setUri(getRequestUri());
response.setBody(bytes);
response.setStatus(res.getStatus());
for (Cookie c : res.getCookies()) {
com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
cookie.put(DOMAIN, c.getDomain());
cookie.put(PATH, c.getPath());
cookie.put(SECURE, c.getSecure() + "");
cookie.put(MAX_AGE, c.getMaxAge() + "");
cookie.put(VERSION, c.getVersion() + "");
response.addCookie(cookie);
}
for (String headerName : res.getHeaderNames()) {
response.putHeader(headerName, res.getHeaders(headerName));
}
return response;
}