天天看點

基于http協定的接口測試(1)

接口的分類:

1、系統與系統之間的調用,比如銀行會提供接口供電子商務網站調用,或者說,支付寶會提供接口給淘寶調用 

2、上層服務對下層服務的調用,比如service層會調用DAO層的接口,而應用層又會調用服務層提供的接口,一般會通過 

3、服務之間的調用,比如注冊使用者時,會先調用使用者查詢的服務,檢視該使用者是否已經注冊。 

而我們所要做的接口測試,先要了解是基于哪一種類型的接口測試,不同類型的接口測試方法可能是不一緻的

http協定的接口測試,一般會用jmeter去測試,jmeter的好處是不用寫測試代碼,直接使用jmeter提供的http請求去測試,也可以使用HTTPClient去測試,好處是可以友善內建和自動化

以下代碼待分類進行改進,僅為參考

接口測試案例:

應用資料同步接口

A平台需要将遊戲的appid和appname同步給交易支撐B平台。同步接口需要進行簽名和驗簽。算法和秘鑰與支付接口保持一緻。

一).接口約定:

1、接口均采用http協定,POST方法。

2、應用資料同步請求參數為transdata、sign。transdata為具體業務參數,資料格式為json格式;sign為transdata的簽名資料。具體呈現方式為transdata=xxxx&sign=yyyy,其中yyyy就是對xxxx的簽名資料,yyyy=rsa(md5(xxxx))。收到資料後需對簽名進行驗證。

3、A平台需要以雙方約定的密鑰對請求資料進行簽名。

二).參數清單:

參數名稱 參數含義 資料類型 是否可選 參數說明
opertype 資料操作類型 integer 必填

資料操作類型:

0 – 新增

1 – 變更

2 – 删除

appid 應用編号 String 必填 A平台給遊戲配置設定的appid
appname 應用名稱 String 必填 遊戲名稱

三).樣例: 請求資料(http包體資料):

transdata={"opertype":0,"appid":"33213","appname":"測試遊戲"}&sign=d91cbc584316b9d99919921a9

成功應答資料(http包體資料):

{"code":"0000","errmsg":"資料同步成功"}

失敗應答 {"code":"9999","errmsg":"參數清單不符合規範"}  ,還有其它的code代碼:9994、9998等

接口請求位址:http://192.168.0.140:8101/pismire/sfyan/syncAppData.lzyhjfs 實作代碼如下:

/**
	 * 使用 HttpClient 需要以下 5 個步驟:
		1. 建立 HttpClient 的執行個體
		2. 建立某種連接配接方法的執行個體,在這裡是 httppost<span style="font-family:Arial,Helvetica,sans-serif">,在httppost 的構造函數中傳入待連接配接的位址</span>
		3. 調用第一步中建立好的執行個體的 execute 方法來執行第二步中建立好的 method 執行個體
		4. 讀 response
		5. 釋放連接配接。無論執行方法是否成功,都必須釋放連接配接
	  */

	public static String conResult, encrypt_app, encrypt_sign;
	public static String smsUrl = "http://192.168.0.140:8101/pismire/sfyan/syncAppData.lzyhjfs";
	
	@Test
	public static void sendSms() throws Exception {
		//初始化httpclient  
		HttpClient httpclient = new DefaultHttpClient();
		//擷取httppost 
		HttpPost httppost = new HttpPost(smsUrl);		
		try {
			//添加參數
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("transdata",encrypt_app()));
			nameValuePairs.add(new BasicNameValuePair("sign", encrypt_sign()));

			//設定封包頭以及參數的格式
			httppost.addHeader("Content-type","application/x-www-form-urlencoded");
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			//執行post請求  
			HttpResponse response = httpclient.execute(httppost);
			
			//列印伺服器傳回的狀态
			System.out.println("伺服器傳回的狀态:"+response.getStatusLine().getStatusCode());
			// if (response.getStatusLine().getStatusCode() == 200) 
			/**
			 * 讀傳回資料
			 * */
			conResult = EntityUtils.toString(response.getEntity());
			System.out.println(conResult);
			
			//斷開連接配接
			httpclient.getConnectionManager().shutdown(); 

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		// return conResult;
	}

	/**
	 * 拼接transdata資料
	 * */
	public static String encrypt_app() throws Exception {
		int opertype = 1;
		String appid = "2001850000000320311";
		String appname = "同步資料11";

		String transdata = "{\"appid\":\"" + appid + "\",\"appname\":\""+ appname + "\",\"opertype\":" + opertype + "}";
		encrypt_app = transdata;
		System.out.println("transdata:" + encrypt_app);

		return encrypt_app;
	}

	/**
	 * 用MD5算法對transdata進行加密
	 * */
	public static String encrypt_sign() throws Exception {
		// sign簽名秘鑰,key1 和mod為背景固定的秘鑰
		BigInteger key1 = new BigInteger(EncryptorUtils.getDecryptionString("2SWUfsOjTTBpLfyWSNnXvvA37WLqvpUuOnIN9sjW6U4="));
		BigInteger mod = new BigInteger(EncryptorUtils.getDecryptionString("+9mTlYq/DeqPDwNuvyuLI4eHw03rdP5w0O+bzb1uMcTASSC2AH381WeGO70wv9jm"));

		String desc = RSAUtil.encrypt(MD5.md5Digest(encrypt_app), key1, mod);
		encrypt_sign = desc;
		System.out.println("sign:" + encrypt_sign);

		return encrypt_sign;
	}
           

-----------------------TestNg執行完成輸出的日志

transdata:{"appid":"2001850000000320311","appname":" 同步資料11 ","opertype":0}

sign:3f58b89e2e1efd0f104cf0cc891bf6c 4d07cd4c898c0df22af83776e1d04bbc 36339c09d26c267d381ed4682fff9392 

伺服器傳回的狀态:200

{"code":"0000","errmsg":"資料同步成功"}

PASSED: sendSms

輸出請求位址: System.out.println("請求位址:"+smsUrl+EntityUtils.toString(httppost.getEntity()));

基于http協定的接口測試(1)

可以使用Firefox的一個插件:HttpRequester 模拟發送請求(請求URL、傳輸方法、參數)

備注:

比較蛋疼的問題就是httpclient4.3版本,會自動的儲存cookies,其它接口直接引用登陸的cookies,偶爾會出現失敗,如果輸出該cookies資訊,其它接口可以正常調用成功

參考文檔:

1.http://blog.csdn.net/zhangchaoy/article/details/10112225

2.http://blog.csdn.net/zhangchaoy/article/details/10063099

繼續閱讀