Java源码示例:org.springframework.test.web.servlet.StubMvcResult

示例1
@Test
public void printHandlerNull() throws Exception {
	StubMvcResult mvcResult = new StubMvcResult(this.request, null, null, null, null, null, this.response);
	this.handler.handle(mvcResult);

	assertValue("Handler", "Type", null);
}
 
示例2
@Test
public void stringEncodingDetection() throws Exception {
	String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<person><name>Jürgen</name></person>";
	byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	StreamUtils.copy(bytes, response.getOutputStream());
	StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response);

	new XpathResultMatchers("/person/name", null).string("Jürgen").match(result);
}
 
示例3
@Test(expected = AssertionError.class)
public void prefixWithPayloadNotLongEnough() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(new String("test".getBytes("ISO-8859-1")));
	StubMvcResult result =  new StubMvcResult(null, null, null, null, null, null, response);

	new JsonPathResultMatchers("$.str").prefix("prefix").value("foo").match(result);
}
 
示例4
@Test
public void testHttpStatusCodeResultMatchers() throws Exception {

	List<AssertionError> failures = new ArrayList<>();

	for (HttpStatus status : HttpStatus.values()) {
		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		try {
			Method method = getMethodForHttpStatus(status);
			ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, this.matchers);
			try {
				matcher.match(mvcResult);
			}
			catch (AssertionError error) {
				failures.add(error);
			}
		}
		catch (Exception ex) {
			throw new Exception("Failed to obtain ResultMatcher for status " + status, ex);
		}
	}

	if (!failures.isEmpty()) {
		fail("Failed status codes: " + failures);
	}
}
 
示例5
@Test
public void statusRanges() throws Exception {
	for (HttpStatus status : HttpStatus.values()) {
		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		switch (status.series().value()) {
			case 1:
				this.matchers.is1xxInformational().match(mvcResult);
				break;
			case 2:
				this.matchers.is2xxSuccessful().match(mvcResult);
				break;
			case 3:
				this.matchers.is3xxRedirection().match(mvcResult);
				break;
			case 4:
				this.matchers.is4xxClientError().match(mvcResult);
				break;
			case 5:
				this.matchers.is5xxServerError().match(mvcResult);
				break;
			default:
				fail("Unexpected range for status code value " + status);
		}
	}
}
 
示例6
@Test
public void printHandlerNull() throws Exception {
	StubMvcResult mvcResult = new StubMvcResult(this.request, null, null, null, null, null, this.response);
	this.handler.handle(mvcResult);

	assertValue("Handler", "Type", null);
}
 
示例7
@Test
public void stringEncodingDetection() throws Exception {
	String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<person><name>Jürgen</name></person>";
	byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	StreamUtils.copy(bytes, response.getOutputStream());
	StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response);

	new XpathResultMatchers("/person/name", null).string("Jürgen").match(result);
}
 
示例8
@Test(expected = AssertionError.class)
public void prefixWithPayloadNotLongEnough() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(new String("test".getBytes("ISO-8859-1")));
	StubMvcResult result =  new StubMvcResult(null, null, null, null, null, null, response);

	new JsonPathResultMatchers("$.str").prefix("prefix").value("foo").match(result);
}
 
示例9
@Test
public void testHttpStatusCodeResultMatchers() throws Exception {

	List<AssertionError> failures = new ArrayList<>();

	for (HttpStatus status : HttpStatus.values()) {
		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		try {
			Method method = getMethodForHttpStatus(status);
			ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, this.matchers);
			try {
				matcher.match(mvcResult);
			}
			catch (AssertionError error) {
				failures.add(error);
			}
		}
		catch (Exception ex) {
			throw new Exception("Failed to obtain ResultMatcher for status " + status, ex);
		}
	}

	if (!failures.isEmpty()) {
		fail("Failed status codes: " + failures);
	}
}
 
示例10
@Test
public void statusRanges() throws Exception {
	for (HttpStatus status : HttpStatus.values()) {
		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		switch (status.series().value()) {
			case 1:
				this.matchers.is1xxInformational().match(mvcResult);
				break;
			case 2:
				this.matchers.is2xxSuccessful().match(mvcResult);
				break;
			case 3:
				this.matchers.is3xxRedirection().match(mvcResult);
				break;
			case 4:
				this.matchers.is4xxClientError().match(mvcResult);
				break;
			case 5:
				this.matchers.is5xxServerError().match(mvcResult);
				break;
			default:
				fail("Unexpected range for status code value " + status);
		}
	}
}
 
示例11
@Test
public void printHandlerNull() throws Exception {
	StubMvcResult mvcResult = new StubMvcResult(this.request, null, null, null, null, null, this.response);
	this.handler.handle(mvcResult);

	assertValue("Handler", "Type", null);
}
 
示例12
@Test
public void stringEncodingDetection() throws Exception {
	String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
			"<person><name>Jürgen</name></person>";
	byte[] bytes = content.getBytes(Charset.forName("UTF-8"));
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	StreamUtils.copy(bytes, response.getOutputStream());
	StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response);

	new XpathResultMatchers("/person/name", null).string("Jürgen").match(result);
}
 
示例13
@Test
public void testHttpStatusCodeResultMatchers() throws Exception {

	List<AssertionError> failures = new ArrayList<AssertionError>();

	for(HttpStatus status : HttpStatus.values()) {
		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		try {
			Method method = getMethodForHttpStatus(status);
			ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, this.matchers);
			try {
				matcher.match(mvcResult);
			}
			catch (AssertionError error) {
				failures.add(error);
			}
		}
		catch (Exception ex) {
			throw new Exception("Failed to obtain ResultMatcher for status " + status, ex);
		}
	}

	if (!failures.isEmpty()) {
		fail("Failed status codes: " + failures);
	}
}
 
示例14
@Test
public void statusRanges() throws Exception {

	for(HttpStatus status : HttpStatus.values()) {

		MockHttpServletResponse response = new MockHttpServletResponse();
		response.setStatus(status.value());
		MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response);
		switch (status.series().value()) {
			case 1:
				this.matchers.is1xxInformational().match(mvcResult);
				break;
			case 2:
				this.matchers.is2xxSuccessful().match(mvcResult);
				break;
			case 3:
				this.matchers.is3xxRedirection().match(mvcResult);
				break;
			case 4:
				this.matchers.is4xxClientError().match(mvcResult);
				break;
			case 5:
				this.matchers.is5xxServerError().match(mvcResult);
				break;
			default:
				fail("Unexpected range for status code value " + status);
		}
	}
}
 
示例15
private MvcResult getMvcResult(ModelAndView modelAndView) {
	return new StubMvcResult(null, null, null, null, modelAndView, null, null);
}
 
示例16
private StubMvcResult getStubMvcResult() {
	FlashMap flashMap = new FlashMap();
	flashMap.put("good", "good");
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, flashMap, null);
	return mvcResult;
}
 
示例17
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	response.getWriter().print(new String(RESPONSE_CONTENT.getBytes(StandardCharsets.ISO_8859_1)));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
示例18
@Test(expected = AssertionError.class)
public void valueWithJsonPrefixNotConfigured() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").value("foo").match(result);
}
 
示例19
@Test(expected = AssertionError.class)
public void valueWithJsonWrongPrefix() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").prefix("wrong").value("foo").match(result);
}
 
示例20
@Test
public void valueWithJsonPrefix() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").prefix(jsonPrefix).value("foo").match(result);
}
 
示例21
private StubMvcResult createPrefixedStubMvcResult(String jsonPrefix) throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json");
	response.getWriter().print(jsonPrefix + new String(RESPONSE_CONTENT.getBytes("ISO-8859-1")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
示例22
private StubMvcResult getRedirectedUrlStubMvcResult(String redirectUrl) throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.sendRedirect(redirectUrl);
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
	return mvcResult;
}
 
示例23
private StubMvcResult getForwardedUrlStubMvcResult(String forwardedUrl) {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.setForwardedUrl(forwardedUrl);
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, null, response);
	return mvcResult;
}
 
示例24
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/json; charset=UTF-8");
	response.getWriter().print(new String(CONTENT.getBytes("UTF-8")));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
示例25
private MvcResult getMvcResult(ModelAndView modelAndView) {
	return new StubMvcResult(null, null, null, null, modelAndView, null, null);
}
 
示例26
private StubMvcResult getStubMvcResult() {
	FlashMap flashMap = new FlashMap();
	flashMap.put("good", "good");
	StubMvcResult mvcResult = new StubMvcResult(null, null, null, null, null, flashMap, null);
	return mvcResult;
}
 
示例27
private StubMvcResult getStubMvcResult() throws Exception {
	MockHttpServletResponse response = new MockHttpServletResponse();
	response.addHeader("Content-Type", "application/xml");
	response.getWriter().print(new String(RESPONSE_CONTENT.getBytes(StandardCharsets.ISO_8859_1)));
	return new StubMvcResult(null, null, null, null, null, null, response);
}
 
示例28
@Test(expected = AssertionError.class)
public void valueWithJsonPrefixNotConfigured() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").value("foo").match(result);
}
 
示例29
@Test(expected = AssertionError.class)
public void valueWithJsonWrongPrefix() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").prefix("wrong").value("foo").match(result);
}
 
示例30
@Test
public void valueWithJsonPrefix() throws Exception {
	String jsonPrefix = "prefix";
	StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix);
	new JsonPathResultMatchers("$.str").prefix(jsonPrefix).value("foo").match(result);
}