Introduction to Interface Test Learning (1)--Pre-knowledge reserve

Source: Internet
Author: User

Required knowledge before interface testing:
1. The principle of interface testing:

Regardless of the test method, the principle of the interface test is through the test program to simulate the client sends the request message to the server, the server receives the request message after the corresponding message processing and then sends the reply message to the client, the client receives the reply message this process.

2. Prior Knowledge Reserve
2.1 http knowledge (see http://www.blogjava.net/zjusuyong/articles/304788.html writing is great!) )

HTTP port number 80, do not enter default is, HTTPS port number 443, based on SSL

HTTPS encryption principle:

Basic Understanding:
(1) Request header
required to be set in programming:
post.setheader ("User-agent", "Chrome");
Post.setheader ("Referer", "passport.36kr.com");
String Xsrftoken = responsecookie.getxsrftoken (response);
Related:
Referer: The address of the visitor, that is, the address of the previous page, which is generally set to host in the program, to prevent cross-domain
User-agent: The type of browser you are using to access the website, the operating system and version, the CPU type, Identification of information such as browser rendering engine, browser language, browser plugin, and so on.
Xsrftoken: In order to prevent CSRF, it is necessary to do not put the HTTP (s) request parameters in get, but should be placed in Post/put/delete. Then add a change of token on the page (each time the page will change, different from the previous token), the request server to verify the token, the normal client side, when sending the request will contain this item, and server-side to the number, it played a role in validation. Validation is required here for a specific page.
(2) response header
response.getfirstheader ("Set-cookie"));//Output Cookie header
Response.getlastheader ("Set-cookie"));//output cookie trailing
header[] head= Response.getallheaders ("Set-cookie"); Get all the Cookies

2.2 Post and get
Using version 4.3, set the request method with:
Httpget/httppost , the old version is used Methodget/methodpost
Packages imported with HttpClient:

Invoking the HTTP interface via get and post is summarized as follows:

* The steps to invoke the HTTP interface in the Get mode:
* 1. Constructing httpclient Instances
* 2. Constructing GetMethod Instances
* 3. Execute GetMethod, invoke HTTP interface
* 4. Read content
* 5. Processing the returned content
* 6. Release the connection
*

Post: Submit the form, collect or cancel the collection, look at the header of the interface request display, but note: May be divided into parameters, and no parameters (the specific settings of the parameters are as follows)
* Post method to invoke the HTTP interface:
* 1. Construct httpclient Instance
* 2. Construct Postmethod Instance
* 3. Put the parameter values into the Postmethod object
* Mode 1: Take advantage of the Namevaluepair class
list<namevaluepair> data = new arraylist<namevaluepair> ();
Data.add (new Basicnamevaluepair ("username", "1771019*****") );
Data.add (new Basicnamevaluepair ("Password", "1111111");
* Mode 2: Addparameter method for Postmethod instance object directly
* 4. Execute Postmethod, invoke HTTP interface
* 5. Read content
* 6. Handling returned content
* 7. Release Connection

2.3 Interface-related knowledge (how to get an interface)
For mobile, you can refer to my blog post description to get HTTPS requests, using fiddler crawl, phone binding local IP, port 8888, can crawl the general request, during the crawl can not be disconnected fiddler

On the PC side can be obtained directly from the browser, as follows:

Based on the returned response, you can determine which interface is in the end, or ask the developer


2.4 JSON parsing
The returned results are in JSON format, generally need to be converted to string type, and then based on the JSON parsing of Ali get the value we want, the following section to explain.


Small use case for first practiced hand: (Use the most primitive method Java.net.URL to practice, pay attention to the processing of the output stream)
Example 1: (GET request)

 PackageCom.wyy.demo;ImportJava.io.BufferedReader;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importjava.net.URLConnection;/*** Created by Yikai on 2016/3/30.*/ Public classGet { Public StaticString doget ()throwsexception{URL url=NewURL ("Http://blog.csdn.net/geekstart/svc/GetCategoryArticleList?id=1715873&type=foot");//Obtaining a connector (Java.net.URLConnection) from the Unified Resource Locator (Java.net.URL)URLConnection con=url.openconnection ();//Open ConnectionHttpURLConnection urlconnection= (httpurlconnection) con;//HttpURLConnection inherited from URLConnectionUrlconnection.setrequestproperty ("Accept-charset", "Utf-8"); Urlconnection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");//set the content type of the transfer//gets the returned content as an input streamInputStream InputStream =NULL; InputStreamReader InputStreamReader=NULL; BufferedReader Reader=NULL; StringBuffer Resultbuffer=NewStringBuffer (); String Templine=NULL; if(Urlconnection.getresponsecode () >= 300) {            Throw NewException ("The request is unsuccessful, the response code is" +Urlconnection.getresponsecode ()); }        Try{InputStream=Urlconnection.getinputstream (); InputStreamReader=NewInputStreamReader (InputStream); Reader=NewBufferedReader (InputStreamReader);  while((Templine = Reader.readline ())! =NULL) {resultbuffer.append (templine); }        } finally {            if(Reader! =NULL) {reader.close (); }            if(InputStreamReader! =NULL) {inputstreamreader.close (); }            if(InputStream! =NULL) {inputstream.close (); }        }        returnresultbuffer.tostring (); }    }


Example 2: (POST request)

 PackageCom.wyy.demo;ImportJava.io.*;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importjava.net.URLConnection;/*** Created by Yikai on 2016/3/30.*/ Public classPost { Public StaticString DoPost ()throwsException {String parameterdata= "Readauth?url=/alwayswyy/p/5235280.html"; URL URL=NewURL ("http://***); URLConnection Con=url.openconnection (); HttpURLConnection HttpURLConnection=(httpurlconnection) con; Httpurlconnection.setdooutput (true); Httpurlconnection.setrequestmethod ("POST"); Httpurlconnection.setrequestproperty ("Accept-charset", "Utf-8"); Httpurlconnection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); Httpurlconnection.setrequestproperty ("Content-length", String.valueof (Parameterdata.length ())); OutputStream OutputStream=NULL; OutputStreamWriter OutputStreamWriter=NULL; InputStream InputStream=NULL; InputStreamReader InputStreamReader=NULL; BufferedReader Reader=NULL; StringBuffer Resultbuffer=NewStringBuffer (); String Templine=NULL; Try{OutputStream=Httpurlconnection.getoutputstream (); OutputStreamWriter=NewOutputStreamWriter (OutputStream);            Outputstreamwriter.write (Parameterdata.tostring ());            Outputstreamwriter.flush (); if(Httpurlconnection.getresponsecode () >= 300) {                Throw NewException ("The request is unsuccessful, the response code is" +Httpurlconnection.getresponsecode ()); } InputStream=Httpurlconnection.getinputstream (); InputStreamReader=NewInputStreamReader (InputStream); Reader=NewBufferedReader (InputStreamReader);  while((Templine = Reader.readline ())! =NULL) {resultbuffer.append (templine); }        } finally {            if(OutputStreamWriter! =NULL) {outputstreamwriter.close (); }            if(OutputStream! =NULL) {outputstream.close (); }            if(Reader! =NULL) {reader.close (); }            if(InputStreamReader! =NULL) {inputstreamreader.close (); }            if(InputStream! =NULL) {inputstream.close (); }        }        returnresultbuffer.tostring (); }}

Introduction to Interface Test Learning (1)--Pre-knowledge reserve

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.