Avoids function name conflict, and uses `http://httpbin.org/gzip` to test gzip functionality since only httpbin.org was added to exception list in Info.plist. (#16933)

This commit is contained in:
James Chen 2016-12-13 11:44:42 +08:00 committed by minggo
parent 636a253981
commit f26cc15373
2 changed files with 69 additions and 74 deletions

View File

@ -24,34 +24,6 @@
THE SOFTWARE.
****************************************************************************/
//some utils functions
function ensureLeftAligned (label) {
label.anchorX = 0;
label.anchorY = 1;
label.textAlign = cc.TEXT_ALIGNMENT_LEFT;
}
function streamXHREventsToLabel ( xhr, label, textbox, method ) {
// Simple events
['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'].forEach(function (eventname) {
xhr["on" + eventname] = function () {
label.string += "\nEvent : " + eventname
}
});
// Special event
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
var httpStatus = xhr.statusText;
var response = xhr.responseText.substring(0, 100) + "...";
textbox.string = method + " Response (100 chars):\n"
textbox.string += response
label.string += "\nStatus: Got " + method + " response! " + httpStatus
}
}
}
var XHRArrayBufferTestScene = TestScene.extend({
ctor:function () {
this._super(true);
@ -81,24 +53,52 @@ var XHRArrayBufferTestLayer = cc.Layer.extend({
this.sendPostArrayBuffer();
},
//some utils functions
ensureLeftAligned: function(label) {
label.anchorX = 0;
label.anchorY = 1;
label.textAlign = cc.TEXT_ALIGNMENT_LEFT;
},
streamXHREventsToLabel: function( xhr, label, textbox, method, title ) {
// Simple events
['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'].forEach(function (eventname) {
xhr["on" + eventname] = function () {
label.string += "\nEvent : " + eventname;
};
});
// Special event
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
var httpStatus = xhr.statusText;
var response = xhr.responseText.substring(0, 100) + "...";
cc.log("title:" + title + ", response:\n" + xhr.responseText);
textbox.string = method + " Response (100 chars):\n";
textbox.string += response;
label.string += "\nStatus: Got " + method + " response! " + httpStatus;
}
};
},
sendPostArrayBuffer: function() {
var statusPostLabel = new cc.LabelTTF("Status:", "Thonburi", 12);
this.addChild(statusPostLabel, 1);
statusPostLabel.x = 10;
statusPostLabel.y = winSize.height - 100;
ensureLeftAligned(statusPostLabel);
this.ensureLeftAligned(statusPostLabel);
statusPostLabel.setString("Status: Send Post Request to httpbin.org with ArrayBuffer");
var responseLabel = new cc.LabelTTF("", "Thonburi", 16);
this.addChild(responseLabel, 1);
ensureLeftAligned(responseLabel);
this.ensureLeftAligned(responseLabel);
responseLabel.x = 10;
responseLabel.y = winSize.height / 2;
var xhr = cc.loader.getXMLHttpRequest();
streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST");
this.streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST", "sendPostArrayBuffer");
xhr.open("POST", "http://httpbin.org/post");
//set Content-type "text/plain" to post ArrayBuffer or ArrayBufferView

View File

@ -24,34 +24,6 @@
THE SOFTWARE.
****************************************************************************/
//some utils functions
function ensureLeftAligned (label) {
label.anchorX = 0;
label.anchorY = 1;
label.textAlign = cc.TEXT_ALIGNMENT_LEFT;
}
function streamXHREventsToLabel ( xhr, label, textbox, method ) {
// Simple events
['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'].forEach(function (eventname) {
xhr["on" + eventname] = function () {
label.string += "\nEvent : " + eventname;
};
});
// Special event
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
var httpStatus = xhr.statusText;
var response = xhr.responseText.substring(0, 100) + "...";
textbox.string = method + " Response (100 chars):\n";
textbox.string += response;
label.string += "\nStatus: Got " + method + " response! " + httpStatus;
}
};
}
var XHRTestScene = TestScene.extend({
ctor:function () {
this._super(true);
@ -83,33 +55,56 @@ var XHRTestLayer = cc.Layer.extend({
this.sendPostForms();
},
ensureLeftAligned: function(label) {
label.anchorX = 0;
label.anchorY = 1;
label.textAlign = cc.TEXT_ALIGNMENT_LEFT;
},
streamXHREventsToLabel: function( xhr, label, textbox, method, title ) {
// Simple events
['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'].forEach(function (eventname) {
xhr["on" + eventname] = function () {
label.string += "\nEvent : " + eventname;
};
});
// Special event
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
var httpStatus = xhr.statusText;
var response = xhr.responseText.substring(0, 100) + "...";
cc.log("title:" + title + ", response:\n" + xhr.responseText);
textbox.string = method + " Response (100 chars):\n";
textbox.string += response;
label.string += "\nStatus: Got " + method + " response! " + httpStatus;
}
};
},
sendGetRequest: function() {
var statusGetLabel = new cc.LabelTTF("Status:", "Thonburi", 12);
this.addChild(statusGetLabel, 1);
statusGetLabel.x = 10;
statusGetLabel.y = winSize.height - 100;
ensureLeftAligned(statusGetLabel);
this.ensureLeftAligned(statusGetLabel);
statusGetLabel.setString("Status: Send Get Request to httpbin.org");
var responseLabel = new cc.LabelTTF("", "Thonburi", 16);
this.addChild(responseLabel, 1);
ensureLeftAligned(responseLabel);
this.ensureLeftAligned(responseLabel);
responseLabel.x = 10;
responseLabel.y = winSize.height / 2;
var xhr = cc.loader.getXMLHttpRequest();
streamXHREventsToLabel(xhr, statusGetLabel, responseLabel, "GET");
this.streamXHREventsToLabel(xhr, statusGetLabel, responseLabel, "GET", "sendGetRequest");
// 5 seconds for timeout
xhr.timeout = 5000;
//set arguments with <URL>?xxx=xxx&yyy=yyy
if(cc.sys.isNative){
xhr.open("GET", "http://geek.csdn.net/news/detail/33683", true);
}else{
xhr.open("GET", "http://httpbin.org/get?show_env=1", true);
}
xhr.open("GET", "http://httpbin.org/gzip", true);
xhr.setRequestHeader("Accept-Encoding","gzip,deflate");
xhr.send();
@ -121,18 +116,18 @@ var XHRTestLayer = cc.Layer.extend({
statusPostLabel.x = winSize.width / 10 * 3;
statusPostLabel.y = winSize.height - 100;
ensureLeftAligned(statusPostLabel);
this.ensureLeftAligned(statusPostLabel);
statusPostLabel.setString("Status: Send Post Request to httpbin.org with plain text");
var responseLabel = new cc.LabelTTF("", "Thonburi", 16);
this.addChild(responseLabel, 1);
ensureLeftAligned(responseLabel);
this.ensureLeftAligned(responseLabel);
responseLabel.x = winSize.width / 10 * 3;
responseLabel.y = winSize.height / 2;
var xhr = cc.loader.getXMLHttpRequest();
streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST");
this.streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST", "sendPostPlainText");
xhr.open("POST", "http://httpbin.org/post");
@ -147,18 +142,18 @@ var XHRTestLayer = cc.Layer.extend({
statusPostLabel.x = winSize.width / 10 * 7;
statusPostLabel.y = winSize.height - 100;
ensureLeftAligned(statusPostLabel);
this.ensureLeftAligned(statusPostLabel);
statusPostLabel.setString("Status: Send Post Request to httpbin.org width form data");
var responseLabel = new cc.LabelTTF("", "Thonburi", 16);
this.addChild(responseLabel, 1);
ensureLeftAligned(responseLabel);
this.ensureLeftAligned(responseLabel);
responseLabel.x = winSize.width / 10 * 7;
responseLabel.y = winSize.height / 2;
var xhr = cc.loader.getXMLHttpRequest();
streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST");
this.streamXHREventsToLabel(xhr, statusPostLabel, responseLabel, "POST", "sendPostForms");
xhr.open("POST", "http://httpbin.org/post");
//set Content-Type "application/x-www-form-urlencoded" to post form data
@ -185,4 +180,4 @@ var runXHRTest = function () {
var pLayer = new XHRTestLayer();
pScene.addChild(pLayer);
cc.director.runScene(pScene);
};
};