예제 #1
0
        public async Task Request_ShouldCall_NextMiddleware_Test()
        {
            var httpContext = new DefaultHttpContext();

            httpContext.Request.Path = new PathString("/request");

            await _historyMiddleware.InvokeAsync(httpContext);

            A.CallTo(() => _historyRepository.Get()).MustNotHaveHappened();
            A.CallTo(() => _next(httpContext)).MustHaveHappened();
            A.CallTo(() => _historyRepository.Clear()).MustNotHaveHappened();
        }
예제 #2
0
        public async Task InvokeAsync(HttpContext httpContext)
        {
            if (httpContext.Request.Path == _mockerOptions.HistoryPath)
            {
                if (httpContext.Request.Method == HttpMethods.Get)
                {
                    httpContext.Response.StatusCode  = StatusCodes.Status200OK;
                    httpContext.Response.ContentType = "application/json";
                    var historyData = _historyRepository.Get();
                    var content     = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(historyData));
                    await httpContext.Response.Body.WriteAsync(content, httpContext.RequestAborted);
                }
                else if (httpContext.Request.Method == HttpMethods.Delete)
                {
                    _historyRepository.Clear();
                    httpContext.Response.StatusCode = StatusCodes.Status204NoContent;
                }
                else
                {
                    httpContext.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
                }
                return;
            }

            await _next(httpContext);
        }