コード例 #1
0
        public void HttpResponseDataInResult()
        {
            MockServiceTracer tracer = new MockServiceTracer();

            System.Net.Http.HttpResponseMessage httpResponse = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
            httpResponse.Headers.Add("x-ms-header", "value");
            tracer.HttpResponses.Add(httpResponse);

            LiveTestRequest request = new LiveTestRequest();

            request.Id           = "12345";
            request.JsonRpc      = "2.0";
            request.HttpResponse = true;

            CommandExecutionResult result   = new CommandExecutionResult(null, null, false);
            LiveTestResponse       response = request.MakeResponse(result, tracer, this.logger);

            Assert.NotNull(response.Result.Headers);
            Assert.True(response.Result.Headers is Dictionary <string, object>);
            Dictionary <string, object> headers = (Dictionary <string, object>)response.Result.Headers;

            Assert.Equal(1, headers.Count);
            Assert.True(headers.ContainsKey("x-ms-header"));
            Assert.Equal(new string[] { "value" }, headers["x-ms-header"]);

            Assert.Equal((long)System.Net.HttpStatusCode.Accepted, response.Result.StatusCode);
        }
コード例 #2
0
        public void MultipleResultResponse()
        {
            MockServiceTracer tracer    = new MockServiceTracer();
            object            psResult1 = 5;
            object            psResult2 = "test";
            List <object>     psResults = new List <object>();

            psResults.Add(psResult1);
            psResults.Add(psResult2);
            CommandExecutionResult result  = new CommandExecutionResult(psResults, null, false);
            LiveTestRequest        request = new LiveTestRequest();

            request.Id      = "12345";
            request.JsonRpc = "2.0";
            LiveTestResponse response = request.MakeResponse(result, tracer, this.logger);

            Assert.Equal(request.Id, response.Id);
            Assert.Equal(request.JsonRpc, response.JsonRpc);

            Assert.NotNull(response.Result);
            Assert.Null(response.Result.Headers);
            Assert.Equal(default(long), response.Result.StatusCode);
            Assert.NotNull(response.Result.Response);
            Assert.Collection <object>((object[])response.Result.Response, new Action <object>[]
            {
                (obj) =>
                {
                    Assert.Equal(psResult1, obj);
                },
                (obj) =>
                {
                    Assert.Equal(psResult2, obj);
                }
            });
        }
コード例 #3
0
        public void SingleErrorResponse()
        {
            MockServiceTracer tracer      = new MockServiceTracer();
            object            errorResult = 5;
            List <object>     errors      = new List <object>();

            errors.Add(errorResult);
            CommandExecutionResult result  = new CommandExecutionResult(null, errors, true);
            LiveTestRequest        request = new LiveTestRequest();

            request.Id      = "12345";
            request.JsonRpc = "2.0";
            LiveTestResponse response = request.MakeResponse(result, tracer, this.logger);

            Assert.Equal(request.Id, response.Id);
            Assert.Equal(request.JsonRpc, response.JsonRpc);

            Assert.NotNull(response.Error);
            Assert.Equal(-32600, response.Error.Code); // Invalid Request error code defined by LSP

            Assert.NotNull(response.Error.Data);
            Assert.Null(response.Error.Data.Headers);
            Assert.Equal(default(long), response.Error.Data.StatusCode);
            Assert.NotNull(response.Error.Data.Response);
            Assert.Equal(errorResult, response.Error.Data.Response);
        }
コード例 #4
0
        public void NullResultResponse()
        {
            MockServiceTracer      tracer  = new MockServiceTracer();
            CommandExecutionResult result  = new CommandExecutionResult(null, null, false);
            LiveTestRequest        request = new LiveTestRequest();

            request.Id      = "12345";
            request.JsonRpc = "2.0";
            LiveTestResponse response = request.MakeResponse(result, tracer, this.logger);

            Assert.Equal(request.Id, response.Id);
            Assert.Equal(request.JsonRpc, response.JsonRpc);

            Assert.NotNull(response.Result);
            Assert.Null(response.Result.Headers);
            Assert.Equal(default(long), response.Result.StatusCode);
            Assert.Null(response.Result.Response);
        }
