Exemple #1
0
        public void ResponseMessageIsLoggedForSuccessfulResponses()
        {
            // Arrange
            INLogger _logger        = Substitute.For <INLogger>();
            var      configSettings = new Settings {
                RequestCorrelationIdIsRequired = true
            };
            var pipelineHelper = TestSetup.SetupPipelineHelper(configSettings, logger: _logger);

            var responseBody = new JObject();

            responseBody.Add("Batman", "Begins");

            var response = new Nancy.Response()
            {
                StatusCode = Nancy.HttpStatusCode.OK,
                Contents   = stream =>
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(responseBody);
                    }
                }
            };

            // Act
            pipelineHelper.LogAndFormatResponse(response);

            // Assert
            _logger.Received(1).Trace(Arg.Any <string>(), Arg.Any <Exception>(), Arg.Any <Guid>());
            Assert.AreEqual(Nancy.HttpStatusCode.OK, response.StatusCode);
        }
Exemple #2
0
        public void ResponseMessageIsLoggedForResponsesContainingErrors()
        {
            // Arrange
            INLogger _logger        = Substitute.For <INLogger>();
            var      configSettings = new Settings {
                RequestCorrelationIdIsRequired = true
            };
            var pipelineHelper = TestSetup.SetupPipelineHelper(configSettings, logger: _logger);

            var responseBody = JsonSerializer.ToJson(new
            {
                Batman = "Begins",
                Errors = new List <object> {
                    new {
                        ErrorCode    = "ErrorCode",
                        ErrorMessage = "ErrorMessage"
                    }
                }
            });

            var response = new Nancy.Response()
            {
                StatusCode = Nancy.HttpStatusCode.OK,
                Contents   = stream =>
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(responseBody);
                    }
                }
            };

            // Act
            pipelineHelper.LogAndFormatResponse(response);

            // Assert
            _logger.Received(1).Trace(Arg.Any <string>(), Arg.Any <Exception>(), Arg.Any <Guid>());
            Assert.AreEqual(Nancy.HttpStatusCode.BadRequest, response.StatusCode);
        }
        public RhinoModule()
        {
            Get["/healthcheck"] = _ => "healthy";

            var endpoints = EndPointDictionary.GetDictionary();

            foreach (var kv in endpoints)
            {
                Get[kv.Key] = _ =>
                {
                    if (NancySelfHost.RunningHttps && !Request.Url.IsSecure)
                    {
                        string url = Request.Url.ToString().Replace("http", "https");
                        return(new Nancy.Responses.RedirectResponse(url, Nancy.Responses.RedirectResponse.RedirectType.Permanent));
                    }
                    Logger.WriteInfo($"GET {kv.Key}", null);
                    var response = kv.Value.HandleGetAsResponse();
                    if (response != null)
                    {
                        return(response);
                    }
                    return(kv.Value.HandleGet());
                };
                Post[kv.Key] = _ =>
                {
                    if (NancySelfHost.RunningHttps && !Request.Url.IsSecure)
                    {
                        return(Nancy.HttpStatusCode.HttpVersionNotSupported);
                    }

                    Logger.WriteInfo($"POST {kv.Key}", GetApiToken());
                    if (!string.IsNullOrWhiteSpace(kv.Key) && kv.Key.Length > 1)
                    {
                        var authCheck = CheckAuthorization();
                        if (authCheck != Nancy.HttpStatusCode.OK)
                        {
                            return(authCheck);
                        }
                    }
                    var jsonString = Request.Body.AsString();

                    // In order to enable CORS, we add the proper headers to the response
                    var resp = new Nancy.Response();
                    resp.Headers.Add("Access-Control-Allow-Origin", "*");
                    resp.Headers.Add("Access-Control-Allow-Methods", "POST,GET");
                    resp.Headers.Add("Access-Control-Allow-Headers", "Accept, Origin, Content-type");
                    resp.Contents = (e) =>
                    {
                        using (var sw = new System.IO.StreamWriter(e))
                        {
                            bool multiple = false;
                            System.Collections.Generic.Dictionary <string, string> returnModifiers = null;
                            foreach (string name in Request.Query)
                            {
                                if (name.StartsWith("return.", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    if (returnModifiers == null)
                                    {
                                        returnModifiers = new System.Collections.Generic.Dictionary <string, string>();
                                    }
                                    string dataType = "Rhino.Geometry." + name.Substring("return.".Length);
                                    string items    = Request.Query[name];
                                    returnModifiers[dataType] = items;
                                    continue;
                                }
                                if (name.Equals("multiple", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    multiple = Request.Query[name];
                                }
                            }
                            var postResult = kv.Value.HandlePost(jsonString, multiple, returnModifiers);
                            sw.Write(postResult);
                            sw.Flush();
                        }
                    };
                    return(resp);
                };
            }
        }