public async Task <ActionResult <Server> > AddServer(Server server)
        {
            var result = new Microsoft.AspNetCore.Mvc.ContentResult();
            await _context.Servers.AddAsync(server);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ServerExists(server.ServerId))
                {
                    result.StatusCode = 400;
                    result.Content    = "server allready exist\n";
                }
                else
                {
                    result.StatusCode = 500;
                }
                return(result);
            }
            result.StatusCode = 201;
            return(result);
        }
예제 #2
0
        public async Task ContentResult_DisablesResponseBuffering_IfBufferingFeatureAvailable()
        {
            // Arrange
            var data = "Test Content";
            var contentResult = new ContentResult
            {
                Content = data,
                ContentType = new MediaTypeHeaderValue("text/plain")
                {
                    Encoding = Encoding.ASCII
                }.ToString()
            };
            var httpContext = GetHttpContext();
            httpContext.Features.Set<IHttpBufferingFeature>(new TestBufferingFeature());
            var memoryStream = new MemoryStream();
            httpContext.Response.Body = memoryStream;
            var actionContext = GetActionContext(httpContext);

            // Act
            await contentResult.ExecuteResultAsync(actionContext);

            // Assert
            Assert.Equal("text/plain; charset=us-ascii", httpContext.Response.ContentType);
            Assert.Equal(Encoding.ASCII.GetString(memoryStream.ToArray()), data);
            var bufferingFeature = (TestBufferingFeature)httpContext.Features.Get<IHttpBufferingFeature>();
            Assert.True(bufferingFeature.DisableResponseBufferingInvoked);
        }
        public async Task <ActionResult <FlightPlanDto> > GetFlightPlan(string id)
        {
            if (FlightPlanExists(id))
            {
                return(await GetInternalFlightPlan(id));
            }
            var result = new Microsoft.AspNetCore.Mvc.ContentResult();

            try
            {
                //find the server which the requested flight plan came from
                var flightServerMap = await _context.Map.FindAsync(id);

                if (flightServerMap != null)
                {
                    return(await GetExternalFlightPlan(id, result, flightServerMap));
                }


                result.StatusCode = 400;
                result.Content    = "the flight you request does not exist in any internal nor external air control server\n";
                return(result);
            }
            catch {
                result.StatusCode = 500;
                return(result);
            }
        }
 public IActionResult Contact()
 {
     var lolPage = Cache.Pages.First(p => p.Category.Matches("Resume") && p.URL.Matches("LOL"));
     var niceTry = new ContentResult
     {
         ContentType = "text/plain",
         Content = lolPage.Body
     };
     return niceTry;
 }
예제 #5
0
        public async Task <IActionResult> OnGetFilterEntityAsync(string filterString)
        {
            if (filterString == null)
            {
                filterString = "";
            }
            string response = await DS.GetAsync("entity/FilterEntities/" + filterString);

            List <Entity> entities = JsonConvert.DeserializeObject <List <Entity> >(response);

            Microsoft.AspNetCore.Mvc.ContentResult t = new Microsoft.AspNetCore.Mvc.ContentResult();
            t.Content     = response;
            t.ContentType = "text/html";
            return(t);
        }
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //before:(before go to the controller) request tới middleware, và stuff mà bạn có thể làm trước khi nó bước sang middle tiếp theo
            //check if request is cached
            var cacheSettings = context.HttpContext.RequestServices.GetRequiredService <RedisCacheSettings>();

            //nếu cache không được kích hoạt thì vào action và k làm gì cả
            if (!cacheSettings.Enable)
            {
                await next();

                return;
            }


            var cacheService = context.HttpContext.RequestServices.GetRequiredService <IResponseCacheService>();

            //tạo cacheKey từ request
            var cacheKey = GenerateCacheKeyFromRequest(context.HttpContext.Request);

            //lấy cacheResponse(cacheValue từ cacheKey)
            var cachedReponse = await cacheService.GetCachedReponseAsync(cacheKey);

            if (!string.IsNullOrEmpty(cachedReponse))
            {
                var contentResult = new Microsoft.AspNetCore.Mvc.ContentResult
                {
                    Content     = cachedReponse,
                    ContentType = "application/json",
                    StatusCode  = 200
                };
                context.Result = contentResult;
                return;
            }

            //nếu key này chưa đc cache thì mình sẽ thực hiện action trước sau đó lấy response để tạo ra 1 cache
            var executedContext = await next();

            if (executedContext.Result is OkObjectResult okObjectResult)
            {
                await cacheService.CacheResponseAsync(cacheKey, okObjectResult.Value, TimeSpan.FromSeconds(_timeToLiveSeconds));
            }
            ;

            //after:(after go to the controller) response tới
            //get the calue
            //cache the respose
        }
