本文整理汇总了Java中org.springframework.test.web.servlet.MvcResult.getResponse方法的典型用法代码示例。如果您正苦于以下问题:Java MvcResult.getResponse方法的具体用法?Java MvcResult.getResponse怎么用?Java MvcResult.getResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.test.web.servlet.MvcResult
的用法示例。
在下文中一共展示了MvcResult.getResponse方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTransformationProperties
import org.springframework.test.web.servlet.MvcResult; //导入方法依赖的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: createJsonFileFromSwaggerEndpoint
import org.springframework.test.web.servlet.MvcResult; //导入方法依赖的package包/类
@Test
public void createJsonFileFromSwaggerEndpoint() throws Exception {
String outputDir = Optional.ofNullable(System.getProperty("io.springfox.staticdocs.outputDir"))
.orElse("build/swagger");
System.err.println(outputDir);
MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
MockHttpServletResponse response = mvcResult.getResponse();
String swaggerJson = response.getContentAsString();
Files.createDirectories(Paths.get(outputDir));
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"),
StandardCharsets.UTF_8)) {
writer.write(swaggerJson);
}
}
示例3: Should_ReturnErrorMessage_ForUnmatchingUser
import org.springframework.test.web.servlet.MvcResult; //导入方法依赖的package包/类
@Test
@DisplayName("Should return error message for when user not existing in the database tries to login.")
public void Should_ReturnErrorMessage_ForUnmatchingUser() throws Exception {
User user = new User();
user.setEmail("[email protected]");
user.setPassword("foobar");
//
// Create JSON Representation for User object;
// Gson is a Java serialization/deserialization library to
// convert Java Objects into JSON and back
//
Gson gson = new Gson();
String jsonUser = gson.toJson(user);
//
// Mock the isValidUser method of userService
//
Mockito.when(this.userService.isValidUser("[email protected]", "foobar")).thenReturn(null);
//
// Invoke the method
//
MvcResult result = this.mockMvc.perform(
post("/account/login/process")
.contentType(MediaType.APPLICATION_JSON)
.content(jsonUser))
.andExpect(status().isOk())
.andReturn();
MockHttpServletResponse response = result.getResponse();
ObjectMapper mapper = new ObjectMapper();
ExecutionStatus responseObj = mapper.readValue(response.getContentAsString(), ExecutionStatus.class);
assertTrue(responseObj.getCode().equals("USER_LOGIN_UNSUCCESSFUL"));
assertTrue(responseObj.getMessage().equals("Username or password is incorrect. Please try again!"));
}
开发者ID:PacktPublishing,项目名称:Building-Web-Apps-with-Spring-5-and-Angular,代码行数:35,代码来源:UserAccountControllerTest.java
示例4: handle
import org.springframework.test.web.servlet.MvcResult; //导入方法依赖的package包/类
@Override
public void handle(MvcResult result) throws Exception {
MockHttpServletRequest request = result.getRequest();
MockHttpServletResponse response = result.getResponse();
logger.error("HTTP method: {}, status code: {}", request.getMethod(), response.getStatus());
}
示例5: should_upload_and_download_zipped_log
import org.springframework.test.web.servlet.MvcResult; //导入方法依赖的package包/类
@Test
public void should_upload_and_download_zipped_log() throws Throwable {
// given:
ClassLoader classLoader = CmdControllerTest.class.getClassLoader();
URL resource = classLoader.getResource("test-cmd-id.out.zip");
Path path = Paths.get(resource.getFile());
byte[] data = Files.readAllBytes(path);
CmdInfo cmdBase = new CmdInfo("test-zone-1", "test-agent-1", CmdType.RUN_SHELL, "~/hello.sh");
Cmd cmd = cmdService.create(cmdBase);
String originalFilename = cmd.getId() + ".out.zip";
MockMultipartFile zippedCmdLogPart = new MockMultipartFile("file", originalFilename, "application/zip", data);
MockMultipartFile cmdIdPart = new MockMultipartFile("cmdId", "", "text/plain", cmd.getId().getBytes());
// when: upload zipped cmd log
MockMultipartHttpServletRequestBuilder content = fileUpload("/cmd/log/upload")
.file(zippedCmdLogPart)
.file(cmdIdPart);
this.mockMvc.perform(content)
.andDo(print())
.andExpect(status().isOk());
// then: check upload file path and logging queue
Path zippedLogPath = Paths.get(cmdLogDir.toString(), originalFilename);
Assert.assertTrue(Files.exists(zippedLogPath));
Assert.assertEquals(data.length, Files.size(zippedLogPath));
// when: download uploaded zipped cmd log
MvcResult result = this.mockMvc.perform(get("/cmd/log/download")
.param("cmdId", cmd.getId()).param("index", Integer.toString(0)))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
// then:
MockHttpServletResponse response = result.getResponse();
Assert.assertEquals("application/zip", response.getContentType());
Assert.assertEquals(data.length, response.getContentLength());
Assert.assertTrue(response.getHeader("Content-Disposition").contains(originalFilename));
}