protected override async Task WriteResponse(MazeResponse response)
 {
     foreach (var package in _packages)
     {
         await response.Body.WriteAsync(package, 0, package.Length);
     }
 }
Пример #2
0
        protected override async Task WriteResponse(MazeResponse response)
        {
            response.StatusCode = 303;

            var testResponse = Encoding.UTF8.GetBytes("Hello Universe!");
            await response.Body.WriteAsync(testResponse, 0, testResponse.Length);
        }
Пример #3
0
 public MazeRequestReceivedEventArgs(MazeRequest request, MazeResponse response,
                                     CancellationToken cancellationToken)
 {
     Request           = request;
     Response          = response;
     CancellationToken = cancellationToken;
 }
Пример #4
0
            protected override async Task AssertReceivedResponse(MazeResponse response, HttpResponseMessage responseMessage)
            {
                var expected = Merge(_packages.Select(x => new ArraySegment <byte>(x)).ToList());

                byte[] actual;

                if (responseMessage.Content.Headers.ContentEncoding.Contains("gzip"))
                {
                    using (var result = new MemoryStream())
                    {
                        using (var gzipStream = new GZipStream(await responseMessage.Content.ReadAsStreamAsync(), CompressionMode.Decompress))
                        {
                            await gzipStream.CopyToAsync(result);
                        }

                        actual = result.ToArray();
                    }
                }
                else
                {
                    using (var result = new MemoryStream())
                    {
                        await responseMessage.Content.CopyToAsync(result);

                        actual = result.ToArray();
                    }
                }

                Assert.Equal(expected.Length, actual.Length);
                Assert.Equal(expected, actual);
            }
Пример #5
0
        private static void EncodeResponse(MazeResponse response, TextWriter textWriter)
        {
            textWriter.WriteLine(response.StatusCode);

            EncodeHeaders(response.Headers, textWriter);

            textWriter.WriteLine(); //finish
        }
Пример #6
0
        public static int FormatResponse(MazeResponse mazeResponse, ArraySegment <byte> buffer)
        {
            var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count, true);

            using (var streamWriter = new StreamWriter(memoryStream, Encoding, 8192, true))
                EncodeResponse(mazeResponse, streamWriter);

            return((int)memoryStream.Position);
        }
Пример #7
0
        private static long SetContentLength(MazeResponse response, RangeItemHeaderValue range)
        {
            var start  = range.From.Value;
            var end    = range.To.Value;
            var length = end - start + 1;

            response.ContentLength = length;
            return(length);
        }
            protected override async Task AssertReceivedResponse(MazeResponse response, HttpResponseMessage responseMessage)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await responseMessage.Content.CopyToAsync(memoryStream);

                    var expected = Merge(_packages.Select(x => new ArraySegment <byte>(x)).ToList());
                    var actual   = memoryStream.ToArray();
                    Assert.Equal(expected, actual);
                }
            }
Пример #9
0
        public static HttpResponseMessage ToHttpResponseMessage(this MazeResponse response)
        {
            var message =
                new HttpResponseMessage((HttpStatusCode)response.StatusCode)
            {
                Content = new RawStreamContent(response.Body)
            };

            response.Headers.CopyHeadersTo(message.Headers, message.Content.Headers);

            return(message);
        }
Пример #10
0
        public DefaultMazeContext(MazeRequest request, MazeResponse response, IServiceProvider serviceProvider)
        {
            Request  = request;
            Response = response;

            Request.Context = this;

            RequestServices = serviceProvider;

            _requestCancellationTokenSource = new CancellationTokenSource();
            RequestAborted = _requestCancellationTokenSource.Token;
        }
Пример #11
0
        private static void SetLastModifiedAndEtagHeaders(MazeResponse response, DateTimeOffset?lastModified, EntityTagHeaderValue etag)
        {
            var httpResponseHeaders = response.GetTypedHeaders();

            if (lastModified.HasValue)
            {
                httpResponseHeaders.LastModified = lastModified;
            }
            if (etag != null)
            {
                httpResponseHeaders.ETag = etag;
            }
        }
Пример #12
0
        protected override async Task AssertReceivedResponse(MazeResponse response, HttpResponseMessage responseMessage)
        {
            Assert.Contains(responseMessage.Content.Headers.ContentEncoding, s => s == "gzip");

            using (var receiveStream = new MemoryStream())
            {
                await responseMessage.Content.CopyToAsync(receiveStream);

                receiveStream.Position = 0;

                using (var result = new MemoryStream())
                {
                    using (var gzipStream = new GZipStream(receiveStream, CompressionMode.Decompress))
                    {
                        await gzipStream.CopyToAsync(result);
                    }

                    var actual = result.ToArray();
                    Assert.Equal(_testData.Length, actual.Length);
                    Assert.Equal(_testData, actual);
                }
            }
        }
 public static ResponseHeaders GetTypedHeaders(this MazeResponse request)
 {
     return(new ResponseHeaders(request.Headers));
 }
Пример #14
0
        protected virtual Task AssertReceivedResponse(MazeResponse response, HttpResponseMessage responseMessage)
        {
            Assert.Equal(response.StatusCode, (int)responseMessage.StatusCode);

            return(Task.CompletedTask);
        }
Пример #15
0
 private static void SetAcceptRangeHeader(MazeResponse response)
 {
     response.Headers[HeaderNames.AcceptRanges] = AcceptRangeHeaderValue;
 }
Пример #16
0
 protected override Task WriteResponse(MazeResponse response)
 {
     response.StatusCode = StatusCodes.Status404NotFound;
     return(Task.CompletedTask);
 }
Пример #17
0
 protected abstract Task WriteResponse(MazeResponse response);
Пример #18
0
        protected override async Task AssertReceivedResponse(MazeResponse response, HttpResponseMessage responseMessage)
        {
            await base.AssertReceivedResponse(response, responseMessage);

            Assert.Equal("Hello Universe!", await responseMessage.Content.ReadAsStringAsync());
        }
Пример #19
0
 protected override Task WriteResponse(MazeResponse response)
 {
     return(response.Body.WriteAsync(_testData, 0, _testData.Length));
 }
Пример #20
0
 /// <summary>
 ///     Registers an object for disposal by the host once the request has finished processing.
 /// </summary>
 /// <param name="response">The <see cref="MazeResponse" /></param>
 /// <param name="disposable">The object to be disposed.</param>
 public static void RegisterForDispose(this MazeResponse response, IDisposable disposable)
 {
     response.OnCompleted(DisposeDelegate, disposable);
 }
Пример #21
0
 /// <summary>
 ///     Adds a delegate to be invoked just before response headers will be sent to the client.
 /// </summary>
 /// <param name="response">The <see cref="MazeResponse" /></param>
 /// <param name="callback">The delegate to execute.</param>
 public static void OnStarting(this MazeResponse response, Func <Task> callback)
 {
     response.OnStarting(CallbackDelegate, callback);
 }