1 // Copyright 2011 Google Inc. All Rights Reserved. 2 3 package com.google.inject.servlet; 4 5 import com.google.common.collect.ImmutableMap; 6 import com.google.common.collect.Maps; 7 import java.io.Serializable; 8 import java.lang.reflect.InvocationHandler; 9 import java.lang.reflect.Method; 10 import java.lang.reflect.Proxy; 11 import java.util.Map; 12 import javax.servlet.FilterChain; 13 import javax.servlet.ServletRequest; 14 import javax.servlet.ServletResponse; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletRequestWrapper; 17 import javax.servlet.http.HttpServletResponse; 18 import javax.servlet.http.HttpSession; 19 20 /** 21 * Utilities for servlet tests. 22 * 23 * @author sameb@google.com (Sam Berlin) 24 */ 25 public class ServletTestUtils { 26 ServletTestUtils()27 private ServletTestUtils() {} 28 29 private static class ThrowingInvocationHandler implements InvocationHandler { 30 @Override invoke(Object proxy, Method method, Object[] args)31 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 32 throw new UnsupportedOperationException("No methods are supported on this object"); 33 } 34 } 35 36 /** Returns a FilterChain that does nothing. */ newNoOpFilterChain()37 public static FilterChain newNoOpFilterChain() { 38 return new FilterChain() { 39 @Override 40 public void doFilter(ServletRequest request, ServletResponse response) {} 41 }; 42 } 43 44 /** Returns a fake, HttpServletRequest which stores attributes in a HashMap. */ newFakeHttpServletRequest()45 public static HttpServletRequest newFakeHttpServletRequest() { 46 HttpServletRequest delegate = 47 (HttpServletRequest) 48 Proxy.newProxyInstance( 49 HttpServletRequest.class.getClassLoader(), 50 new Class[] {HttpServletRequest.class}, 51 new ThrowingInvocationHandler()); 52 53 return new HttpServletRequestWrapper(delegate) { 54 final Map<String, Object> attributes = Maps.newHashMap(); 55 final HttpSession session = newFakeHttpSession(); 56 57 @Override 58 public String getMethod() { 59 return "GET"; 60 } 61 62 @Override 63 public Object getAttribute(String name) { 64 return attributes.get(name); 65 } 66 67 @Override 68 public void setAttribute(String name, Object value) { 69 attributes.put(name, value); 70 } 71 72 @Override 73 public Map getParameterMap() { 74 return ImmutableMap.of(); 75 } 76 77 @Override 78 public String getRequestURI() { 79 return "/"; 80 } 81 82 @Override 83 public String getContextPath() { 84 return ""; 85 } 86 87 @Override 88 public HttpSession getSession() { 89 return session; 90 } 91 }; 92 } 93 94 /** 95 * Returns a fake, HttpServletResponse which throws an exception if any of its methods are called. 96 */ newFakeHttpServletResponse()97 public static HttpServletResponse newFakeHttpServletResponse() { 98 return (HttpServletResponse) 99 Proxy.newProxyInstance( 100 HttpServletResponse.class.getClassLoader(), 101 new Class[] {HttpServletResponse.class}, 102 new ThrowingInvocationHandler()); 103 } 104 105 private static class FakeHttpSessionHandler implements InvocationHandler, Serializable { 106 final Map<String, Object> attributes = Maps.newHashMap(); 107 108 @Override invoke(Object proxy, Method method, Object[] args)109 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 110 String name = method.getName(); 111 if ("setAttribute".equals(name)) { 112 attributes.put((String) args[0], args[1]); 113 return null; 114 } else if ("getAttribute".equals(name)) { 115 return attributes.get(args[0]); 116 } else { 117 throw new UnsupportedOperationException(); 118 } 119 } 120 } 121 122 /** Returns a fake, serializable HttpSession which stores attributes in a HashMap. */ newFakeHttpSession()123 public static HttpSession newFakeHttpSession() { 124 return (HttpSession) 125 Proxy.newProxyInstance( 126 HttpSession.class.getClassLoader(), 127 new Class[] {HttpSession.class}, 128 new FakeHttpSessionHandler()); 129 } 130 } 131