org.webframe.test
Class BaseHttpClientTests

java.lang.Object
  extended by org.webframe.test.web.BaseWebServerTests
      extended by org.webframe.test.BaseHttpClientTests

public class BaseHttpClientTests
extends BaseWebServerTests

Version:
$Id: codetemplates.xml,v 1.1 2009/09/07 08:48:12 Exp $ Create: 2011-3-28 下午08:51:08
Author:
黄国庆

Field Summary
protected static org.apache.http.impl.client.DefaultHttpClient client
           
private  org.apache.http.protocol.HttpContext context
           
protected static String defaultEncode
           
private static Map<String,Boolean> userOnceMap
           
 
Fields inherited from class org.webframe.test.web.BaseWebServerTests
HTTP, HTTPS
 
Constructor Summary
BaseHttpClientTests()
           
 
Method Summary
protected  void enableAuth(String usernamePassword)
          开启安全认证后,可以访问受限制资源
protected  org.apache.http.HttpResponse executeGet(String url)
          发送Get请求对象到服务器,返回HttpResponse
protected  org.apache.http.HttpResponse executePost(String url, List<org.apache.http.NameValuePair> params)
          发送Post请求对象到服务器,返回HttpResponse
protected  org.apache.http.HttpResponse executePost(String url, Map<String,Object> params)
          发送Post请求对象到服务器,返回HttpResponse
protected  String getBaseUrl()
           
protected  org.apache.http.protocol.HttpContext getContext()
           
protected  org.apache.http.client.methods.HttpGet getHttpGet(String url)
          通过url获取Get请求对象
protected  org.apache.http.client.methods.HttpPost getHttpPost(String url)
          通过url获取Post请求对象
protected  List<org.apache.http.NameValuePair> getPairs(Map<String,Object> params)
          Map参数集合转换为NameValuePair List参数集合
protected  String getResponseContent(org.apache.http.HttpResponse res)
           
protected  String getUrl(String url)
          获取url全路径包括主机和端口,默认使用http;
protected  String getUrlParamsString(List<org.apache.http.NameValuePair> params)
          获取参数的字符串格式
protected  String getUrlParamsString(Map<String,Object> params)
          获取参数的字符串格式
protected  void login()
          使用默认的登陆链接进行登陆:url("/j_spring_security_check"), user("admin:1")
protected  void login(String loginUrl, Map<String,Object> user)
          执行登陆方法后,就有权限方法其他资源
protected  void login(String loginUrl, String usernamePassword)
          执行登陆方法后,就有权限方法其他资源
protected  String sendGet(String url)
          发送Get请求对象到服务器,返回字符串
protected  String sendPost(String url, List<org.apache.http.NameValuePair> params)
          发送Post请求对象到服务器,返回字符串
protected  String sendPost(String url, Map<String,Object> params)
          发送Post请求对象到服务器,返回字符串
protected  void setContext(org.apache.http.protocol.HttpContext context)
           
 
Methods inherited from class org.webframe.test.web.BaseWebServerTests
disposeConnector, getServerHost, getServerPort, getServerURL, getServerURL
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultEncode

protected static final String defaultEncode
See Also:
Constant Field Values

client

protected static final org.apache.http.impl.client.DefaultHttpClient client

context

private org.apache.http.protocol.HttpContext context

userOnceMap

private static Map<String,Boolean> userOnceMap
Constructor Detail

BaseHttpClientTests

public BaseHttpClientTests()
Method Detail

getBaseUrl

protected String getBaseUrl()

getUrl

protected String getUrl(String url)
获取url全路径包括主机和端口,默认使用http;

Parameters:
url -
Returns:

getHttpGet

protected final org.apache.http.client.methods.HttpGet getHttpGet(String url)
通过url获取Get请求对象

Parameters:
url -
Returns:

getHttpPost

protected final org.apache.http.client.methods.HttpPost getHttpPost(String url)
通过url获取Post请求对象

Parameters:
url -
Returns:

sendGet

protected String sendGet(String url)
                  throws Exception
发送Get请求对象到服务器,返回字符串

Parameters:
url - 字符串上可以包含参数,例如:/login?username=admin&password=1
Returns:
字符串内容结尾包含'\r\n'
Throws:
Exception

sendPost

protected String sendPost(String url,
                          Map<String,Object> params)
                   throws Exception
发送Post请求对象到服务器,返回字符串

Parameters:
url - 不包含参数的url路径
params - Map参数集合
Returns:
字符串内容结尾包含'\r\n'
Throws:
Exception

sendPost

protected String sendPost(String url,
                          List<org.apache.http.NameValuePair> params)
                   throws Exception
发送Post请求对象到服务器,返回字符串

Parameters:
url - 不包含参数的url路径
params - NameValuePair List参数集合
Returns:
字符串内容结尾包含'\r\n'
Throws:
Exception

executeGet

protected org.apache.http.HttpResponse executeGet(String url)
                                           throws Exception
发送Get请求对象到服务器,返回HttpResponse

Parameters:
url - 字符串上可以包含参数,例如:/login?username=admin&password=1
Returns:
HttpResponse对象
Throws:
Exception

executePost

protected org.apache.http.HttpResponse executePost(String url,
                                                   Map<String,Object> params)
                                            throws Exception
发送Post请求对象到服务器,返回HttpResponse

Parameters:
url - 不包含参数的url路径
params - Map参数集合
Returns:
HttpResponse对象
Throws:
Exception

executePost

protected org.apache.http.HttpResponse executePost(String url,
                                                   List<org.apache.http.NameValuePair> params)
                                            throws Exception
发送Post请求对象到服务器,返回HttpResponse

Parameters:
url - 不包含参数的url路径
params - NameValuePair List参数集合
Returns:
HttpResponse对象
Throws:
Exception

getResponseContent

protected String getResponseContent(org.apache.http.HttpResponse res)
                             throws Exception
Throws:
Exception

getUrlParamsString

protected String getUrlParamsString(Map<String,Object> params)
获取参数的字符串格式

Parameters:
params - Map参数集合
Returns:

getUrlParamsString

protected String getUrlParamsString(List<org.apache.http.NameValuePair> params)
获取参数的字符串格式

Parameters:
params - NameValuePair List参数集合
Returns:

getPairs

protected final List<org.apache.http.NameValuePair> getPairs(Map<String,Object> params)
Map参数集合转换为NameValuePair List参数集合

Parameters:
params - Map参数集合
Returns:

setContext

protected void setContext(org.apache.http.protocol.HttpContext context)

getContext

protected final org.apache.http.protocol.HttpContext getContext()

enableAuth

protected final void enableAuth(String usernamePassword)
开启安全认证后,可以访问受限制资源

Parameters:
usernamePassword - 例如:'admin:123456'

login

protected void login()
              throws Exception
使用默认的登陆链接进行登陆:url("/j_spring_security_check"), user("admin:1")

Throws:
Exception

login

protected void login(String loginUrl,
                     String usernamePassword)
              throws Exception
执行登陆方法后,就有权限方法其他资源

Parameters:
loginUrl - 登陆url,例如:'/j_spring_security_check'
usernamePassword - 用户信息,例如:"admin:1"
Throws:
Exception

login

protected void login(String loginUrl,
                     Map<String,Object> user)
              throws Exception
执行登陆方法后,就有权限方法其他资源

Parameters:
loginUrl - 登陆url,例如:'/j_spring_security_check'
user - 包含用户信息的Map,例如:user.put("j_username", "admin");user.put("j_password", "1");
Throws:
Exception


Copyright © 2012. All Rights Reserved.