Beispiel #1
0
        public async Task Basic_Http_Get_Headers_Are_Retrieved()
        {
            var subject = new TestObject();
            var manager = new RuntimeManager(new ManualManagerSettings {
                ScriptTimeoutMilliSeconds = 0
            });

            HttpPackageHelpers.RegisterPackage(manager.RequireManager);

            manager.AddConsoleReference = true;
            var options = new ExecutionOptions();

            options.HostObjects.Add(new HostObject {
                Name = "subject", Target = subject
            });

            var scriptAwaiter = new ScriptAwaiter();

            options.HostObjects.Add(new HostObject {
                Name = "scriptAwaiter", Target = scriptAwaiter
            });
            options.HostObjects.Add(new HostObject {
                Name = "cookieA", Target = new CookieContainer()
            });

            var code = "var requestFactory = require('javascript_request_factory');" +
                       "var http = requestFactory.create({url:'http://www.baidu.com/'});" +
                       "var data = encodeURIComponent('errorMsg=&to=http%253A%252F%252Fwww.zhonghuasuan.com%252F&token=5b9c1a3c6f2db8c737b7788ac560a397&account=111111&password=111111');" +
                       "Console.WriteLine('aaaa111'); var aa = http.getString({timeout:10,headers:{token:'aaaaaatoken'},cookieContainer:cookieA});Console.WriteLine(aa);";

            await manager.ExecuteAsync("testScript", code, options);
        }
Beispiel #2
0
        public async void Basic_Http_Get_Headers_Are_Retrieved()
        {
            var subject = new TestObject();
            var manager = new RuntimeManager(new ManualManagerSettings {
                ScriptTimeoutMilliSeconds = 0
            });

            PackageHelpers.RegisterRequestPackages();

            manager.AddConsoleReference = true;
            var options = new ExecutionOptions();

            options.HostObjects.Add(new HostObject {
                Name = "subject", Target = subject
            });

            var scriptAwaiter = new ScriptAwaiter();

            options.HostObjects.Add(new HostObject {
                Name = "scriptAwaiter", Target = scriptAwaiter
            });

            var code = "var request = require('request');" +
                       "request({url: 'http://api.icndb.com/jokes/random/1', json: true}," +
                       " function (error, response, body) {subject.Response = response; subject.Headers = response.headers; scriptAwaiter.Callback();});";

            await manager.ExecuteAsync("testScript", code, options);

            await scriptAwaiter.T;

            subject.Headers.Count().ShouldBeGreaterThan(0);
        }
Beispiel #3
0
        public async void Basic_Http_Get_Body_Is_Retrieved()
        {
            var subject = new TestObject();
            var manager = new RuntimeManager(new ManualManagerSettings {
                ScriptTimeoutMilliSeconds = 0
            });

            HttpPackageHelpers.RegisterPackage(manager.RequireManager);

            manager.AddConsoleReference = true;
            var options = new ExecutionOptions();

            options.HostObjects.Add(new HostObject {
                Name = "subject", Target = subject
            });

            var scriptAwaiter = new ScriptAwaiter();

            options.HostObjects.Add(new HostObject {
                Name = "scriptAwaiter", Target = scriptAwaiter
            });

            var code = "var request = require('request');" +
                       "request({url: 'http://api.icndb.com/jokes/random/1', json: true}," +
                       " function (error, response, body) {subject.Response = response; subject.Body = body; subject.Joke = body.value[0].joke; scriptAwaiter.Callback();});";

            await manager.ExecuteAsync("testScript", code, options);

            await scriptAwaiter.T;

            subject.Joke.ShouldNotBeNull();
        }
