Example #1
0
        public void RequestReceived(object sender, HttpContextArgs args)
        {
            if (!CheckHeaders(args.Context.Request))
            {
                throw new InvalidOperationException($"Request headers do not match for operation {httpMethod} {completeRoute}");
            }

            if (rawBodyProcessingCallback == null &&
                basicResponseBodyHandler == null)
            {
                throw new InvalidOperationException($"No body processing handler defined for operation {ToString()}");
            }

            IncreaseInvocations();

            if (rawBodyProcessingCallback != null)
            {
                var requestObject = DeserializeBody(args.Context.Request);
                var respoObject   = rawBodyProcessingCallback(requestObject);

                var operationResponse = new OperationResponse <TResp>()
                {
                    Body = respoObject
                };

                SendResponse(args.Context.Response, operationResponse);
            }

            if (basicResponseBodyHandler != null)
            {
                // var requestObject = DeserializeBody(args.Context.Request);
                var respoObject = basicResponseBodyHandler();

                var operationResponse = new OperationResponse <TResp>()
                {
                    Body = respoObject
                };

                SendResponse(args.Context.Response, operationResponse);
            }
        }
Example #2
0
        protected void OnRequestReceived(HttpContextArgs args)
        {
            //if (RequestReceived != null)
            //{
            //    RequestReceived(this, args);
            //}
            string operationKey = $"{args.Context.Request.HttpMethod}:{args.Context.Request.Url.PathAndQuery}"; // Same here - whether path only, or path and query, is more suitable
            IFluentOperationUnknown expectedOperation;

            // This will work well for concretely defined paths, but what about such with placeholders for parameters???
            while (!expectations.TryGetValue(operationKey, out expectedOperation))  // Have to add some code here
            {
            }

            if (expectedOperation == null)
            {
                throw new InvalidOperationException($"No expectation set up for operation {operationKey} !");
            }

            // TODO: Have to figure out a nicer way to do this
            var expectedOperationConfig = expectedOperation as IOperationRequestReceivedHandler;

            expectedOperationConfig.RequestReceived(this, args);
        }
Example #3
0
        private async void Run()
        {
            while (httpListener.IsListening)
            {
                try
                {
                    var httpContext = await httpListener.GetContextAsync();

                    if (httpContext != null)
                    {
                        var receivedRequest = new HttpContextArgs()
                        {
                            Context = httpContext
                        };

                        OnRequestReceived(receivedRequest);
                    }
                }
                catch (Exception ex)
                {
                    // Log the exception
                }
            }
        }