|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +using System.Collections.Specialized; |
| 4 | +using System.Net.Http.Formatting; |
| 5 | +using System.Net.Http.Headers; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.TestCommon; |
| 9 | +using Xunit; |
| 10 | +using Xunit.Extensions; |
| 11 | +using Assert = Microsoft.TestCommon.AssertEx; |
| 12 | + |
| 13 | +namespace System.Net.Http |
| 14 | +{ |
| 15 | + public class HttpContentFormDataExtensionsTest |
| 16 | + { |
| 17 | + public static TheoryDataSet<string> FormDataContentTypes |
| 18 | + { |
| 19 | + get |
| 20 | + { |
| 21 | + return new TheoryDataSet<string> |
| 22 | + { |
| 23 | + "application/x-www-form-urlencoded", |
| 24 | + "APPLICATION/x-www-form-urlencoded", |
| 25 | + "application/X-WWW-FORM-URLENCODED", |
| 26 | + "application/x-www-form-urlencoded; charset=utf-8", |
| 27 | + "application/x-www-form-urlencoded; parameter=value", |
| 28 | + }; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static TheoryDataSet<string> NonFormDataContentTypes |
| 33 | + { |
| 34 | + get |
| 35 | + { |
| 36 | + return new TheoryDataSet<string> |
| 37 | + { |
| 38 | + "application/xml", |
| 39 | + "APPLICATION/json", |
| 40 | + "text/xml", |
| 41 | + "text/xml; charset=utf-8", |
| 42 | + "text/xml; parameter=value", |
| 43 | + }; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public static TheoryDataSet<string> FormData |
| 48 | + { |
| 49 | + get |
| 50 | + { |
| 51 | + return new TheoryDataSet<string> |
| 52 | + { |
| 53 | + "a=b", |
| 54 | + "a+c=d+e", |
| 55 | + "n1=v1&n2=v2", |
| 56 | + "n1=v1a+v1b&n2=v2a+v2b", |
| 57 | + "N=%c3%a6%c3%b8%c3%a5", |
| 58 | + }; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static TheoryDataSet<string> IrregularFormData |
| 63 | + { |
| 64 | + get |
| 65 | + { |
| 66 | + return new TheoryDataSet<string> |
| 67 | + { |
| 68 | + "?data", |
| 69 | + Char.ConvertFromUtf32(0x0D), |
| 70 | + "Hello World", |
| 71 | + "<string>Hello World</string>", |
| 72 | + "{ \"Message\" : \"Hello World\"", |
| 73 | + }; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void IsFormData_ThrowsOnNull() |
| 79 | + { |
| 80 | + Assert.ThrowsArgumentNull(() => HttpContentFormDataExtensions.IsFormData(null), "content"); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public void IsFormData_HandlesNullContentType() |
| 85 | + { |
| 86 | + HttpContent content = new StringContent(String.Empty); |
| 87 | + content.Headers.ContentType = null; |
| 88 | + Assert.False(content.IsFormData()); |
| 89 | + } |
| 90 | + |
| 91 | + [Theory] |
| 92 | + [PropertyData("FormDataContentTypes")] |
| 93 | + public void IsFormData_AcceptsFormDataMediaTypes(string mediaType) |
| 94 | + { |
| 95 | + HttpContent content = new StringContent(String.Empty); |
| 96 | + content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType); |
| 97 | + Assert.True(content.IsFormData()); |
| 98 | + } |
| 99 | + |
| 100 | + [Theory] |
| 101 | + [PropertyData("NonFormDataContentTypes")] |
| 102 | + public void IsFormData_RejectsNonFormDataMediaTypes(string mediaType) |
| 103 | + { |
| 104 | + HttpContent content = new StringContent(String.Empty); |
| 105 | + content.Headers.ContentType = MediaTypeHeaderValue.Parse(mediaType); |
| 106 | + Assert.False(content.IsFormData()); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public void ReadAsFormDataAsync_ThrowsOnNull() |
| 111 | + { |
| 112 | + Assert.ThrowsArgumentNull(() => HttpContentFormDataExtensions.ReadAsFormDataAsync(null), "content"); |
| 113 | + } |
| 114 | + |
| 115 | + [Theory] |
| 116 | + [PropertyData("FormData")] |
| 117 | + public Task ReadAsFormDataAsync_HandlesFormData(string formData) |
| 118 | + { |
| 119 | + // Arrange |
| 120 | + HttpContent content = new StringContent(formData); |
| 121 | + content.Headers.ContentType = MediaTypeConstants.ApplicationFormUrlEncodedMediaType; |
| 122 | + |
| 123 | + // Act |
| 124 | + return content.ReadAsFormDataAsync().ContinueWith( |
| 125 | + readTask => |
| 126 | + { |
| 127 | + NameValueCollection data = readTask.Result; |
| 128 | + |
| 129 | + // Assert |
| 130 | + Assert.Equal(TaskStatus.RanToCompletion, readTask.Status); |
| 131 | + Assert.Equal(formData, data.ToString()); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + [Theory] |
| 136 | + [PropertyData("IrregularFormData")] |
| 137 | + public Task ReadAsFormDataAsync_HandlesIrregularFormData(string irregularFormData) |
| 138 | + { |
| 139 | + // Arrange |
| 140 | + HttpContent content = new StringContent(irregularFormData); |
| 141 | + content.Headers.ContentType = MediaTypeConstants.ApplicationFormUrlEncodedMediaType; |
| 142 | + |
| 143 | + // Act |
| 144 | + return content.ReadAsFormDataAsync().ContinueWith( |
| 145 | + readTask => |
| 146 | + { |
| 147 | + NameValueCollection data = readTask.Result; |
| 148 | + |
| 149 | + // Assert |
| 150 | + Assert.Equal(TaskStatus.RanToCompletion, readTask.Status); |
| 151 | + Assert.Equal(1, data.Count); |
| 152 | + Assert.Equal(irregularFormData, data.AllKeys[0]); |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + [Fact] |
| 157 | + public void ReadAsFormDataAsync_HandlesNonFormData() |
| 158 | + { |
| 159 | + HttpContent content = new StringContent(String.Empty, Encoding.UTF8, "test/unknown"); |
| 160 | + Assert.Throws<InvalidOperationException>(() => content.ReadAsFormDataAsync()); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments