An interface test post four kinds of analog data request

https://www.jianshu.com/p/3b6d7aa2043a

 

I. Background

In routine testing of the interface, an analog interface requests are generally two methods, simulation and Fiddler HttpClient simulation.

Fiddler is a simple proxy http protocol debugging tool, it user-friendly, easy to operate, is one of analog http request weapon.
And we often say HttpClient kits, traced an integral part of the Apache Foundation HttpComponent projects. HttpComponent There are three main components, namely HttpCore, HttpClient and AsynchHttpClient. As described on the official website HttpComponent, HttpCore HTTP is the underlying transport components, realized with minimal memory to analog Http request the client and server. HttpClient is based HttpCore achieve Http request model agent, it can be used to provide efficient, new, feature-rich client support HTTP protocol programming toolkit. Asynch HttpClient also based HttpCore as the name suggests is a http proxy when processing a large number of concurrent requests. Data in different formats by different treatment of these two methods of simulation.

In the tests the interface, the interface is usually post request or a get request. get the requested test is generally relatively simple, just set up the relevant request header, url to write correctly. However, when testing post request, the request format setting data often somewhat complicated. Especially in the case of a developer interface documentation describing unclear, it will affect the efficiency of the test.

This paper therefore summarizes the four common post requests and their corresponding data format fiddler, HttpClient simulation configuration request method.


Two, post request body Explanation

 一个正常的post请求主要包括请求行,请求头,请求主体,也就是
 <method><url><version> <headers> <entity-body> 

Get request is not a request for the body of entity-body. For the post request, the data format for sending the request will not be restricted, in theory, you can send arbitrary data, but the server can not handle is another matter. After the server receives the data, how to interpret the data it? It will be requested content Content-Type header provided to perform data analysis. After determining good Content-Type format, the data format of request body also determined. Content-Type four formats: namely, application / x-www-form- urlencoded ( which is the default format), application / json, text / xml and multipart / form-data format.
These different post request to the data format is configured by HttpEntity, it is necessary to subject HttpClient HttpEntity simple haircut because all require post requests data transmission disposed HttpEntity entity. HttpEntity is an interface, achieve this specific type of interface There are many more commonly used is StringEntity, UrlEncodedFormEntity (inherited from StringEntity), MultipartEntity. They will be used when sending a different format post request. Next, details on the data format corresponding to each request fiddler analog and analog httpClient request (java implementation) achievement.


Three or four Post request data format and configuration requests fiddler and analog HttpClient

(A) application / x-www-form-urlencoded data format

W3C官网上明确对这种数据格式进行了定义:
This is the default content type. Forms submitted with this content type must be encoded as follows:
Control names and values are escaped. Space characters are replaced by '+', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by '%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., '%0D%0A').The control names/values are listed in the order they appear in the document. The name is separated from the value by '=' and name/value pairs are separated from each other by '&'.

This post is the most common request is the default format of the data submitted. It requires between data name (name) and data values (value) with an equal sign is connected between the other set of name / value with the value of & coupled. For example: parameter1 = 12345 & parameter2 = 23456 . The format of the requested content, in fact, a simplified method for simultaneously transmitting a client, the server acquires simplified, the information transmitted by the server getParameters (String name) can be obtained.
We look at how to simulate post request with fiddler and HttpClient words.
(1) If the analog fiddler requests, request headers and the requested content may be configured such body:

 

 

After the simulation request, you can view the results from the return to our request data:

 

 

(2) If the analog HttpClient post request, then the request may be so constructed:

DefaultHttpClient client = new DefaultHttpClient(); List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); //定义键值对列表,用于存放向url发送post请求的数据。 params.add(new BasicNameValuePair("parameter1", "12345")); params.add(new BasicNameValuePair("parameter2", "23456")); //向params设置数据 HttpPost post = new HttpPost("http://example.com"); //定义HttpPost对象并初始化它 HttpEntity reqEntity = new UrlEncodedFormEntity(params); //用UrlEncodedFormEntity对象包装请求体数据 post.setEntity(reqEntity); //设置post请求实体 HttpResponse response = client.execute(post); //发送http请求 System.out.println("the request body is:"+EntityUtils.toString(reqEntity)); //打印出请求实体 System.out.println(response.getStatusLine().getStatusCode()); //打印http请求返回码 

** (b) application / json data format **
file application / json request header format is used to tell the server refers to post the message body past JSON string is serialized.
(1) If the analog fiddler requests, request headers and the requested content may be configured such body:

 

 

After the simulation request, we can see the return of the requested data from the results:

 

 

 