예제 #7
0
        public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding()
        {
            // Arrange
            var contentResult = new ContentResult
            {
                Content = null,
                ContentType = new MediaTypeHeaderValue("text/plain")
                {
                    Encoding = Encoding.UTF7
                }.ToString()
            };
            var httpContext = GetHttpContext();
            var actionContext = GetActionContext(httpContext);

            // Act
            await contentResult.ExecuteResultAsync(actionContext);

            // Assert
            MediaTypeAssert.Equal("text/plain; charset=utf-7", httpContext.Response.ContentType);
        }
예제 #8
0
        public async Task ContentResult_Response_NullContent_SetsContentTypeAndEncoding()
        {
            // Arrange
            var contentResult = new ContentResult
            {
                Content     = null,
                ContentType = new MediaTypeHeaderValue("text/plain")
                {
                    Encoding = Encoding.UTF7
                }.ToString()
            };
            var httpContext   = GetHttpContext();
            var actionContext = GetActionContext(httpContext);

            // Act
            await contentResult.ExecuteResultAsync(actionContext);

            // Assert
            MediaTypeAssert.Equal("text/plain; charset=utf-7", httpContext.Response.ContentType);
        }
        public async Task <ActionResult <Server> > DeleteServer(string id)
        {
            var result = new Microsoft.AspNetCore.Mvc.ContentResult();

            if (!ServerExists(id))
            {
                result.StatusCode = 400;
                result.Content    = "server does not exist";
                return(result);
            }
            var server = _context.Servers.Find(id);

            try {
                _context.Servers.Remove(server);
                await _context.SaveChangesAsync();
            }
            catch
            {
                result.StatusCode = 500;
            }
            result.StatusCode = 204;
            return(result);
        }
예제 #10
0
        public async Task ContentResult_ExecuteAsync_WritesDataCorrectly_ForDifferentContentSizes(string content, string contentType)
        {
            // Arrange
            var contentResult = new ContentResult
            {
                Content     = content,
                ContentType = contentType
            };
            var httpContext  = GetHttpContext();
            var memoryStream = new MemoryStream();

            httpContext.Response.Body = memoryStream;
            var encoding = MediaTypeHeaderValue.Parse(contentType).Encoding;

            // Act
            await((IResult)contentResult).ExecuteAsync(httpContext);

            // Assert
            memoryStream.Seek(0, SeekOrigin.Begin);
            var streamReader  = new StreamReader(memoryStream, encoding);
            var actualContent = await streamReader.ReadToEndAsync();

            Assert.Equal(content, actualContent);
        }
예제 #11
0
        public async Task ContentResult_ExecuteResultAsync_SetContentTypeAndEncoding_OnResponse(
            MediaTypeHeaderValue contentType,
            string content,
            string responseContentType,
            string expectedContentType,
            byte[] expectedContentData)
        {
            // Arrange
            var contentResult = new ContentResult
            {
                Content = content,
                ContentType = contentType?.ToString()
            };
            var httpContext = GetHttpContext();
            var memoryStream = new MemoryStream();
            httpContext.Response.Body = memoryStream;
            httpContext.Response.ContentType = responseContentType;
            var actionContext = GetActionContext(httpContext);

            // Act
            await contentResult.ExecuteResultAsync(actionContext);

            // Assert
            var finalResponseContentType = httpContext.Response.ContentType;
            Assert.Equal(expectedContentType, finalResponseContentType);
            Assert.Equal(expectedContentData, memoryStream.ToArray());
        }