Beispiel #4
0
        private void invokeJsCode(string code, bool isDebuger = false)
        {
            code = code.Replace("``{{", "\"+").Replace("}}``", "+\"").Replace("`{{", "\'+").Replace("}}`", "+\'").Replace("{{", "\"+").Replace("}}", "+\"");
            bool tryCatch = this.catchBox.CheckState.Equals(CheckState.Checked);

            isRun = true;
            Enable(false);
            Task.Factory.StartNew(async() =>
            {
                try
                {
                    var codeLines = string.Join("\n", code.Split('\n').ToList().Select(r => (tryCatch? "        " : "    ") + r));
                    if (codeLines.StartsWith("        "))
                    {
                        codeLines = codeLines.Substring(8);
                    }
                    else if (codeLines.StartsWith("    "))
                    {
                        codeLines = codeLines.Substring(4);
                    }
                    if (isDebuger)
                    {
                        if (tryCatch)
                        {
                            code = $@"
var tabris,console;
(function () 
{{
    debugger;
    try{{
        tabris = tabris || require('javascript_tabris');
        console = console || this.tabris.create('LOG');
        {codeLines}
    }}catch(err){{
        host.err=err.message;
        host.ex=err;
    }}
}})()";
                        }
                        else
                        {
                            code = $@"
var tabris,console;
(function () 
{{
    debugger;
    tabris = tabris || require('javascript_tabris');
    console = console || this.tabris.create('LOG');
    {codeLines}
}})()";
                        }


                        //code = "debugger;\nvar tabris;\n" + "(function (){\n  tabris = tabris || require('javascript_tabris'); \n" + code + "\n})();";
                    }
                    else
                    {
                        if (tryCatch)
                        {
                            code = $@"
var tabris,console;
(function () 
{{
    try{{
        tabris = tabris || require('javascript_tabris');
        console = console || this.tabris.create('LOG');
        {codeLines}
    }}catch(err){{
        host.err=err.message;
        host.ex=err;
    }}
}})()";
                            //code = "var tabris;\n" + "(function (){\n  tabris = tabris || require('javascript_tabris'); \n" + code + "\n})();";
                        }
                        else
                        {
                            code = $@"
var tabris,console;
(function () 
{{
    tabris = tabris || require('javascript_tabris');
    console = console || this.tabris.create('LOG');
    {codeLines}
}})()";
                        }
                    }

                    dynamic host = new ExpandoObject();
                    var option   = new ExecutionOptions
                    {
                        HostObjects = new List <HostObject>
                        {
                            new HostObject {
                                Name = "host", Target = host
                            }
                        }
                    };
                    var scriptAwaiter = new ScriptAwaiter();
                    if (code.Contains("scriptAwaiter"))
                    {
                        option.HostObjects.Add(new HostObject {
                            Name = "scriptAwaiter", Target = scriptAwaiter
                        });
                    }

                    try
                    {
                        await manager.ExecuteAsync(Guid.NewGuid().ToString(), code, option);
                        if (code.Contains("scriptAwaiter"))
                        {
                            await scriptAwaiter.T;
                        }
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        if (isDebuger)
                        {
                            this.BeginInvoke(new EventHandler(delegate
                            {
                                this.codemirrow.Visible = true;
                                debuggerBrower.Visible  = false;
                            }));
                        }
                    }

                    try
                    {
                        if (!string.IsNullOrEmpty(host.err.ToString()))
                        {
                            logAction(LogLevel.ERROR, host.err, "");
                        }

                        var exception = host.ex as DynamicObject;
                        if (exception != null)
                        {
                            var kv = exception.GetDynamicProperties();
                            if (kv != null)
                            {
                                foreach (var itemKeyValuePair in kv)
                                {
                                    if (itemKeyValuePair.Value is Exception)
                                    {
                                        Exception ex = (Exception)itemKeyValuePair.Value;
                                        while (ex.InnerException != null)
                                        {
                                            ex = ex.InnerException;
                                        }

                                        logAction(LogLevel.ERROR, ex.Message, "");
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }


                    //if (Index == 0)
                    //{
                    //    Thread.Sleep(10000);
                    //    logAction(LogLevel.ERROR, "ces", "");
                    //}
                }
                catch (ScriptEngineException ex)
                {
                    logAction(LogLevel.ERROR, ((Microsoft.ClearScript.ScriptEngineException)ex).ErrorDetails, "");
                }
                catch (Exception ex)
                {
                    logAction(LogLevel.ERROR, ex.Message, "");
                }
                finally
                {
                    Enable(true);
                    isRun = false;
                }
            }).ContinueWith((t) =>
            {
                if (t.IsFaulted)
                {
                    Exception ex = t.Exception;
                    while (ex is AggregateException && ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }
                    logAction(LogLevel.ERROR, ex.Message, "");
                }
                else if (t.IsCanceled)
                {
                    logAction(LogLevel.ERROR, "Canclled.", "");
                }
            });
        }