2011-08-28 70 views
3

我想創建一個servlet類,它接收來自jsp的兩個輸入參數let say login.jsp和thae servlet「CommandQueueTestServlet」將那些包含參數的參數設置爲頭參數,然後發送請求和響應參數到另一個servlet「CheckForCommandServlet」。手動設置測試頭

我需要這樣做只是爲了測試我的功能,因爲我的「CheckForCommandServlet」實際上會被其他具有頭部參數的應用程序調用。

但是對於我自己的測試,我想創建一個servlet「CommandQueueTestServlet」來設置標題。

請檢查下面的代碼,我試圖解釋

javax.servlet.*; 
import javax.servlet.http.*; 
import java.util.*; 

public class CommandQueueTestServlet extends HttpServlet{ 

public void doGet(HttpServletRequest request, 
     HttpServletResponse response) 
throws ServletException, IOException { 


String hwId=request.getParameter("hardware_id"); 
String panelistId=request.getParameter("panelist_id")); 

// Setting input parameter as header parameter.Since request object dont have setHeader so setting in response 
//object 

response.setHeader("x-HwId",hwid); 
response.setHeader("x-panelistId,panelistId); 

// creating instance of CheckForCommandServlet and passing in doGet() method: 

CheckForCommandServlet headerParam= new CheckForCommandServlet(); 

headerParam.doGet(request,response); 


} 
} 

// Code for CheckForCommandServlet 

public class CheckForCommandServlet extends HttpServlet { 

@Override 
public void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 



    Enumeration enumeration = httpServletRequest.getHeaderNames(); 
    String headerName; 
    String headerValue; 
    while (enumeration.hasMoreElements()) 
    { 
     headerName = (String)enumeration.nextElement(); 
     if (headerName == null) 
     { 
      headerName = ""; 
     } 

     headerName = headerName.toLowerCase(); 
     headerValue = httpServletRequest.getHeader(headerName); 
     logger.log(Level.INFO, "Header headerName " + headerName); 
     logger.log(Level.INFO, "Header ParamaterValue " + headerValue); 

    } 

    } 

我CheckForCommandServlet是怎麼得到的headerParemeter在CommandQueueTestServlet設置,因爲它是在頭參數設置。

回答

2

下列行:

// Setting input parameter as header parameter.Since request object dont have setHeader so setting in response 
//object 

response.setHeader("x-HwId",hwid); 
response.setHeader("x-panelistId,panelistId); 

增加了兩個標頭將由servlet容器中生成HTTP響應,並且不被轉發到CheckForCommandServlet servlet的HTTP請求。

因爲你的意圖是一個HTTP標頭添加到原始請求以便後續HttpServletRequest.getHeader()調用將讀取相應的值,你可以採取使用HttpServletRequestWrapper,它覆蓋getHeader方法,返回被髮送的值的方法客戶端。更多細節可在this related StackOverflow answer中找到。


用於驗證CheckForCommandServlet的行爲的目的,更好的方法,是使用一個HTTP調試代理像Fiddler。 Fiddler允許您將request headers automatically添加到客戶端發出的請求。您只需確保將客戶端配置爲使用Fiddler作爲HTTP代理。

即使你的意圖是編寫一個輕量級的單元測試,最好在客戶端使用一個像Apache HttpComponents這樣的HTTP庫,而不是使用額外開銷寫入請求包裝器和servlet的方法需要構建測試特定的WAR。

1

要在維尼特雷諾的出色答卷擴大,這是一個使用java.net.HttpURLConnection一個方式,你可以設置HTTP頭:

URL url = new URL("http://localhost:8080/CheckForCommandServlet"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestProperty("X-HwId", "Foo"); 
connection.setRequestProperty("X-panelistId", "Bar"); 
connection.setRequestMethod("GET"); 
connection.connect(); 
connection.getInputStream().close(); //Must open stream to make request. 
connection.disconnect(); 

可以包裹起來像這樣在你的功能測試斷言上除其他事項外響應。


編輯

使用HttpServletRequestWrapper

  1. 設置標頭,請求屬性。
  2. 在包裝中覆蓋getHeader(String name)getHeaderNames()
  3. 使用包裝就地轉發到目標servlet。

代碼(注意,我已經刪除了簡潔進口):

public class CommandQueueTestServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
     MyWrapper wrapper = new MyWrapper(request); 
     request.setAttribute("X-HwId", "HardWare ID"); 
     request.setAttribute("X-PanelListId", "Panel List ID"); 
     request.getRequestDispatcher("/CheckForCommandServlet").forward(
       wrapper, response); 
    } 

    private static class MyWrapper extends HttpServletRequestWrapper { 

     public MyWrapper(HttpServletRequest request) { 
      super(request); 
     } 

     @Override 
     public String getHeader(String name) { 
      String header = super.getHeader(name); 
      return header == null ? (String) super.getAttribute(name) : header; 
     } 

     @Override 
     @SuppressWarnings("unchecked") 
     public Enumeration<String> getHeaderNames() { 
      List<String> headerNames = Collections.list(super.getHeaderNames()); 
      headerNames.addAll(Collections.list(super.getAttributeNames())); 
      return Collections.enumeration(headerNames); 
     } 
    } 
} 

您現在應該能夠使用request.getHeader(String name)像往常一樣:

public class CheckForCommandServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
     response.getWriter() 
       .format("Hardware ID = %s\n Panel List ID = %s", 
         request.getHeader("X-HwId"), 
         request.getHeader("X-PanelListId")); 
    } 
} 

即使此作品,這是一個相當脆弱的測試,正如Vineet已經提到的那樣,需要你部署一個單獨的WAR。恕我直言,在某種測試中使用servlet代碼之外的HTTP庫仍然是最好的選擇。

+0

謝謝大家的優秀回覆! – vibhas