forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatterParameterBindingTest.cs
More file actions
122 lines (106 loc) · 5.63 KB
/
Copy pathFormatterParameterBindingTest.cs
File metadata and controls
122 lines (106 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Metadata;
using System.Web.Http.Validation;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ModelBinding
{
public class FormatterParameterBindingTest
{
[Fact]
public void ReadContentAsync_Throws_ForNoContentType()
{
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost");
request.Content = new StringContent("The quick, brown fox tripped and fell.");
request.Content.Headers.ContentType = null;
var formatters = new MediaTypeFormatterCollection();
var descriptor = new Mock<HttpParameterDescriptor>();
descriptor.Setup(desc => desc.IsOptional).Returns(false);
var binding = new FormatterParameterBinding(descriptor.Object, formatters, null);
HttpResponseException exception = Assert.Throws<HttpResponseException>(
() => binding.ReadContentAsync(request, typeof(string), formatters, null));
Assert.Equal(HttpStatusCode.UnsupportedMediaType, exception.Response.StatusCode);
HttpError error;
exception.Response.TryGetContentValue(out error);
Assert.Equal(
"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.",
error.Message);
}
[Fact]
public void ReadContentAsync_Throws_ForUnsupportedMediaType()
{
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost");
request.Content = new StringContent("The quick, brown fox tripped and fell.");
var formatters = new MediaTypeFormatterCollection();
var descriptor = new Mock<HttpParameterDescriptor>();
descriptor.Setup(desc => desc.IsOptional).Returns(false);
var binding = new FormatterParameterBinding(descriptor.Object, formatters, null);
HttpResponseException exception = Assert.Throws<HttpResponseException>(
() => binding.ReadContentAsync(request, typeof(string), formatters, null));
Assert.Equal(HttpStatusCode.UnsupportedMediaType, exception.Response.StatusCode);
HttpError error;
exception.Response.TryGetContentValue(out error);
Assert.Equal(
"The request entity's media type 'text/plain' is not supported for this resource.",
error.Message);
}
[Fact]
public async Task ExecuteBindingAsync_PassesCancellationTokenTo_ReadContentAsync()
{
// Arrange
var parameter = new Mock<HttpParameterDescriptor>();
parameter.Setup(p => p.IsOptional).Returns(false);
parameter.Setup(p => p.ParameterName).Returns("ParameterName");
var formatters = Enumerable.Empty<MediaTypeFormatter>();
IBodyModelValidator validator = null;
ModelMetadataProvider metadataProvider = new Mock<ModelMetadataProvider>().Object;
HttpRequestMessage request = new HttpRequestMessage();
HttpActionContext actionContext = new HttpActionContext { ControllerContext = new HttpControllerContext { Request = request } };
CancellationTokenSource cts = new CancellationTokenSource();
Mock<FormatterParameterBinding> binding = new Mock<FormatterParameterBinding>(parameter.Object, formatters, validator);
binding.CallBase = true;
binding.Setup(b => b.ReadContentAsync(request, null, formatters, It.IsAny<IFormatterLogger>()))
.Returns(Task.FromResult<object>(42))
.Verifiable();
// Act
await binding.Object.ExecuteBindingAsync(metadataProvider, actionContext, cts.Token);
// Assert
binding.Verify();
}
[Fact]
public async Task ReadContentAsync_PassesCancellationToken_Further()
{
// Arrange
var parameter = new Mock<HttpParameterDescriptor>();
parameter.Setup(p => p.IsOptional).Returns(false);
IBodyModelValidator validator = null;
HttpRequestMessage request = new HttpRequestMessage();
request.Content = new StringContent("");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("app/test");
IFormatterLogger logger = null;
CancellationTokenSource cts = new CancellationTokenSource();
Mock<MediaTypeFormatter> formatter = new Mock<MediaTypeFormatter>();
formatter.Setup(f => f.CanReadType(typeof(int))).Returns(true);
formatter.Object.SupportedMediaTypes.Add(request.Content.Headers.ContentType);
formatter.Setup(f => f.ReadFromStreamAsync(typeof(int), It.IsAny<Stream>(), request.Content, logger, cts.Token))
.Returns(Task.FromResult<object>(42))
.Verifiable();
var formatters = new[] { formatter.Object };
FormatterParameterBinding binding = new FormatterParameterBinding(parameter.Object, formatters, validator);
// Act
await binding.ReadContentAsync(request, typeof(int), formatters, logger, cts.Token);
// Assert
formatter.Verify();
}
}
}