You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ko...@apache.org on 2014/11/28 10:22:20 UTC

[2/2] olingo-odata4-js git commit: [OLINGO-439] add scenario tests

[OLINGO-439] add scenario tests


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/commit/49d9ecb5
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/tree/49d9ecb5
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/diff/49d9ecb5

Branch: refs/heads/master
Commit: 49d9ecb5a1acbc3016767d189b860ea37f2b49f1
Parents: 2d9599b
Author: Sven Kobler <sv...@sap.com>
Authored: Fri Nov 28 10:22:14 2014 +0100
Committer: Sven Kobler <sv...@sap.com>
Committed: Fri Nov 28 10:22:14 2014 +0100

----------------------------------------------------------------------
 odatajs/package.json               |   3 +-
 odatajs/src/lib/odata/net.js       | 298 +++++++++-----------------------
 odatajs/src/lib/xml.js             |   7 +-
 odatajs/test/test-demo-scenario.js | 285 ++++++++++++++++++++++++++++--
 4 files changed, 361 insertions(+), 232 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/49d9ecb5/odatajs/package.json
----------------------------------------------------------------------
diff --git a/odatajs/package.json b/odatajs/package.json
index 0323953..6c782bf 100644
--- a/odatajs/package.json
+++ b/odatajs/package.json
@@ -41,6 +41,7 @@
     "grunt-contrib-uglify": "^0.4.0",
     "grunt-curl": "^2.0.2",
     "grunt-jsdoc": "^0.5.6",
-    "grunt-nuget": "^0.1.3"
+    "grunt-nuget": "^0.1.3",
+    "xmldom": "^0.1.19"
   }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/49d9ecb5/odatajs/src/lib/odata/net.js
----------------------------------------------------------------------
diff --git a/odatajs/src/lib/odata/net.js b/odatajs/src/lib/odata/net.js
index 6e0f6c8..7d4f23b 100644
--- a/odatajs/src/lib/odata/net.js
+++ b/odatajs/src/lib/odata/net.js
@@ -20,8 +20,9 @@
 /** @module odata/net */
 
 
-
+var http = require('http');
 var utils    = require('./../utils.js');
+var url = require("url");
 // Imports.
 
 var defined = utils.defined;
@@ -40,55 +41,10 @@ var ticks = 0;
  * request but may in practice return content with a different MIME type.
  */
 function canUseJSONP(request) {
-    
-    if (request.method && request.method !== "GET") {
-        return false;
-    }
-
-    return true;
+    return false;
 }
 
-/** Creates an IFRAME tag for loading the JSONP script
- * @param {String} url - The source URL of the script
- * @returns {HTMLElement} The IFRAME tag
- */
-function createIFrame(url) {
-    var iframe = window.document.createElement("IFRAME");
-    iframe.style.display = "none";
 
-    var attributeEncodedUrl = url.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
-    var html = "<html><head><script type=\"text/javascript\" src=\"" + attributeEncodedUrl + "\"><\/script><\/head><body><\/body><\/html>";
-
-    var body = window.document.getElementsByTagName("BODY")[0];
-    body.appendChild(iframe);
-
-    writeHtmlToIFrame(iframe, html);
-    return iframe;
-};
-
-/** Creates a XmlHttpRequest object.
- * @returns {XmlHttpRequest} XmlHttpRequest object.
- */
-function createXmlHttpRequest() {
-    if (window.XMLHttpRequest) {
-        return new window.XMLHttpRequest();
-    }
-    var exception;
-    if (window.ActiveXObject) {
-        try {
-            return new window.ActiveXObject("Msxml2.XMLHTTP.6.0");
-        } catch (_) {
-            try {
-                return new window.ActiveXObject("Msxml2.XMLHTTP.3.0");
-            } catch (e) {
-                exception = e;
-            }
-        }
-    } else {
-        exception = { message: "XMLHttpRequest not supported" };
-    }
-    throw exception;
-}
 
 /** Checks whether the specified URL is an absolute URL.
  * @param {String} url - URL to check.
@@ -116,68 +72,30 @@ function isLocalUrl(url) {
     return (url.indexOf(locationDomain) === 0);
 }
 
-/** Removes a callback used for a JSONP request.
- * @param {String} name - Function name to remove.
- * @param {Number} tick - Tick count used on the callback.
- */
-function removeCallback(name, tick) {
-    try {
-        delete window[name];
-    } catch (err) {
-        window[name] = undefined;
-        if (tick === ticks - 1) {
-            ticks -= 1;
-        }
-    }
-};
-
-/** Removes an iframe.
- * @param {Object} iframe - The iframe to remove.
- * @returns {Object} Null value to be assigned to iframe reference.
- */
-function removeIFrame(iframe) {
-    if (iframe) {
-        writeHtmlToIFrame(iframe, "");
-        iframe.parentNode.removeChild(iframe);
-    }
-
-    return null;
-};
 
 /** Reads response headers into array.
  * @param {XMLHttpRequest} xhr - HTTP request with response available.
  * @param {Array} headers - Target array to fill with name/value pairs.
  */
