1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. 2 3 using System.Net.Http.Formatting; 4 using System.Net.Http.Formatting.Mocks; 5 using System.Net.Http.Mocks; 6 using System.Threading; 7 using System.Threading.Tasks; 8 using Moq; 9 using Xunit; 10 using Assert = Microsoft.TestCommon.AssertEx; 11 12 namespace System.Net.Http 13 { 14 public class HttpClientExtensionsTest 15 { 16 private readonly MediaTypeFormatter _formatter = new MockMediaTypeFormatter { CallBase = true }; 17 private readonly HttpClient _client; 18 HttpClientExtensionsTest()19 public HttpClientExtensionsTest() 20 { 21 Mock<TestableHttpMessageHandler> handlerMock = new Mock<TestableHttpMessageHandler> { CallBase = true }; 22 handlerMock 23 .Setup(h => h.SendAsyncPublic(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>())) 24 .Returns((HttpRequestMessage request, CancellationToken _) => TaskHelpers.FromResult(new HttpResponseMessage() { RequestMessage = request })); 25 26 _client = new HttpClient(handlerMock.Object); 27 } 28 29 [Fact] PostAsJsonAsync_WhenClientIsNull_ThrowsException()30 public void PostAsJsonAsync_WhenClientIsNull_ThrowsException() 31 { 32 HttpClient client = null; 33 34 Assert.ThrowsArgumentNull(() => client.PostAsJsonAsync("http://www.example.com", new object()), "client"); 35 } 36 37 [Fact] PostAsJsonAsync_WhenUriIsNull_ThrowsException()38 public void PostAsJsonAsync_WhenUriIsNull_ThrowsException() 39 { 40 Assert.Throws<InvalidOperationException>(() => _client.PostAsJsonAsync(null, new object()), 41 "An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set."); 42 } 43 44 [Fact] PostAsJsonAsync_UsesJsonMediaTypeFormatter()45 public void PostAsJsonAsync_UsesJsonMediaTypeFormatter() 46 { 47 var result = _client.PostAsJsonAsync("http://example.com", new object()); 48 49 var response = result.Result; 50 var content = Assert.IsType<ObjectContent<object>>(response.RequestMessage.Content); 51 Assert.IsType<JsonMediaTypeFormatter>(content.Formatter); 52 } 53 54 [Fact] PostAsXmlAsync_WhenClientIsNull_ThrowsException()55 public void PostAsXmlAsync_WhenClientIsNull_ThrowsException() 56 { 57 HttpClient client = null; 58 59 Assert.ThrowsArgumentNull(() => client.PostAsXmlAsync("http://www.example.com", new object()), "client"); 60 } 61 62 [Fact] PostAsXmlAsync_WhenUriIsNull_ThrowsException()63 public void PostAsXmlAsync_WhenUriIsNull_ThrowsException() 64 { 65 Assert.Throws<InvalidOperationException>(() => _client.PostAsXmlAsync(null, new object()), 66 "An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set."); 67 } 68 69 [Fact] PostAsXmlAsync_UsesXmlMediaTypeFormatter()70 public void PostAsXmlAsync_UsesXmlMediaTypeFormatter() 71 { 72 var result = _client.PostAsXmlAsync("http://example.com", new object()); 73 74 var response = result.Result; 75 var content = Assert.IsType<ObjectContent<object>>(response.RequestMessage.Content); 76 Assert.IsType<XmlMediaTypeFormatter>(content.Formatter); 77 } 78 79 [Fact] PostAsync_WhenClientIsNull_ThrowsException()80 public void PostAsync_WhenClientIsNull_ThrowsException() 81 { 82 HttpClient client = null; 83 84 Assert.ThrowsArgumentNull(() => client.PostAsync("http://www.example.com", new object(), new JsonMediaTypeFormatter(), "text/json"), "client"); 85 } 86 87 [Fact] PostAsync_WhenRequestUriIsNull_ThrowsException()88 public void PostAsync_WhenRequestUriIsNull_ThrowsException() 89 { 90 Assert.Throws<InvalidOperationException>(() => _client.PostAsync(null, new object(), new JsonMediaTypeFormatter(), "text/json"), 91 "An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set."); 92 } 93 94 [Fact] PostAsync_WhenRequestUriIsSet_CreatesRequestWithAppropriateUri()95 public void PostAsync_WhenRequestUriIsSet_CreatesRequestWithAppropriateUri() 96 { 97 _client.BaseAddress = new Uri("http://example.com/"); 98 99 var result = _client.PostAsync("myapi/", new object(), _formatter); 100 101 var request = result.Result.RequestMessage; 102 Assert.Equal("http://example.com/myapi/", request.RequestUri.ToString()); 103 } 104 105 [Fact] PostAsync_WhenAuthoritativeMediaTypeIsSet_CreatesRequestWithAppropriateContentType()106 public void PostAsync_WhenAuthoritativeMediaTypeIsSet_CreatesRequestWithAppropriateContentType() 107 { 108 var result = _client.PostAsync("http://example.com/myapi/", new object(), _formatter, "foo/bar; charset=utf-16"); 109 110 var request = result.Result.RequestMessage; 111 Assert.Equal("foo/bar", request.Content.Headers.ContentType.MediaType); 112 Assert.Equal("utf-16", request.Content.Headers.ContentType.CharSet); 113 } 114 115 [Fact] PostAsync_WhenFormatterIsSet_CreatesRequestWithObjectContentAndCorrectFormatter()116 public void PostAsync_WhenFormatterIsSet_CreatesRequestWithObjectContentAndCorrectFormatter() 117 { 118 var result = _client.PostAsync("http://example.com/myapi/", new object(), _formatter, "foo/bar; charset=utf-16"); 119 120 var request = result.Result.RequestMessage; 121 var content = Assert.IsType<ObjectContent<object>>(request.Content); 122 Assert.Same(_formatter, content.Formatter); 123 } 124 125 [Fact] PostAsync_IssuesPostRequest()126 public void PostAsync_IssuesPostRequest() 127 { 128 var result = _client.PostAsync("http://example.com/myapi/", new object(), _formatter); 129 130 var request = result.Result.RequestMessage; 131 Assert.Same(HttpMethod.Post, request.Method); 132 } 133 134 [Fact] PostAsync_WhenMediaTypeFormatterIsNull_ThrowsException()135 public void PostAsync_WhenMediaTypeFormatterIsNull_ThrowsException() 136 { 137 Assert.ThrowsArgumentNull(() => _client.PostAsync("http;//example.com", new object(), formatter: null), "formatter"); 138 } 139 140 [Fact] PutAsJsonAsync_WhenClientIsNull_ThrowsException()141 public void PutAsJsonAsync_WhenClientIsNull_ThrowsException() 142 { 143 HttpClient client = null; 144 145 Assert.ThrowsArgumentNull(() => client.PutAsJsonAsync("http://www.example.com", new object()), "client"); 146 } 147 148 [Fact] PutAsJsonAsync_WhenUriIsNull_ThrowsException()149 public void PutAsJsonAsync_WhenUriIsNull_ThrowsException() 150 { 151 Assert.Throws<InvalidOperationException>(() => _client.PutAsJsonAsync(null, new object()), 152 "An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set."); 153 } 154 155 [Fact] PutAsJsonAsync_UsesJsonMediaTypeFormatter()156 public void PutAsJsonAsync_UsesJsonMediaTypeFormatter() 157 { 158 var result = _client.PutAsJsonAsync("http://example.com", new object()); 159 160 var response = result.Result; 161 var content = Assert.IsType<ObjectContent<object>>(response.RequestMessage.Content); 162 Assert.IsType<JsonMediaTypeFormatter>(content.Formatter); 163 } 164 165 [Fact] PutAsXmlAsync_WhenClientIsNull_ThrowsException()166 public void PutAsXmlAsync_WhenClientIsNull_ThrowsException() 167 { 168 HttpClient client = null; 169 170 Assert.ThrowsArgumentNull(() => client.PutAsXmlAsync("http://www.example.com", new object()), "client"); 171 } 172 173 [Fact] PutAsXmlAsync_WhenUriIsNull_ThrowsException()174 public void PutAsXmlAsync_WhenUriIsNull_ThrowsException() 175 { 176 Assert.Throws<InvalidOperationException>(() => _client.PutAsXmlAsync(null, new object()), 177 "An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set."); 178 } 179 180 [Fact] PutAsXmlAsync_UsesXmlMediaTypeFormatter()181 public void PutAsXmlAsync_UsesXmlMediaTypeFormatter() 182 { 183 var result = _client.PutAsXmlAsync("http://example.com", new object()); 184 185 var response = result.Result; 186 var content = Assert.IsType<ObjectContent<object>>(response.RequestMessage.Content); 187 Assert.IsType<XmlMediaTypeFormatter>(content.Formatter); 188 } 189 190 [Fact] PutAsync_WhenClientIsNull_ThrowsException()191 public void PutAsync_WhenClientIsNull_ThrowsException() 192 { 193 HttpClient client = null; 194 195 Assert.ThrowsArgumentNull(() => client.PutAsync("http://www.example.com", new object(), new JsonMediaTypeFormatter(), "text/json"), "client"); 196 } 197 198 [Fact] PutAsync_WhenRequestUriIsNull_ThrowsException()199 public void PutAsync_WhenRequestUriIsNull_ThrowsException() 200 { 201 Assert.Throws<InvalidOperationException>(() => _client.PutAsync(null, new object(), new JsonMediaTypeFormatter(), "text/json"), 202 "An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set."); 203 } 204 205 [Fact] PutAsync_WhenRequestUriIsSet_CreatesRequestWithAppropriateUri()206 public void PutAsync_WhenRequestUriIsSet_CreatesRequestWithAppropriateUri() 207 { 208 _client.BaseAddress = new Uri("http://example.com/"); 209 210 var result = _client.PutAsync("myapi/", new object(), _formatter); 211 212 var request = result.Result.RequestMessage; 213 Assert.Equal("http://example.com/myapi/", request.RequestUri.ToString()); 214 } 215 216 [Fact] PutAsync_WhenAuthoritativeMediaTypeIsSet_CreatesRequestWithAppropriateContentType()217 public void PutAsync_WhenAuthoritativeMediaTypeIsSet_CreatesRequestWithAppropriateContentType() 218 { 219 var result = _client.PutAsync("http://example.com/myapi/", new object(), _formatter, "foo/bar; charset=utf-16"); 220 221 var request = result.Result.RequestMessage; 222 Assert.Equal("foo/bar", request.Content.Headers.ContentType.MediaType); 223 Assert.Equal("utf-16", request.Content.Headers.ContentType.CharSet); 224 } 225 226 [Fact] PutAsync_WhenFormatterIsSet_CreatesRequestWithObjectContentAndCorrectFormatter()227 public void PutAsync_WhenFormatterIsSet_CreatesRequestWithObjectContentAndCorrectFormatter() 228 { 229 var result = _client.PutAsync("http://example.com/myapi/", new object(), _formatter, "foo/bar; charset=utf-16"); 230 231 var request = result.Result.RequestMessage; 232 var content = Assert.IsType<ObjectContent<object>>(request.Content); 233 Assert.Same(_formatter, content.Formatter); 234 } 235 236 [Fact] PutAsync_IssuesPutRequest()237 public void PutAsync_IssuesPutRequest() 238 { 239 var result = _client.PutAsync("http://example.com/myapi/", new object(), _formatter); 240 241 var request = result.Result.RequestMessage; 242 Assert.Same(HttpMethod.Put, request.Method); 243 } 244 245 [Fact] PutAsync_WhenMediaTypeFormatterIsNull_ThrowsException()246 public void PutAsync_WhenMediaTypeFormatterIsNull_ThrowsException() 247 { 248 Assert.ThrowsArgumentNull(() => _client.PutAsync("http;//example.com", new object(), formatter: null), "formatter"); 249 } 250 } 251 } 252