|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -48,6 +48,7 @@ import org.springframework.core.io.Resource; |
|
|
|
import org.springframework.core.io.ResourceLoader; |
|
|
|
import org.springframework.core.io.ResourceLoader; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaTypeFactory; |
|
|
|
import org.springframework.http.MediaTypeFactory; |
|
|
|
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.ClassUtils; |
|
|
|
import org.springframework.util.ClassUtils; |
|
|
|
import org.springframework.util.MimeType; |
|
|
|
import org.springframework.util.MimeType; |
|
|
|
@ -129,14 +130,17 @@ public class MockServletContext implements ServletContext { |
|
|
|
|
|
|
|
|
|
|
|
private final Set<String> declaredRoles = new LinkedHashSet<>(); |
|
|
|
private final Set<String> declaredRoles = new LinkedHashSet<>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
private Set<SessionTrackingMode> sessionTrackingModes; |
|
|
|
private Set<SessionTrackingMode> sessionTrackingModes; |
|
|
|
|
|
|
|
|
|
|
|
private final SessionCookieConfig sessionCookieConfig = new MockSessionCookieConfig(); |
|
|
|
private final SessionCookieConfig sessionCookieConfig = new MockSessionCookieConfig(); |
|
|
|
|
|
|
|
|
|
|
|
private int sessionTimeout; |
|
|
|
private int sessionTimeout; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
private String requestCharacterEncoding; |
|
|
|
private String requestCharacterEncoding; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
private String responseCharacterEncoding; |
|
|
|
private String responseCharacterEncoding; |
|
|
|
|
|
|
|
|
|
|
|
private final Map<String, MediaType> mimeTypes = new LinkedHashMap<>(); |
|
|
|
private final Map<String, MediaType> mimeTypes = new LinkedHashMap<>(); |
|
|
|
@ -165,7 +169,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
* and no base path. |
|
|
|
* and no base path. |
|
|
|
* @param resourceLoader the ResourceLoader to use (or null for the default) |
|
|
|
* @param resourceLoader the ResourceLoader to use (or null for the default) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockServletContext(ResourceLoader resourceLoader) { |
|
|
|
public MockServletContext(@Nullable ResourceLoader resourceLoader) { |
|
|
|
this("", resourceLoader); |
|
|
|
this("", resourceLoader); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -178,7 +182,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
* @param resourceLoader the ResourceLoader to use (or null for the default) |
|
|
|
* @param resourceLoader the ResourceLoader to use (or null for the default) |
|
|
|
* @see #registerNamedDispatcher |
|
|
|
* @see #registerNamedDispatcher |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) { |
|
|
|
public MockServletContext(String resourceBasePath, @Nullable ResourceLoader resourceLoader) { |
|
|
|
this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); |
|
|
|
this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); |
|
|
|
this.resourceBasePath = resourceBasePath; |
|
|
|
this.resourceBasePath = resourceBasePath; |
|
|
|
|
|
|
|
|
|
|
|
@ -262,6 +266,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public String getMimeType(String filePath) { |
|
|
|
public String getMimeType(String filePath) { |
|
|
|
String extension = StringUtils.getFilenameExtension(filePath); |
|
|
|
String extension = StringUtils.getFilenameExtension(filePath); |
|
|
|
if (this.mimeTypes.containsKey(extension)) { |
|
|
|
if (this.mimeTypes.containsKey(extension)) { |
|
|
|
@ -285,6 +290,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public Set<String> getResourcePaths(String path) { |
|
|
|
public Set<String> getResourcePaths(String path) { |
|
|
|
String actualPath = (path.endsWith("/") ? path : path + "/"); |
|
|
|
String actualPath = (path.endsWith("/") ? path : path + "/"); |
|
|
|
Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath)); |
|
|
|
Resource resource = this.resourceLoader.getResource(getResourceLocation(actualPath)); |
|
|
|
@ -305,12 +311,15 @@ public class MockServletContext implements ServletContext { |
|
|
|
return resourcePaths; |
|
|
|
return resourcePaths; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException ex) { |
|
|
|
catch (IOException ex) { |
|
|
|
logger.warn("Couldn't get resource paths for " + resource, ex); |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
|
|
|
|
logger.warn("Could not get resource paths for " + resource, ex); |
|
|
|
|
|
|
|
} |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public URL getResource(String path) throws MalformedURLException { |
|
|
|
public URL getResource(String path) throws MalformedURLException { |
|
|
|
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); |
|
|
|
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); |
|
|
|
if (!resource.exists()) { |
|
|
|
if (!resource.exists()) { |
|
|
|
@ -323,12 +332,15 @@ public class MockServletContext implements ServletContext { |
|
|
|
throw ex; |
|
|
|
throw ex; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException ex) { |
|
|
|
catch (IOException ex) { |
|
|
|
logger.warn("Couldn't get URL for " + resource, ex); |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
|
|
|
|
logger.warn("Could not get URL for " + resource, ex); |
|
|
|
|
|
|
|
} |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public InputStream getResourceAsStream(String path) { |
|
|
|
public InputStream getResourceAsStream(String path) { |
|
|
|
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); |
|
|
|
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); |
|
|
|
if (!resource.exists()) { |
|
|
|
if (!resource.exists()) { |
|
|
|
@ -338,7 +350,9 @@ public class MockServletContext implements ServletContext { |
|
|
|
return resource.getInputStream(); |
|
|
|
return resource.getInputStream(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException ex) { |
|
|
|
catch (IOException ex) { |
|
|
|
logger.warn("Couldn't open InputStream for " + resource, ex); |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
|
|
|
|
logger.warn("Could not open InputStream for " + resource, ex); |
|
|
|
|
|
|
|
} |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -406,8 +420,9 @@ public class MockServletContext implements ServletContext { |
|
|
|
registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); |
|
|
|
registerNamedDispatcher(this.defaultServletName, new MockRequestDispatcher(this.defaultServletName)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
@Deprecated |
|
|
|
@Deprecated |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public Servlet getServlet(String name) { |
|
|
|
public Servlet getServlet(String name) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -441,13 +456,16 @@ public class MockServletContext implements ServletContext { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public String getRealPath(String path) { |
|
|
|
public String getRealPath(String path) { |
|
|
|
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); |
|
|
|
Resource resource = this.resourceLoader.getResource(getResourceLocation(path)); |
|
|
|
try { |
|
|
|
try { |
|
|
|
return resource.getFile().getAbsolutePath(); |
|
|
|
return resource.getFile().getAbsolutePath(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IOException ex) { |
|
|
|
catch (IOException ex) { |
|
|
|
logger.warn("Couldn't determine real path of resource " + resource, ex); |
|
|
|
if (logger.isWarnEnabled()) { |
|
|
|
|
|
|
|
logger.warn("Could not determine real path of resource " + resource, ex); |
|
|
|
|
|
|
|
} |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -484,6 +502,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public Object getAttribute(String name) { |
|
|
|
public Object getAttribute(String name) { |
|
|
|
Assert.notNull(name, "Attribute name must not be null"); |
|
|
|
Assert.notNull(name, "Attribute name must not be null"); |
|
|
|
return this.attributes.get(name); |
|
|
|
return this.attributes.get(name); |
|
|
|
@ -495,7 +514,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void setAttribute(String name, Object value) { |
|
|
|
public void setAttribute(String name, @Nullable Object value) { |
|
|
|
Assert.notNull(name, "Attribute name must not be null"); |
|
|
|
Assert.notNull(name, "Attribute name must not be null"); |
|
|
|
if (value != null) { |
|
|
|
if (value != null) { |
|
|
|
this.attributes.put(name, value); |
|
|
|
this.attributes.put(name, value); |
|
|
|
@ -521,6 +540,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public ClassLoader getClassLoader() { |
|
|
|
public ClassLoader getClassLoader() { |
|
|
|
return ClassUtils.getDefaultClassLoader(); |
|
|
|
return ClassUtils.getDefaultClassLoader(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -630,6 +650,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
* @see javax.servlet.ServletContext#getServletRegistration(java.lang.String) |
|
|
|
* @see javax.servlet.ServletContext#getServletRegistration(java.lang.String) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public ServletRegistration getServletRegistration(String servletName) { |
|
|
|
public ServletRegistration getServletRegistration(String servletName) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -668,6 +689,7 @@ public class MockServletContext implements ServletContext { |
|
|
|
* @see javax.servlet.ServletContext#getFilterRegistration(java.lang.String) |
|
|
|
* @see javax.servlet.ServletContext#getFilterRegistration(java.lang.String) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
|
|
|
|
@Nullable |
|
|
|
public FilterRegistration getFilterRegistration(String filterName) { |
|
|
|
public FilterRegistration getFilterRegistration(String filterName) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|