コード例 #5
0
        public void HttpResponseDataInErrorNoHttpResponseEnabled()
        {
            MockServiceTracer tracer = new MockServiceTracer();

            System.Net.Http.HttpResponseMessage httpResponse = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.BadGateway);
            httpResponse.Headers.Add("x-ms-header", "value");
            tracer.HttpResponses.Add(httpResponse);

            LiveTestRequest request = new LiveTestRequest();

            request.Id      = "12345";
            request.JsonRpc = "2.0";

            CommandExecutionResult result   = new CommandExecutionResult(null, null, true);
            LiveTestResponse       response = request.MakeResponse(result, tracer, this.logger);

            Assert.Null(response.Error.Data.Headers);
            Assert.Equal(default(long), response.Error.Data.StatusCode);
        }
コード例 #6
0
        public void ExceptionResponse()
        {
            int             errorCode = 1;
            Exception       ex        = new NotImplementedException();
            LiveTestRequest request   = new LiveTestRequest();

            request.Id      = "12345";
            request.JsonRpc = "2.0";
            LiveTestResponse response = request.MakeResponse(ex, errorCode);

            Assert.Equal(request.Id, response.Id);
            Assert.Equal(request.JsonRpc, response.JsonRpc);

            Assert.NotNull(response.Error);
            Assert.Equal(errorCode, response.Error.Code);

            Assert.NotNull(response.Error.Data);
            Assert.Null(response.Error.Data.Headers);
            Assert.Equal(default(long), response.Error.Data.StatusCode);
            Assert.Equal(ex, response.Error.Data.Response);
        }
コード例 #7
0
        public void MultipleErrorResponse()
        {
            MockServiceTracer tracer       = new MockServiceTracer();
            object            errorResult1 = 5;
            object            errorResult2 = "test";
            List <object>     errors       = new List <object>();

            errors.Add(errorResult1);
            errors.Add(errorResult2);
            CommandExecutionResult result  = new CommandExecutionResult(null, errors, true);
            LiveTestRequest        request = new LiveTestRequest();

            request.Id      = "12345";
            request.JsonRpc = "2.0";
            LiveTestResponse response = request.MakeResponse(result, tracer, this.logger);

            Assert.Equal(request.Id, response.Id);
            Assert.Equal(request.JsonRpc, response.JsonRpc);

            Assert.NotNull(response.Error);
            Assert.Equal(-32600, response.Error.Code); // Invalid Request error code defined by LSP

            Assert.NotNull(response.Error.Data);
            Assert.Null(response.Error.Data.Headers);
            Assert.Equal(default(long), response.Error.Data.StatusCode);
            Assert.NotNull(response.Error.Data.Response);
            Assert.True(response.Error.Data.Response is object[]);
            Assert.Collection <object>((object[])response.Error.Data.Response, new Action <object>[]
            {
                (obj) =>
                {
                    Assert.Equal(errorResult1, obj);
                },
                (obj) =>
                {
                    Assert.Equal(errorResult2, obj);
                }
            });
        }