(2) If the analog HttpClient post request, then the request may be so constructed:

 HttpClient client = new DefaultHttpClient(); JSONObject js = new JSONObject(); //定义一个JSON数据格式对象,用其保存请求主体数据。 js.element("parameter1", "12345"); //为JSON对象的各个key值赋值 js.element("parameter2","23456"); String postRequest = js.toString(); HttpPost post = new HttpPost("http://example.com"); //定义HttpPost对象并初始化它 StringEntity reqEntity = new StringEntity(js.toString()); //用StringEntity对象包装请求体数据 reqEntity.setContentType("application/json"); //设置请求头数据传输格式 post.setEntity(reqEntity); //设置post请求实体 HttpResponse response = client.execute(post); //发送http请求 System.out.println("the request body is:"+EntityUtils.toString(reqEntity)); //打印出请求实体 System.out.println(response.getStatusLine().getStatusCode()); //打印http请求返回码 

Here we found HttpClient analog post request, a request header format is application / x-www-form- urlencoded and application / json main difference is the construction of the format of the request body (the former is a key-value pair, which is a string JSON) and the method of packaging different request body data (the former is UrlEncodedFormEntity, which is StringEntity).
** (c) text / xml ** Data Format
(1) If the analog fiddler requests, request headers and the request body may be configured such content:

 

 

After the simulation request, we can see the return of the requested data from the results:

 

 

(2) If the analog HttpClient post request, then the request may be so constructed:

Document doc = DocumentHelper.createDocument(); //创建document对象 Element book = doc.addElement("book"); //构建document对象各个节点 book.addElement("title").addText("芈月传"); book.addElement("author").addText("蒋胜男"); String body = book.asXML(); //Document对象转成string类型 StringEntity reqEntity = new StringEntity(body); //用StringEntity对象包装请求体数据 reqEntity.setContentType("text/xml"); //设置请求头数据传输格式 reqEntity.setContentEncoding("utf-8"); //设置请求头数据编码格式 HttpPost post = new HttpPost("http://example.com"); //定义HttpPost对象并初始化它 post.setEntity(reqEntity); //设置post请求实体 HttpResponse response = client.execute(post); //发送http请求 System.out.println("the request body is:"+EntityUtils.toString(reqEntity)); //打印出请求实体 System.out.println(response.getStatusLine().getStatusCode()); //打印http请求返回码 

(四)multipart/form-data数据格式
除了传统的application/x-www-form-urlencoded表单,我们另一个经常用到的是上传文件用的表单,这种表单的类型为multipart/form-data。在HttpClient程序扩展包(HttpMime)中专门有一个类与之对应,那就是MultipartEntity类。此类同样实现了HttpEntity接口。
(1)如果用fiddler模拟请求的话,请求头和请求主体的内容可以这样构造:
第一步,先设置好请求头格式,然后点击upload file...

 

 

第二步,上传你的文件,这里我上传一个png的图片

 

 

这是fiddler根据我们上传的文件自动调整生成的请求,在请求头中看到,我们需要选择一段数据作为“分割边界”(boundary属性),这个“边界数据”不能在内容其他地方出现,一般来说使用一段从概率上说“几乎不可能”的数据即可。每次post浏览器都会生成一个随机的30-40位长度的随机字符串,浏览器一般不会遍历这次post的所有数据找到一个不可能出现在数据中的字符串,一般都是随机生成。选择了这个边界之后,浏览器便把它放在Content-Type 里面传递给服务器,服务器根据此边界解析数据。下面的数据便根据boundary划分段,每一段便是一项数据。(每个field被分成小部分,而且包含一个value是"form-data"的"Content-Disposition"的头部;一个"name"属性对应field的ID等等,文件的话包括一个filename)
模拟请求之后,从返回结果可以看到我们的请求数据:

 

(2)如果用HttpClient模拟post请求的话,请求可以这样构造:

HttpPost post = new HttpPost("http://example.com");           //定义HttpPost对象并初始化它
MultipartEntity mutiEntity = new MultipartEntity();           //定义MultipartEntity对象
File file = new File("C:\Users\hzsuixiang\Desktop\image_20151117151539.png");
mutiEntity.addPart("desc",new StringBody("网易云阅读", Charset.forName("utf-8")));     //设置multiEntity对象的主体数据
mutiEntity.addPart("pic", newFileBody(file));post.setEntity(mutiEntity);             //设置post请求主体
HttpResponse  httpResponse = client.execute(post);                                   //执行post请求
HttpEntity httpEntity =  httpResponse.getEntity();                                   //获得响应返回实体

综上,就是接口测试中fiddler与HttpClient模拟post接口四种请求数据的构造方法,总结起来有利于在以后的接口测试工组过程中可以及时查阅。另外,为了尽可能简化核心代码,列出的这些代码中并没有写出需要的jar包,大家使用的时候需要自行添加。

Guess you like

Origin www.cnblogs.com/wangcp-2014/p/11572428.html