-function readResponseHeaders(xhr, headers) {
-
-    var responseHeaders = xhr.getAllResponseHeaders().split(/\r?\n/);
-    var i, len;
-    for (i = 0, len = responseHeaders.length; i < len; i++) {
-        if (responseHeaders[i]) {
-            var header = responseHeaders[i].split(": ");
-            headers[header[0]] = header[1];
+function readResponseHeaders(inHeader, outHeader) {
+    for (var property in inHeader) {
+        
+        if (inHeader.hasOwnProperty(property)) {
+            //console.log(property);
+            //console.log(inHeader[property]);
+            outHeader[property] = inHeader[property];
         }
     }
 }
 
-/** Writes HTML to an IFRAME document.
- * @param {HTMLElement} iframe - The IFRAME element to write to.
- * @param {String} html - The HTML to write.
- */
-function writeHtmlToIFrame(iframe, html) {
-    var frameDocument = (iframe.contentWindow) ? iframe.contentWindow.document : iframe.contentDocument.document;
-    frameDocument.open();
-    frameDocument.write(html);
-    frameDocument.close();
-}
+    
 
-exports.defaultHttpClient = {
-    callbackParameterName: "$callback",
 
-    formatQueryString: "$format=json",
 
-    enableJsonpCallback: false,
+exports.defaultHttpClient = {
+    formatQueryString: "$format=json",
 
+    
     /** Performs a network request.
      * @param {Object} request - Request description
      * @param {Function} success - Success callback with the response object.
@@ -187,12 +105,26 @@ exports.defaultHttpClient = {
     request: function (request, success, error) {
 
         var result = {};
-        var xhr = null;
         var done = false;
-        var iframe;
+        
+        var options = url.parse(request.requestUri);
+        options.method = request.method || "GET";
+        options.headers = {};
+        //options.auth = request.user + ':' + request.password;
+        //add headers
+        var name;
+        if (request.headers) {
+            for (name in request.headers) {
+                options.headers[name] = request.headers[name];
+            }
+        }   
+        
+
+        //console.log('options'+JSON.stringify(options));
+        var xhr = http.request(options);
 
         result.abort = function () {
-            iframe = removeIFrame(iframe);
+            //console.log('_4');
             if (done) {
                 return;
             }
@@ -206,129 +138,71 @@ exports.defaultHttpClient = {
             error({ message: "Request aborted" });
         };
 
-        var handleTimeout = function () {
-            iframe = removeIFrame(iframe);
-            if (!done) {
-                done = true;
-                xhr = null;
-                error({ message: "Request timed out" });
-            }
-        };
-
-        var name;
-        var url = request.requestUri;
-        var enableJsonpCallback = defined(request.enableJsonpCallback, this.enableJsonpCallback);
-        var callbackParameterName = defined(request.callbackParameterName, this.callbackParameterName);
-        var formatQueryString = defined(request.formatQueryString, this.formatQueryString);
-        if (!enableJsonpCallback || isLocalUrl(url)) {
-
-            xhr = createXmlHttpRequest();
-            xhr.onreadystatechange = function () {
-                if (done || xhr === null || xhr.readyState !== 4) {
-                    return;
-                }
-
-                // Workaround for XHR behavior on IE.
-                var statusText = xhr.statusText;
-                var statusCode = xhr.status;
-                if (statusCode === 1223) {
-                    statusCode = 204;
-                    statusText = "No Content";
+        // Set the timeout if available.
+        if (request.timeoutMS) {
+            //console.log('_6');
+            xhr.setTimeout(request.timeoutMS,function () {
+                if (!done) {
+                    done = true;
+                    xhr = null;
+                    error({ message: "Request timed out" });
                 }
+            });
+        }
 
-                var headers = [];
-                readResponseHeaders(xhr, headers);
+        xhr.on('error', function(e) {
+            //console.log('_22'+e);
+            var response = { requestUri: url, statusCode: 400, statusText: e.message, headers: headers, body: body };
+            error({ message: "HTTP request failed", request: request, response: response });
+        });
+             
 
-                var response = { requestUri: url, statusCode: statusCode, statusText: statusText, headers: headers, body: xhr.responseText };
+        xhr.on('response', function (resp) {
+            //console.log('1');
+            if (done || xhr === null) {
+                return;
+            }
+            //console.log('2');
+            
+            var headers = [];
+            readResponseHeaders(resp.headers, headers);
+                        
+            var body = '';
+
+            resp.on('data', function(chunk) {
+                ///console.log('chunk'+JSON.stringify(chunk));
+                body+=chunk;
+                //console.log('3');
+                
+            });
+            resp.on('end', function() {
+                //console.log('4');
+                // do what you do
+                var response = { requestUri: url, statusCode: resp.statusCode, statusText: '', headers: headers, body: body };
 
                 done = true;
                 xhr = null;
-                if (statusCode >= 200 && statusCode <= 299) {
+                if (resp.statusCode >= 200 && resp.statusCode <= 299) {
+                    //console.log('5');
+                    //console.log(response);
                     success(response);
                 } else {
                     error({ message: "HTTP request failed", request: request, response: response });
-                }
-            };
-
-            xhr.open(request.method || "GET", url, true, request.user, request.password);
-
-            // Set the name/value pairs.
-            if (request.headers) {
-                for (name in request.headers) {
-                    xhr.setRequestHeader(name, request.headers[name]);
-                }
-            }
-
-            // Set the timeout if available.
-            if (request.timeoutMS) {
-                xhr.timeout = request.timeoutMS;
-                xhr.ontimeout = handleTimeout;
-            }
-
-            xhr.send(request.body);
-        } else {
-            if (!canUseJSONP(request)) {
-                throw { message: "Request is not local and cannot be done through JSONP." };
-            }
-
-            var tick = ticks;
-            ticks += 1;
-            var tickText = tick.toString();
-            var succeeded = false;
-            var timeoutId;
-            name = "handleJSONP_" + tickText;
-            window[name] = function (data) {
-                iframe = removeIFrame(iframe);
-                if (!done) {
-                    succeeded = true;
-                    window.clearTimeout(timeoutId);
-                    removeCallback(name, tick);
-
-                    // Workaround for IE8 and IE10 below where trying to access data.constructor after the IFRAME has been removed
-                    // throws an "unknown exception"
-                    if (window.ActiveXObject) {
-                        data = window.JSON.parse(window.JSON.stringify(data));
-                    }
-
-
-                    var headers;
-                    if (!formatQueryString || formatQueryString == "$format=json") {
-                        headers = { "Content-Type": "application/json;odata.metadata=minimal", "OData-Version": "4.0" };
-                    } else {
-                        // the formatQueryString should be in the format of "$format=xxx", xxx should be one of the application/json;odata.metadata=minimal(none or full)
-                        // set the content-type with the string xxx which stars from index 8.
-                        headers = { "Content-Type": formatQueryString.substring(8), "OData-Version": "4.0" };
-                    }
-
-                    // Call the success callback in the context of the parent window, instead of the IFRAME
-                    delay(function () {
-                        removeIFrame(iframe);
-                        success({ body: data, statusCode: 200, headers: headers });
-                    });
-                }
-            };
-
-            // Default to two minutes before timing out, 1000 ms * 60 * 2 = 120000.
-            var timeoutMS = (request.timeoutMS) ? request.timeoutMS : 120000;
-            timeoutId = window.setTimeout(handleTimeout, timeoutMS);
-
-            var queryStringParams = callbackParameterName + "=parent." + name;
-            if (formatQueryString) {
-                queryStringParams += "&" + formatQueryString;
-            }
-
-            var qIndex = url.indexOf("?");
-            if (qIndex === -1) {
-                url = url + "?" + queryStringParams;
-            } else if (qIndex === url.length - 1) {
-                url = url + queryStringParams;
-            } else {
-                url = url + "&" + queryStringParams;
-            }
-
-            iframe = createIFrame(url);
+                }   
+            });
+        });
+
+        //xhr.open(request.method || "GET", url, true,);
+        //console.log(request.body);
+        //console.log('_1');
+        if (request.body) {
+            //console.log('_2');
+            xhr.write(request.body);
         }
-
+        //console.log('_3');
+        xhr.end();
+        //console.log('_4');
+         
         return result;
     }
 };

http://git-wip-us.apache.org/repos/asf/olingo-odata4-js/blob/49d9ecb5/odatajs/src/lib/xml.js
----------------------------------------------------------------------
diff --git a/odatajs/src/lib/xml.js b/odatajs/src/lib/xml.js
index b8af4fe..fdffe10 100644
--- a/odatajs/src/lib/xml.js
+++ b/odatajs/src/lib/xml.js
@@ -175,7 +175,12 @@ function xmlThrowParserError(exceptionOrReason, srcText, errorXmlText) {
  * This function will throw an exception in case of a parse error
  */
 function xmlParse(text) {
-    var domParser = window.DOMParser && new window.DOMParser();
+    var domParser = undefined;
+    if (utils.inBrowser()) {
+        domParser = window.DOMParser && new window.DOMParser();
+    } else {
+        domParser = new (require('xmldom').DOMParser)();
+    }
     var dom;
 
     if (!domParser) {