1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System;
6 using System.Diagnostics;
7 using System.IO;
8 using System.Net.Http;
9 using System.Text;
10 using System.Threading.Tasks;
11 using Xunit;
12 using Xunit.Abstractions;
13 
14 namespace System.Net.Http.Functional.Tests
15 {
16     using Configuration = System.Net.Test.Common.Configuration;
17 
18     public class PostScenarioUWPTest : HttpClientTestBase
19     {
20         private readonly ITestOutputHelper _output;
21 
PostScenarioUWPTest(ITestOutputHelper output)22         public PostScenarioUWPTest(ITestOutputHelper output)
23         {
24             _output = output;
25         }
26 
27         [Fact]
Authentication_UseStreamContent_Throws()28         public void Authentication_UseStreamContent_Throws()
29         {
30             RemoteInvoke(async useManagedHandlerString =>
31             {
32                 // This test validates the current limitation of CoreFx's NetFxToWinRtStreamAdapter
33                 // which throws exceptions when trying to rewind a .NET Stream when it needs to be
34                 // re-POST'd to the server.
35                 string username = "testuser";
36                 string password = "password";
37                 Uri uri = Configuration.Http.BasicAuthUriForCreds(secure: false, userName: username, password: password);
38                 HttpClientHandler handler = CreateHttpClientHandler(useManagedHandlerString);
39                 handler.Credentials = new NetworkCredential(username, password);
40 
41                 using (var client = new HttpClient(handler))
42                 {
43                     byte[] postData = Encoding.UTF8.GetBytes("This is data to post.");
44                     var stream = new MemoryStream(postData, false);
45                     var content = new StreamContent(stream);
46 
47                     await Assert.ThrowsAsync<HttpRequestException>(() => client.PostAsync(uri, content));
48                 }
49 
50                 return SuccessExitCode;
51             }, UseManagedHandler.ToString()).Dispose();
52         }
53 
54         [Fact]
Authentication_UseMultiInterfaceNonRewindableStreamContent_Throws()55         public void Authentication_UseMultiInterfaceNonRewindableStreamContent_Throws()
56         {
57             RemoteInvoke(async useManagedHandlerString =>
58             {
59                 string username = "testuser";
60                 string password = "password";
61                 Uri uri = Configuration.Http.BasicAuthUriForCreds(secure: false, userName: username, password: password);
62                 HttpClientHandler handler = CreateHttpClientHandler(useManagedHandlerString);
63                 handler.Credentials = new NetworkCredential(username, password);
64 
65                 using (var client = new HttpClient(handler))
66                 {
67                     byte[] postData = Encoding.UTF8.GetBytes("This is data to post.");
68                     var stream = new MultiInterfaceNonRewindableReadOnlyStream(postData);
69                     var content = new MultiInterfaceStreamContent(stream);
70 
71                     await Assert.ThrowsAsync<HttpRequestException>(() => client.PostAsync(uri, content));
72                 }
73 
74                 return SuccessExitCode;
75             }, UseManagedHandler.ToString()).Dispose();
76         }
77 
78         [Fact]
Authentication_UseMultiInterfaceStreamContent_Success()79         public void Authentication_UseMultiInterfaceStreamContent_Success()
80         {
81             RemoteInvoke(async useManagedHandlerString =>
82             {
83                 string username = "testuser";
84                 string password = "password";
85                 Uri uri = Configuration.Http.BasicAuthUriForCreds(secure: false, userName: username, password: password);
86                 HttpClientHandler handler = CreateHttpClientHandler(useManagedHandlerString);
87                 handler.Credentials = new NetworkCredential(username, password);
88 
89                 using (var client = new HttpClient(handler))
90                 {
91                     byte[] postData = Encoding.UTF8.GetBytes("This is data to post.");
92                     var stream = new MultiInterfaceReadOnlyStream(postData);
93                     var content = new MultiInterfaceStreamContent(stream);
94 
95                     using (HttpResponseMessage response = await client.PostAsync(uri, content))
96                     {
97                         Assert.Equal(HttpStatusCode.OK, response.StatusCode);
98                         string responseContent = await response.Content.ReadAsStringAsync();
99                     }
100                 }
101 
102                 return SuccessExitCode;
103             }, UseManagedHandler.ToString()).Dispose();
104         }
105 
106         [Fact]
Authentication_UseMemoryStreamVisibleBufferContent_Success()107         public void Authentication_UseMemoryStreamVisibleBufferContent_Success()
108         {
109             RemoteInvoke(async useManagedHandlerString =>
110             {
111                 string username = "testuser";
112                 string password = "password";
113                 Uri uri = Configuration.Http.BasicAuthUriForCreds(secure: false, userName: username, password: password);
114                 HttpClientHandler handler = CreateHttpClientHandler(useManagedHandlerString);
115                 handler.Credentials = new NetworkCredential(username, password);
116 
117                 using (var client = new HttpClient(handler))
118                 {
119                     byte[] postData = Encoding.UTF8.GetBytes("This is data to post.");
120                     var stream = new MemoryStream(postData, 0, postData.Length, false, true);
121                     var content = new MultiInterfaceStreamContent(stream);
122 
123                     using (HttpResponseMessage response = await client.PostAsync(uri, content))
124                     {
125                         Assert.Equal(HttpStatusCode.OK, response.StatusCode);
126                         string responseContent = await response.Content.ReadAsStringAsync();
127                     }
128                 }
129 
130                 return SuccessExitCode;
131             }, UseManagedHandler.ToString()).Dispose();
132         }
133 
134         [Fact]
Authentication_UseMemoryStreamNotVisibleBufferContent_Success()135         public void Authentication_UseMemoryStreamNotVisibleBufferContent_Success()
136         {
137             RemoteInvoke(async useManagedHandlerString =>
138             {
139                 string username = "testuser";
140                 string password = "password";
141                 Uri uri = Configuration.Http.BasicAuthUriForCreds(secure: false, userName: username, password: password);
142                 HttpClientHandler handler = CreateHttpClientHandler(useManagedHandlerString);
143                 handler.Credentials = new NetworkCredential(username, password);
144 
145                 using (var client = new HttpClient(handler))
146                 {
147                     byte[] postData = Encoding.UTF8.GetBytes("This is data to post.");
148                     var stream = new MemoryStream(postData, 0, postData.Length, false, false);
149                     var content = new MultiInterfaceStreamContent(stream);
150 
151                     using (HttpResponseMessage response = await client.PostAsync(uri, content))
152                     {
153                         Assert.Equal(HttpStatusCode.OK, response.StatusCode);
154                         string responseContent = await response.Content.ReadAsStringAsync();
155                     }
156                 }
157 
158                 return SuccessExitCode;
159             }, UseManagedHandler.ToString()).Dispose();
160         }
161     }
162 }
163