QUnit uses a proxy to test the json service of different PHP development servers. qunitjson

Source: Internet
Author: User
Tags php print

QUnit uses a proxy to test the json service of different PHP development servers. qunitjson

The following unit tests are required during mobile app development:

How to build different test environments:

Vhost is configured on the local server, test server, UAT server, and official server, and the domain name is mapped to the local php Directory.

The official domain name www.xxxxxx.com points to the IP address of the official server, and other development servers are tested by specifying the proxy IP address.

When JavaScript code accesses the server using ajax, it does not have the proxy function specified by itself. to simplify the configuration of the test client, create proxy. php,

Use the curl library on it to set proxy to access different environments;

The diagnosis information can be found in the following aspects during the test:

The execution result is as follows:

The main code file is as follows:

Proxy. php

1 <? Php 2/* 3 parameters: 4 url ==> call url; ex: http://a.xxxxxx.com/login.php 5 data ==> post data; ex: {"u": "abcd ", "p": "password"} 6 cookie ==> cookie, can empty; ex: PHPSESSID = oh965kppina5fjdi6gv1c2mls5 7 proxy ==> proxy info, can empty; ex: test.xxxxxx.com: 8001 8 */9 10 // phpinfo (); 11 // die (); 12 13/* simulate the browser */14 $ user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2 ;. net clr 1.1.4322) "; 1 5 16 $ t = isset ($ _ REQUEST ['_ T'])? $ _ REQUEST ['_ T']: null; 17 $ url = isset ($ _ REQUEST [' _ url'])? $ _ REQUEST ['_ url']: ""; 18 // $ data = isset ($ _ REQUEST ['data'])? $ _ REQUEST ['data']: ""; 19 $ proxy = isset ($ _ REQUEST ['_ proxy'])? $ _ REQUEST ['_ proxy']: ""; 20 $ cookie = isset ($ _ REQUEST [' _ cookie '])? $ _ REQUEST ['_ cookies']: ""; 21 22 $ data = array (); 23 // treat all non-special parameters as post data 24 foreach ($ _ REQUEST as $ key => $ value) {25 if (! In_array ($ key, array ('_ url',' _ t', '_ proxy',' _ cookier') 26 $ data [$ key] = $ value; 27} 28 29 // test data 30 if ($ t) {31 $ url = "http://a.ajmide.com/login.php"; 32 $ data = array ("u" => "aabbcc ", "p" => "123456"); 33 $ proxy = "test.xxxxxx.com: 8001"; 34 $ cookie = ""; 35} else {36 // $ data = json_decode ($ data); 37} 38 // modify agent39 $ user_agent = "ajmd/test (". $ proxy. ")"; 40 $ content = vcurl ($ url, $ data, $ cookie, $ proxy ); 41 // print_r ($ content); 42 echo ($ content); 43 // $ content = $ content. "test"; 44 45 // $ content = json_decode ($ content); 46 // print_r ($ content); 47 // echo ("test "); 48 // print_r (json_encode ($ content, JSON_UNESCAPED_UNICODE); 49 50 function vcurl ($ url, $ data, $ cookie, $ proxy) {// function 51 $ curl = curl_init () Through simulated logon; // starts a CURL session 52 if ($ proxy & $ proxy! = "") {53 // The following Code sets the proxy server 54 curl_setopt ($ curl, CURLOPT_PROXY, $ proxy); 55} 56 curl_setopt ($ curl, CURLOPT_URL, $ url ); // address 57 if ($ cookie & $ cookie! = "") {58 curl_setopt ($ curl, CURLOPT_COOKIE, $ cookie); 59} 60 // curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0 ); // check the certificate source 61 // curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check from the certificate whether the SSL encryption algorithm has 62 curl_setopt ($ curl, CURLOPT_USERAGENT, $ GLOBALS ['user _ agent']); // simulate the user's browser 63 // @ curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1 ); // use automatic redirect 64 // curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1 ); // Automatically set Referer65 curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request 66 curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data ); // Post submitted data packet 67 // curl_setopt ($ curl, CURLOPT_COOKIEJAR, $ GLOBALS ['cookie _ file']); // name of the file storing Cookie information 68 // curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ GLOBALS ['cookie _ file']); // read the Cookie information stored above 69 curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // set the timeout limit to prevent endless loops 70 curl_set Opt ($ curl, CURLOPT_HEADER, 1); // display the returned Header content 71 curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // The obtained information is returned in the form of a file stream 72 $ tmpInfo = curl_exec ($ curl); // execute Operation 73 if (curl_errno ($ curl )) {74 echo 'call curl error. '. curl_error ($ curl); 75} 76 $ info = curl_getinfo ($ curl); 77 $ httpHeaderSize = $ info ['header _ size']; 78 $ pHeader = substr ($ tmpInfo, 0, $ httpHeaderSize); 79 $ pContent = substr ($ tmpIn Fo, $ httpHeaderSize); 80 // echo "<pre>"; 81 /// print_r (curl_getinfo ($ curl); 82 // print_r ($ pHeader ); 83 // echo "</pre>"; 84 curl_close ($ curl); // disable CURL session 85 // if there is a cookie, put the cookie into the sess field of the returned data. 86 if (preg_match ("/Set-Cookie :(. *);/iU ", $ pHeader, $ arr) {87 $ json = json_decode ($ pContent); 88 if ($ json! = NULL) {89 $ json-> cookie = $ arr [1]; 90 $ pContent = json_encode ($ json, JSON_UNESCAPED_UNICODE ); 91} 92} 93 94 // return $ tmpInfo; // return data 95 return $ pContent; 96} 97?>

