TA的每日心情 | 无聊 2024-11-5 10:03 |
---|
签到天数: 77 天 连续签到: 1 天 [LV.6]测试旅长
|
我们自己用java写接口自动化测试框架或者做个接口自动化测试平台的话,是需要自己进行相关的请
求的,因此我们需要简单的封装下httpclient,我新建了一个http工具类,将get方法和post方法进行了一个
简单的封装。
如果是开发的话,比如APP开发,无论是iOS还是Android,是需要将数据请求进行十分严密的封装的,因
为需要对各种网络状态,请求状态做相应的判断处理,因为需要通过这些判断来做出相应的UI交互界面来
给用户提示,那么我们做接口测试的话是不需要这么严密的,因为测试的前提就是要保证测试环境的稳
定,我们只考虑接口在正常的访问下通过对传参的各种控制来判断接口是否做出了正确的数据传输。
前几篇已经记录下了简单的HTTPClient的get和post请求,下面是对它们进行了个简单的封装,使之更
容易被我们使用
- <p>package com.httpclientget;</p><p> </p><p>import java.io.IOException;</p><p>import java.io.InputStream;</p><p>import java.io.OutputStream;</p><p>import java.util.ArrayList;</p><p>import java.util.Map;</p><p> </p><p>import org.apache.http.Header;</p><p>import org.apache.http.HttpEntity;</p><p>import org.apache.http.HttpResponse;</p><p>import org.apache.http.NameValuePair;</p><p>import org.apache.http.client.HttpClient;</p><p>import org.apache.http.client.config.RequestConfig;</p><p>import org.apache.http.client.config.RequestConfig.Builder;</p><p>import org.apache.http.client.entity.UrlEncodedFormEntity;</p><p>import org.apache.http.client.methods.HttpGet;</p><p>import org.apache.http.client.methods.HttpPost;</p><p>import org.apache.http.impl.client.HttpClients;</p><p>import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;</p><p>import org.apache.http.message.BasicNameValuePair;</p><p>import org.apache.http.util.EntityUtils;</p><p> </p><p>public class HttpUtil {</p><p> </p><p> //连接池</p><p> private static PoolingHttpClientConnectionManager connectionMgr;</p><p> //超时时间</p><p> private static final int MAX_TIMEOUT = 7000;</p><p> </p><p> private static RequestConfig requestConfig;</p><p> static{</p><p> //设置连接池</p><p> connectionMgr = new PoolingHttpClientConnectionManager();</p><p> //设置连接池大小</p><p> connectionMgr.setMaxTotal(100);</p><p> connectionMgr.setDefaultMaxPerRoute(connectionMgr.getMaxTotal());</p><p> </p><p> RequestConfig.Builder configBuilder = RequestConfig.custom();</p><p> //设置连接超时</p><p> configBuilder.setConnectTimeout(MAX_TIMEOUT);</p><p> //设置读取超时</p><p> configBuilder.setSocketTimeout(MAX_TIMEOUT);</p><p> //设置从连接池获取连接实例的超时</p><p> configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);</p><p> </p><p> requestConfig = configBuilder.build();</p><p> </p><p> }</p><p> /**</p><p> * GET 请求 带输入参数</p><p> * @param Url 请求host地址</p><p> * @param params 参数</p><p> * @return</p><p> */</p><p> public static String httpGet(String Url,Map<String, Object>params)</p><p> {</p><p> //返回结果</p><p> String result = null;</p><p> //拼接url</p><p> StringBuilder builder = new StringBuilder(Url);</p><p> if (Url.contains("?")) {</p><p> builder.append("&");</p><p> }else{</p><p> builder.append("?");</p><p> }</p><p> int i=0;</p><p> for (String key : params.keySet()) {</p><p> if (i != 0 ) {</p><p> builder.append("&");</p><p> }</p><p> builder.append(key);</p><p> builder.append("=");</p><p> builder.append(params.get(key));</p><p> </p><p> i++;</p><p> }</p><p> String apiUrl = builder.toString();</p><p> //创建client</p><p> HttpClient client = HttpClients.createDefault();</p><p> </p><p> try {</p><p> HttpGet get = new HttpGet(apiUrl);</p><p> HttpResponse response = client.execute(get);</p><p> //获取请求状态码</p><p> int statusCode = response.getStatusLine().getStatusCode();</p><p> System.out.println(statusCode);</p><p> </p><p> HttpEntity entity = response.getEntity();</p><p> if (entity != null) {</p><p> result = EntityUtils.toString(entity,"UTF-8");</p><p> }</p><p> } catch (Exception e) {</p><p> e.printStackTrace();</p><p> }</p><p> return result;</p><p> }</p><p> /**</p><p> * POST 请求</p><p> * @param url 请求url</p><p> * @param params post提交参数</p><p> * @return </p><p> */</p><p> public static String httpPost(String url,Map<String, Object>params)</p><p> {</p><p> HttpClient client = HttpClients.createDefault();</p><p> String result = null;</p><p> try {</p><p> HttpPost post = new HttpPost(url);</p><p> //添加post提交参数</p><p> ArrayList<NameValuePair> pairList = new ArrayList<NameValuePair>();</p><p> for(Map.Entry<String, Object> entry:params.entrySet())</p><p> {</p><p> NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());</p><p> pairList.add(pair);</p><p> }</p><p> </p><p> post.setEntity(new UrlEncodedFormEntity(pairList,"UTF-8"));</p><p> </p><p> HttpResponse response = client.execute(post);</p><p> //获取状态码</p><p> int statueCode = response.getStatusLine().getStatusCode();</p><p> System.out.println(statueCode);</p><p> </p><p> HttpEntity entity = response.getEntity();</p><p> if (entity != null) {</p><p> result = EntityUtils.toString(entity);</p><p> }</p><p> </p><p> } catch (Exception e) {</p><p> e.printStackTrace();</p><p> }</p><p> return result;</p><p> }</p><p>}</p><p> 做个简单的测试:</p><p>
- </p><p> HttpUtil util = new HttpUtil();</p><p>Map<String, Object> params = new HashMap<String, Object>();</p><p>params.put("q", "good");</p><p>params.put("salt", "1496238482428");</p><p>params.put("sign", "02E15CDAF871B698FE04EE32FD2CF155");</p><p>params.put("from", "en");</p><p>params.put("appKey", "7743eee7f7e11d75");</p><p>params.put("to", "zh-CHS");</p><p> </p><p>String result = util.httpPost(urlStr, params);</p><p>System.out.println(result);</p><p> 输出结果:</p><p>
- </p><p>
- </p><p>{</p><p> "tSpeakUrl": "https://dict.youdao.com/dictvoice?audio=%E5%A5%BD&le=auto&channel=7743eee7f7e11d75&rate=4",</p><p> "web": [</p><p> {</p><p> "value": [</p><p> "好",</p><p> "善",</p><p> "商品"</p><p> ],</p><p> "key": "Good"</p><p> },</p><p> {</p><p> "value": [</p><p> "公共物品",</p><p> "公益事业",</p><p> "公共财"</p><p> ],</p><p> "key": "public good"</p><p> },</p><p> {</p><p> "value": [</p><p> "干的出色",</p><p> "干得好",</p><p> "好工作"</p><p> ],</p><p> "key": "Good Job"</p><p> }</p><p> ],</p><p> "query": "good",</p><p> "translation": [</p><p> "好"</p><p> ],</p><p> "errorCode": "0",</p><p> "basic": {</p><p> "us-phonetic": "ɡʊd",</p><p> "phonetic": "gʊd",</p><p> "uk-phonetic": "gʊd",</p><p> "explains": [</p><p> "n. 好处;善行;慷慨的行为",</p><p> "adj. 好的;优良的;愉快的;虔诚的",</p><p> "adv. 好",</p><p> "n. (Good)人名;(英)古德;(瑞典)戈德"</p><p> ]</p><p> },</p><p> "speakUrl": "https://dict.youdao.com/dictvoice?audio=good&le=auto&channel=7743eee7f7e11d75&rate=4"</p><p>}</p>
复制代码
如果不需要传入参数,把第二个传入的参数置为空即可,这只是简单地封装了下,在后续要写的自动化测试
框架我们可能还是要加入各种各样的判断以及日志输出,毕竟测试最重要的还是断言等这样的测试结果。
|
|