Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #173 from enyojs/ENYO-1413

ENYO-1413

Reviewed-By: Ben Combee (ben.combee@palm.com)
  • Loading branch information...
commit 3f7096747e84ce88f6c4ad73b3eeb244a6f09126 2 parents 810177a + 9fbb08e
Ben Combee unwiredben authored
36 source/ajax/Ajax.js
View
@@ -68,6 +68,11 @@ enyo.kind({
}
}
enyo.mixin(xhr_headers, this.headers);
+ // don't pass in headers structure if there are no headers defined as this messes
+ // up CORS code for IE8-9
+ if (enyo.keys(xhr_headers).length === 0) {
+ xhr_headers = undefined;
+ }
//
try {
this.xhr = enyo.xhr.request({
@@ -91,6 +96,15 @@ enyo.kind({
},
receive: function(inText, inXhr) {
if (!this.failed && !this.destroyed) {
+ var text;
+ if (typeof inXhr.responseText === "string") {
+ text = inXhr.responseText;
+ }
+ this.xhrResponse = {
+ status: inXhr.status,
+ headers: enyo.Ajax.parseResponseHeaders(inXhr),
+ body: text
+ };
if (this.isFailure(inXhr)) {
this.fail(inXhr.status);
} else {
@@ -99,8 +113,9 @@ enyo.kind({
}
},
fail: function(inError) {
- // on failure, explicitly cancel the XHR to
- // prevent further responses
+ // on failure, explicitly cancel the XHR to prevent
+ // further responses. cancellation also resets the
+ // response headers & body,
if (this.xhr) {
enyo.xhr.cancel(this.xhr);
this.xhr = null;
@@ -170,6 +185,23 @@ enyo.kind({
}
}
return pairs.join("&");
+ },
+ parseResponseHeaders: function(xhr) {
+ var headers = {};
+ var headersStr = [];
+ if (xhr.getAllResponseHeaders) {
+ headersStr = xhr.getAllResponseHeaders().split('\r\n');
+ }
+ for (var i = 0; i < headersStr.length; i++) {
+ var headerStr = headersStr[i];
+ var index = headerStr.indexOf(': ');
+ if (index > 0) {
+ var key = headerStr.substring(0, index);
+ var val = headerStr.substring(index + 2);
+ headers[key] = val;
+ }
+ }
+ return headers;
}
}
});
14 source/ajax/xhr.js
View
@@ -67,12 +67,20 @@ enyo.xhr = {
makeReadyStateHandler: function(inXhr, inCallback) {
if (window.XDomainRequest && inXhr instanceof XDomainRequest) {
inXhr.onload = function() {
- inCallback.apply(null, [inXhr.responseText, inXhr]);
+ var text;
+ if (typeof inXhr.responseText === "string") {
+ text = inXhr.responseText;
+ }
+ inCallback.apply(null, [text, inXhr]);
};
}
inXhr.onreadystatechange = function() {
if (inXhr.readyState == 4) {
- inCallback.apply(null, [inXhr.responseText, inXhr]);
+ var text;
+ if (typeof inXhr.responseText === "string") {
+ text = inXhr.responseText;
+ }
+ inCallback.apply(null, [text, inXhr]);
}
};
},
@@ -94,7 +102,7 @@ enyo.xhr = {
// only use XDomainRequest when it exists, no extra headers were set, and the
// target URL maps to a domain other than the document origin.
if (enyo.platform.ie < 10 && window.XDomainRequest && !inParams.headers &&
- !this.inOrigin(inParams.inUrl) && !/^file:\/\//.test(window.location.href)) {
+ !this.inOrigin(inParams.url) && !/^file:\/\//.test(window.location.href)) {
return new XDomainRequest();
}
} catch(e) {}
20 tools/test/ajax/php/test5.php
View
@@ -0,0 +1,20 @@
+<?php
+$method = @$_SERVER['REQUEST_METHOD'];
+switch ($method) {
+ case 'GET':
+ // Set a custom HTTP response code: pick a working one (no clue)
+ //$this->header('HTTP/1.1 500: Internal Server Error');
+ //header('HTTP/1.1 500 Internal Server Error');
+ //http_response_code(500); // php >= 5.4
+ header('X-PHP-Response-Code: 500', true, 500); // php >= 4.3
+
+ // can't use odd charset due to IE exception throwing
+ //header('Content-Type: text/plain; charset=x-user-unparseable');
+ header('Content-Type: text/plain; charset=utf-8');
+
+ echo "my error description";
+ break;
+ default:
+ echo "invalid method";
+}
+?>
33 tools/test/ajax/tests/AjaxTest.js
View
@@ -2,14 +2,18 @@ enyo.kind({
name: "AjaxTest",
kind: enyo.TestSuite,
timeout: 10000,
- _testAjax: function(inProps, inParams, inAssertFn) {
+ _testAjax: function(inProps, inParams, inAssertFn, inAssertErrFn) {
return new enyo.Ajax(inProps)
.response(this, function(inSender, inValue) {
this.finish(inAssertFn.call(null, inValue) ? "" : "bad response: " + inValue);
})
- .error(this, function(inSender, inValue) {
- this.finish("bad status: " + inValue);
- enyo.error(inValue);
+ .error(this, function(inSender, inError) {
+ if (!inAssertErrFn) {
+ this.finish("bad status: " + inError.toString());
+ enyo.error(inError);
+ } else {
+ this.finish(inAssertErrFn.call(null, inError) ? "" : "bad response: " + inError);
+ }
})
.go(inParams);
},
@@ -72,7 +76,7 @@ enyo.kind({
var contentType = "application/x-www-form-urlencoded";
this._testAjax({url: "php/test4.php", method: "POST", postBody: "data"}, null, function(inValue) {
var status = (inValue.ctype.indexOf(contentType) === 0);
- if (status) {
+ if (!status) {
enyo.log("Bad CT: " + inValue.ctype + " expected: " + contentType);
}
return status;
@@ -84,9 +88,9 @@ enyo.kind({
formData.append('token', "data");
var contentType = "multipart/form-data";
this._testAjax({url: "php/test4.php", method: "POST", postBody: formData}, null, function(inValue) {
- var status = (inValue.ctype.indexOf(contentType) === 0)
- && (inValue.ctype.indexOf("boundary=--") > 10);
- if ( ! status) {
+ var status = (inValue.ctype.indexOf(contentType) === 0) &&
+ (inValue.ctype.indexOf("boundary=--") > 10);
+ if (!status) {
enyo.log("Bad CT: " + inValue.ctype + " expected: " + contentType);
}
return status;
@@ -135,5 +139,18 @@ enyo.kind({
enyo.job("timeouttest", enyo.bind(this, function() {this.finish("");}), 4000);
})
.go();
+ },
+ // expected to fail
+ testErrorResponse: function() {
+ var req = this._testAjax({url: "php/test5.php"}, null, function(inValue) {
+ // getting success means server sent wrong response
+ return false;
+ }, function(inError) {
+ return (inError === 500) &&
+ req.xhrResponse &&
+ (req.xhrResponse.status === 500) &&
+ (req.xhrResponse.headers['Content-Type'] === "text/plain; charset=utf-8") &&
+ (req.xhrResponse.body === "my error description");
+ });
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.