コード例 #8
0
        public async Task RunAsync()
        {
            if (this.IsRunning)
            {
                throw new InvalidOperationException("Server is already running.");
            }

            // Retrieve all module information using the current runspace manager
            this.currentModule        = this.parameters.RunspaceManager.GetModuleInfo(this.parameters.ModulePath);
            this.currentModule.Logger = this.parameters.Logger;

            // Parse specifications/metadata files for extra information, e.g. parameter renaming
            if (this.parameters.SpecificationPaths != null)
            {
                foreach (string specificationPath in this.parameters.SpecificationPaths)
                {
                    if (this.parameters.Logger != null)
                    {
                        this.parameters.Logger.LogAsync("Loading specification file: " + specificationPath);
                    }
                    Json.JsonPathFinder jsonFinder = new Json.JsonPathFinder(File.ReadAllText(specificationPath));
                    if (this.parameters.Logger != null)
                    {
                        this.parameters.Logger.LogAsync("Parsing specification file: " + specificationPath);
                    }
                    this.currentModule.LoadMetadataFromSpecification(jsonFinder);
                }
            }

            this.currentModule.CompleteMetadataLoad();

            // For JSON-RPC pipe input/output, add Newtonsoft.Json converters
            Newtonsoft.Json.JsonSerializerSettings inputSerializerSettings = null;
            if (this.Input is JsonRpcPipe)
            {
                JsonRpcPipe jsonRpcPipe = (JsonRpcPipe)this.Input;
                inputSerializerSettings = jsonRpcPipe.JsonSerializerSettings;
                new LiveTestRequestConverter(this.currentModule).RegisterSelf(jsonRpcPipe.JsonSerializerSettings);
                if (this.parameters.Logger != null)
                {
                    this.parameters.Logger.JsonSerializerSettings = jsonRpcPipe.JsonSerializerSettings;
                }
            }

            if (this.Output is JsonRpcPipe)
            {
                JsonRpcPipe jsonRpcPipe = (JsonRpcPipe)this.Output;
                // Double check this is a different object than the input pipe, if any.
                if (inputSerializerSettings == null || inputSerializerSettings != jsonRpcPipe.JsonSerializerSettings)
                {
                    new LiveTestRequestConverter(this.currentModule).RegisterSelf(jsonRpcPipe.JsonSerializerSettings);
                    if (this.parameters.Logger != null)
                    {
                        this.parameters.Logger.JsonSerializerSettings = jsonRpcPipe.JsonSerializerSettings;
                    }
                }
            }

            this.IsRunning = true;

            // Start message read thread
            this.messageReadThread = new Thread(async() =>
            {
                while (this.IsRunning)
                {
                    try
                    {
                        // Block and wait for the next request
                        LiveTestRequest msg = await this.Input.ReadBlock <LiveTestRequest>();
                        if (this.IsRunning)
                        {
                            if (msg == null)
                            {
                                if (this.parameters.Logger != null)
                                {
                                    this.parameters.Logger.LogAsync("Input stream has been closed, stopping server.", msg);
                                }

                                this.IsRunning = false;
                            }
                            else
                            {
                                if (this.parameters.Logger != null)
                                {
                                    this.parameters.Logger.LogAsync("Processing message: {0}", msg);
                                }
                                Task.Run(() =>
                                {
                                    LiveTestResponse response    = null;
                                    IServiceTracer serviceTracer = null;
                                    try
                                    {
                                        // Enable service tracing so that we can get service layer information required by test protocol
                                        long invocationId = this.parameters.TracingManager.GetNextInvocationId();
                                        serviceTracer     = this.parameters.TracingManager.CreateTracer(invocationId, this.parameters.Logger);
                                        this.parameters.TracingManager.EnableTracing();
                                        // Process teh request
                                        CommandExecutionResult commandResult = this.currentModule.ProcessRequest(msg, this.parameters.CredentialFactory);
                                        if (commandResult == null)
                                        {
                                            if (this.parameters.Logger != null)
                                            {
                                                this.parameters.Logger.LogAsync("Command not found.");
                                            }

                                            response = msg.MakeResponse(null, MethodNotFound);
                                        }
                                        else
                                        {
                                            response = msg.MakeResponse(commandResult, serviceTracer, parameters.ObjectTransforms, this.parameters.Logger);
                                        }
                                    }
                                    catch (Exception exRequest)
                                    {
                                        if (this.parameters.Logger != null)
                                        {
                                            this.parameters.Logger.LogError("Exception processing request: " + exRequest.ToString());
                                        }

                                        response = msg.MakeResponse(exRequest, InternalError);
                                    }
                                    finally
                                    {
                                        if (response != null)
                                        {
                                            this.Output.WriteBlock(response);
                                        }

                                        if (serviceTracer != null)
                                        {
                                            this.parameters.TracingManager.RemoveTracer(serviceTracer);
                                        }
                                    }
                                });
                            }
                        }
                    }
                    catch (Exception eRead)
                    {
                        if (this.parameters.Logger != null)
                        {
                            this.parameters.Logger.LogError("Exception during test server message loop: " + eRead.ToString());
                        }

                        this.IsRunning = false;
                    }
                }
            })
            {
                IsBackground = true
            };
            this.messageReadThread.Start();
            if (this.parameters.Logger != null)
            {
                this.parameters.Logger.LogAsync("PowerShell live test server has started.");
            }
        }