本文整理匯總了Java中org.springframework.mock.web.MockHttpServletRequest.setContent方法的典型用法代碼示例。如果您正苦於以下問題:Java MockHttpServletRequest.setContent方法的具體用法?Java MockHttpServletRequest.setContent怎麽用?Java MockHttpServletRequest.setContent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.mock.web.MockHttpServletRequest
的用法示例。
在下文中一共展示了MockHttpServletRequest.setContent方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testInvokeMultiPartEntity
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void testInvokeMultiPartEntity() throws IOException
{
ResourceWithMetadata entityResource = locator.locateEntityResource(api,"multiparttest", HttpMethod.POST);
assertNotNull(entityResource);
MultiPartResourceAction.Create<?> resource = (MultiPartResourceAction.Create<?>) entityResource.getResource();
File file = TempFileProvider.createTempFile("ParamsExtractorTests-", ".txt");
PrintWriter writer = new PrintWriter(file);
writer.println("Multipart Mock test2.");
writer.close();
MultiPartRequest reqBody = MultiPartBuilder.create()
.setFileData(new FileData(file.getName(), file, MimetypeMap.MIMETYPE_TEXT_PLAIN))
.build();
MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", "");
mockRequest.setContent(reqBody.getBody());
mockRequest.setContentType(reqBody.getContentType());
String out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api,null, NOT_USED, resource.create(new FormData(mockRequest), NOT_USED, callBack)));
assertTrue("There must be json output", StringUtils.startsWith(out, "{\"entry\":"));
}
示例2: verifyRequestBody
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void verifyRequestBody() {
final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
+ "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
+ "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
final MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(body.getBytes());
final SamlService impl = new SamlServiceFactory().createService(request);
assertEquals("artifact", impl.getArtifactId());
assertEquals("_192.168.16.51.1024506224022", impl.getRequestID());
}
示例3: verifyRequestBody
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void verifyRequestBody() {
final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
+ "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
+ "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
final MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(body.getBytes());
final SamlService impl = SamlService.createServiceFrom(request);
assertEquals("artifact", impl.getArtifactId());
assertEquals("_192.168.16.51.1024506224022", impl.getRequestID());
}
示例4: verifyRequestBody
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void verifyRequestBody() {
final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
+ "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
+ "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
final MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(body.getBytes());
final SamlService impl = new SamlServiceFactory().createService(request);
assertEquals("artifact", impl.getArtifactId());
assertEquals("_192.168.16.51.1024506224022", impl.getRequestID());
}
示例5: verifySerializeASamlServiceToJson
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void verifySerializeASamlServiceToJson() throws IOException {
final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
+ "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
+ "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
final MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(body.getBytes());
final SamlService serviceWritten = new SamlServiceFactory().createService(request);
MAPPER.writeValue(JSON_FILE, serviceWritten);
final SamlService serviceRead = MAPPER.readValue(JSON_FILE, SamlService.class);
assertEquals(serviceWritten, serviceRead);
}
示例6: testRequestBody
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void testRequestBody() {
final String body = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<SOAP-ENV:Header/><SOAP-ENV:Body><samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" "
+ "MinorVersion=\"1\" RequestID=\"_192.168.16.51.1024506224022\" IssueInstant=\"2002-06-19T17:03:44.022Z\">"
+ "<samlp:AssertionArtifact>artifact</samlp:AssertionArtifact></samlp:Request></SOAP-ENV:Body></SOAP-ENV:Envelope>";
final MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(body.getBytes());
final SamlService impl = SamlService.createServiceFrom(request);
assertEquals("artifact", impl.getArtifactId());
assertEquals("_192.168.16.51.1024506224022", impl.getRequestID());
}
示例7: buildRequestLog_WithCorrectBody_ReturnStringRequestWithUriAndBody
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void buildRequestLog_WithCorrectBody_ReturnStringRequestWithUriAndBody() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod(HttpMethod.GET.name());
request.setRequestURI("/dummy");
request.setContent("test".getBytes());
String log = FILTER.buildRequestLog(new ContentCachingRequestWrapper(request));
assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n > Content-Length: 4\ntest"));
}
示例8: buildRequestLog_WithBadCacheRequest_ReturnStringRequestWithUriAndUnknownBody
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void buildRequestLog_WithBadCacheRequest_ReturnStringRequestWithUriAndUnknownBody() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod(HttpMethod.GET.name());
request.setRequestURI("/dummy");
request.setContent("test".getBytes());
String log = FILTER.buildRequestLog(request);
assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n > Content-Length: 4\n[unknown]"));
}
示例9: buildRequestLog_WithUnreadableBody_ReturnStringRequestWithUriAndUnknownBody
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void buildRequestLog_WithUnreadableBody_ReturnStringRequestWithUriAndUnknownBody() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod(HttpMethod.GET.name());
request.setRequestURI("/dummy");
request.setContent(nextBytes(4));
ContentCachingRequestWrapper requestWrapper = spy(new ContentCachingRequestWrapper(request));
when(requestWrapper.getReader()).thenThrow(new UnsupportedEncodingException());
String log = FILTER.buildRequestLog(requestWrapper);
assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n > Content-Length: 4\n[unknown]"));
}
示例10: executeMethod
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
/**
* Executes WebDAV method for testing
* <p>
* Sets content to request from a test file
*
* @param methodName Method name to prepare, should be initialized (PUT, LOCK, UNLOCK are supported)
* @param fileName the name of the file set to the context, can be used with path, i.e. "path/to/file/fileName.txt"
* @param content If <b>not null</b> adds test content to the request
* @param headers to set to request, can be null
* @throws Exception
*/
private void executeMethod(String methodName, String fileName, byte[] content, Map<String, String> headers) throws Exception
{
if (methodName == WebDAV.METHOD_PUT)
method = new PutMethod();
else if (methodName == WebDAV.METHOD_LOCK)
method = new LockMethod();
else if (methodName == WebDAV.METHOD_UNLOCK)
method = new UnlockMethod();
if (method != null)
{
request = new MockHttpServletRequest(methodName, "/alfresco/webdav/" + fileName);
response = new MockHttpServletResponse();
request.setServerPort(8080);
request.setServletPath("/webdav");
if (content != null)
{
request.setContent(content);
}
if (headers != null && !headers.isEmpty())
{
for (String key : headers.keySet())
{
request.addHeader(key, headers.get(key));
}
}
method.setDetails(request, response, webDAVHelper, companyHomeNodeRef);
method.execute();
}
}
示例11: executeMethod
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
/**
* Executes WebDAV method for testing
* <p>
* Sets content to request from a test file
*
* @param methodName Method to prepare, should be initialized (PUT, LOCK, UNLOCK are supported)
* @param fileName the name of the file set to the context, can be used with path, i.e. "path/to/file/fileName.txt"
* @param content If <b>not null</b> adds test content to the request
* @param headers to set to request, can be null
* @throws Exception
*/
private void executeMethod(String methodName, String fileName, byte[] content, Map<String, String> headers) throws Exception
{
if (methodName == WebDAV.METHOD_PUT)
method = new PutMethod();
else if (methodName == WebDAV.METHOD_LOCK)
method = new LockMethod();
else if (methodName == WebDAV.METHOD_UNLOCK)
method = new UnlockMethod();
if (method != null)
{
request = new MockHttpServletRequest(methodName, "/alfresco/webdav/" + fileName);
response = new MockHttpServletResponse();
request.setServerPort(8080);
request.setServletPath("/webdav");
if (content != null)
{
request.setContent(content);
}
if (headers != null && !headers.isEmpty())
{
for (String key : headers.keySet())
{
request.addHeader(key, headers.get(key));
}
}
method.setDetails(request, response, webDAVHelper, companyHomeNodeRef);
method.execute();
}
}
示例12: makeHttpRequest
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的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;
}
示例13: checkLockedNodeTestWork
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
private void checkLockedNodeTestWork() throws WebDAVServerException
{
req = new MockHttpServletRequest();
resp = new MockHttpServletResponse();
String rootPath = "/app:company_home";
String storeName = "workspace://SpacesStore";
StoreRef storeRef = new StoreRef(storeName);
NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef);
List<NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, rootPath, null, namespaceService, false);
NodeRef defaultRootNode = nodeRefs.get(0);
lockMethod = new LockMethod();
NodeRef rootNodeRef = tenantService.getRootNode(nodeService, searchService, namespaceService, rootPath, defaultRootNode);
String strPath = "/" + "testLockedNode" + GUID.generate();
lockMethod.createExclusive = true;
lockMethod.setDetails(req, resp, webDAVHelper, rootNodeRef);
lockMethod.m_strPath = strPath;
// Lock the node (will create a new one).
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Object>()
{
@Override
public Object execute() throws Throwable {
lockMethod.executeImpl();
return null;
}
});
// Prepare for PUT
req.addHeader(WebDAV.HEADER_IF, "(<" + lockMethod.lockToken + ">)");
putMethod = new PutMethod();
putMethod.setDetails(req, resp, webDAVHelper, rootNodeRef);
putMethod.parseRequestHeaders();
putMethod.m_strPath = strPath;
String content = "test content stream";
req.setContent(content.getBytes());
// Issue a put request
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Object>()
{
@Override
public Object execute() throws Throwable
{
putMethod.executeImpl();
return null;
}
});
}
示例14: testMNT_10873
import org.springframework.mock.web.MockHttpServletRequest; //導入方法依賴的package包/類
@Test
public void testMNT_10873() throws Exception
{
String fileName = TEST_FILE_NAME + GUID.generate();
final MockHttpServletRequest lockRequest = new MockHttpServletRequest();
MockHttpServletResponse lockResponse = new MockHttpServletResponse();
lockRequest.addHeader(WebDAV.HEADER_TIMEOUT, WebDAV.SECOND + 5);
// set request uri to point to non-existent file
lockRequest.setRequestURI("/" + fileName);
String content = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:lockinfo xmlns:D=\"DAV:\"><D:lockscope xmlns:D=\"DAV:\">" +
"<D:exclusive xmlns:D=\"DAV:\"/></D:lockscope><D:locktype xmlns:D=\"DAV:\"><D:write xmlns:D=\"DAV:\"/></D:locktype>" +
"<D:owner xmlns:D=\"DAV:\">" + userName + "</D:owner></D:lockinfo>";
lockRequest.setContent(content.getBytes("UTF-8"));
lockMethod.setDetails(lockRequest, lockResponse, davHelper, folderNodeRef);
lockMethod.parseRequestHeaders();
lockMethod.parseRequestBody();
RetryingTransactionCallback<Void> lockExecuteImplCallBack = new RetryingTransactionCallback<Void>()
{
@Override
public Void execute() throws Throwable
{
lockMethod.executeImpl();
return null;
}
};
// lock node for 5 seconds
this.transactionService.getRetryingTransactionHelper().doInTransaction(lockExecuteImplCallBack);
assertEquals("Unexpected response status code.", HttpServletResponse.SC_CREATED, lockResponse.getStatus());
RetryingTransactionCallback<NodeRef> getNodeRefCallback = new RetryingTransactionCallback<NodeRef>()
{
@Override
public NodeRef execute() throws Throwable
{
return lockMethod.getNodeForPath(folderNodeRef, lockRequest.getRequestURI()).getNodeRef();
}
};
NodeRef nodeRef = this.transactionService.getRetryingTransactionHelper().doInTransaction(getNodeRefCallback);
assertTrue("NodeRef should exists.", nodeService.exists(nodeRef));
assertTrue("sys:webdavNoContent aspect should be applied on node.", nodeService.hasAspect(nodeRef, ContentModel.ASPECT_WEBDAV_NO_CONTENT));
// sleep for 6 seconds to ensure that timer was triggered
Thread.sleep(6000);
assertFalse("File should note exist in repo any more.", nodeService.exists(nodeRef));
assertFalse("File should note exist in trashcan.", nodeService.exists(new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, nodeRef.getId())));
}