Test-core.js

1 var proxy = ""; 2 3 function login (user, password, proxy, success) {4 post ('HTTP: // a.xxxxxx.com/login.php', {u: user, p: password}, null, proxy, success); 5} 6 7 function post (_ url, data, cookie, proxy, success) {8 var url = 'proxy. php? _ Url = '+ _ url; 9 if (proxy) 10 url = url + "& _ proxy =" + proxy; 11 if (cookie) 12 url = url + "& _ cookie =" + encodeURI (cookie); 13 console. log ("===>", _ url, proxy, data); 14 $. ajax ({15 type: 'post', 16 url: url, 17 data: data, 18 dataType: 'json' 19}) 20. done (function (d, status, xhr) {21 console. log ("<===", _ url, d); 22 success (d); 23}) 24. fail (function (xhr, status) {25 console. log ('Post error. ', status, _ url, p Roxy) 26}); 27} 28 29 function parseUriP (key) {30 var parts = document. location. search. slice (1 ). split ("&"), 31 length = parts. length, 32 I = 0, 33 current, 34 value = ""; 35 36 for (; I <length; I ++) {37 current = parts [I]. split ("="); 38 if (current [0] === key) {39 value = current [1]; 40 break; 41} 42} 43 return value; 44} 45 46 (function () {47 var p = parseUriP ("proxy"); 48 if (p! = "") 49 proxy = p; 50 })();View Code

Test-msg.html

1 <! Doctype html> 2 Test-msg.js

1 module ("message system"); 2 asyncTest ("log on to get message", function () {3 login ('aabbcc ', '123', proxy, function (data) {4 // var d = JSON. parse (data); 5 equal (data. code, 0, 'logon successful: '+ data. cookie); 6 this. cookie = data. cookie; 7 console. log ('login sess: ', data); 8 post ('HTTP: // a.xxxxxx.com/msg_get_grouplist.php', {I: 0, c: 20}, this. cookie, proxy, function (data) {9 equal (data. code, 0, JSON. stringify (data); 10 start (); 11}); 12}); 13}); 14 15 16 asyncTest ("log on to send messages", function () {17 login ('aabbccdd', '20140901', proxy, function (data) {18 // var d = JSON. parse (data); 19 equal (data. code, 0, 'logon successful: '+ data. cookie); 20 this. cookie = data. cookie; 21 console. log ('login sess: ', data); 22 post ('HTTP: // a.xxxxxx.com/msg_get_grouplist.php', {I: 0, c: 20}, this. cookie, proxy, function (data) {23 equal (data. code, 0, JSON. stringify (data); 24 start (); 25}); 26}); 27 });

 


How does php print the original json returned from the server?

What you get is an array. The server does not have jsonencode (). You can use var_dump () to troubleshoot the error ~

I used ajax to return data today. Would you like to ask if the server wants to return json, phpini, or those parameters?

Is your php output in json format?
 

Related Article

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.