|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.Http; |
| 9 | +using Microsoft.AspNetCore.Mvc; |
| 10 | +using Microsoft.Azure.WebJobs.Script.Binding; |
| 11 | +using Microsoft.Extensions.Primitives; |
| 12 | +using Microsoft.WebJobs.Script.Tests; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Binding.ActionResults |
| 16 | +{ |
| 17 | + public class RawScriptResultTests |
| 18 | + { |
| 19 | + [Fact] |
| 20 | + public void HasExpectedProperties_WithStatusCode() |
| 21 | + { |
| 22 | + var obj = "hello world"; |
| 23 | + var result = new RawScriptResult(202, obj) { Headers = new Dictionary<string, object>() }; |
| 24 | + |
| 25 | + Assert.Equal(202, result.StatusCode); |
| 26 | + Assert.Equal(obj, result.Content); |
| 27 | + Assert.Empty(result.Headers); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void HasExpectedProperties_WithoutStatusCode() |
| 32 | + { |
| 33 | + var obj = "hello world"; |
| 34 | + var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() }; |
| 35 | + |
| 36 | + Assert.Null(result.StatusCode); |
| 37 | + Assert.Equal(obj, result.Content); |
| 38 | + Assert.Empty(result.Headers); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public async Task HandlesStringContent() |
| 43 | + { |
| 44 | + var obj = "{ \"a\": 1 }"; |
| 45 | + var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() }; |
| 46 | + var context = new ActionContext() { HttpContext = new DefaultHttpContext() }; |
| 47 | + context.HttpContext.Response.Body = new MemoryStream(); |
| 48 | + await result.ExecuteResultAsync(context); |
| 49 | + var body = await TestHelpers.ReadStreamToEnd(context.HttpContext.Response.Body); |
| 50 | + Assert.Equal(obj, body); |
| 51 | + Assert.Equal(200, context.HttpContext.Response.StatusCode); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task HandlesStringContent_WithHeader() |
| 56 | + { |
| 57 | + var obj = "{ \"a\": 1 }"; |
| 58 | + var contentHeader = "application/json"; |
| 59 | + var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() }; |
| 60 | + var context = new ActionContext() { HttpContext = new DefaultHttpContext() }; |
| 61 | + context.HttpContext.Response.Headers.Add("Content-Type", contentHeader); |
| 62 | + context.HttpContext.Response.Body = new MemoryStream(); |
| 63 | + await result.ExecuteResultAsync(context); |
| 64 | + var body = await TestHelpers.ReadStreamToEnd(context.HttpContext.Response.Body); |
| 65 | + StringValues value; |
| 66 | + Assert.True(context.HttpContext.Response.Headers.TryGetValue("Content-Type", out value)); |
| 67 | + Assert.Equal("application/json; charset=utf-8", value); |
| 68 | + Assert.Equal(obj, body); |
| 69 | + Assert.Equal(200, context.HttpContext.Response.StatusCode); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public async Task HandlesByteContent() |
| 74 | + { |
| 75 | + var content = "{ \"a\": 1 }"; |
| 76 | + var obj = Encoding.UTF8.GetBytes(content); |
| 77 | + var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() }; |
| 78 | + var context = new ActionContext() { HttpContext = new DefaultHttpContext() }; |
| 79 | + context.HttpContext.Response.Body = new MemoryStream(); |
| 80 | + await result.ExecuteResultAsync(context); |
| 81 | + var body = await TestHelpers.ReadStreamToEnd(context.HttpContext.Response.Body); |
| 82 | + Assert.Equal(content, body); |
| 83 | + Assert.Equal(200, context.HttpContext.Response.StatusCode); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public async Task HandlesStreamContent() |
| 88 | + { |
| 89 | + var content = "{ \"a\": 1 }"; |
| 90 | + var obj = new MemoryStream(Encoding.UTF8.GetBytes(content)); |
| 91 | + var result = new RawScriptResult(null, obj) { Headers = new Dictionary<string, object>() }; |
| 92 | + var context = new ActionContext() { HttpContext = new DefaultHttpContext() }; |
| 93 | + context.HttpContext.Response.Body = new MemoryStream(); |
| 94 | + await result.ExecuteResultAsync(context); |
| 95 | + var body = await TestHelpers.ReadStreamToEnd(context.HttpContext.Response.Body); |
| 96 | + Assert.Equal(content, body); |
| 97 | + Assert.Equal(200, context.HttpContext.Response.StatusCode); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments