public async Task GetAsync_LargeRequestHeader_HeadersAndValuesSent(Configuration.Http.RemoteServer remoteServer, Uri uri) { // Unfortunately, our remote servers seem to have pretty strict limits (around 16K?) // on the total size of the request header. // TODO: Figure out how to reconfigure remote endpoints to allow larger request headers, // and then increase the limits in this test. string headerValue = new string('a', 2048); const int headerCount = 6; using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer)) { for (int i = 0; i < headerCount; i++) { client.DefaultRequestHeaders.Add($"Header-{i}", headerValue); } using (HttpResponseMessage httpResponse = await client.GetAsync(uri)) { Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); string responseText = await httpResponse.Content.ReadAsStringAsync(); for (int i = 0; i < headerCount; i++) { Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, $"Header-{i}", headerValue)); } } } }
public async Task PostRewindableStreamContentMultipleTimes_StreamContentFullySent(Configuration.Http.RemoteServer remoteServer) { if (IsCurlHandler) { // CurlHandler rewinds the stream at the end: https://github.com/dotnet/corefx/issues/23782 return; } const string requestBody = "ABC"; using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer)) using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(requestBody))) { var content = new StreamContent(ms); for (int i = 1; i <= 3; i++) { HttpResponseMessage response = await client.PostAsync(remoteServer.EchoUri, content); Assert.Equal(requestBody.Length, ms.Position); // Stream left at end after send. string responseBody = await response.Content.ReadAsStringAsync(); _output.WriteLine(responseBody); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseBody, "BodyContent", requestBody)); } } }
private static async Task AssertSuccessfulGetResponse(HttpResponseMessage response, Uri uri, ITestOutputHelper output) { Assert.Equal <HttpStatusCode>(HttpStatusCode.OK, response.StatusCode); Assert.Equal <string>("OK", response.ReasonPhrase); string responseContent = await response.Content.ReadAsStringAsync(); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseContent, "url", uri.AbsoluteUri)); output.WriteLine(responseContent); }
public async Task GetAsync_RequestHeadersAddCustomHeaders_HeaderAndValueSent(string name, string value) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add(name, value); HttpResponseMessage httpResponse = await client.GetAsync(HttpTestServers.RemoteServerHeadersUri); httpResponse.EnsureSuccessStatusCode(); string responseText = await httpResponse.Content.ReadAsStringAsync(); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, name, value)); } }
public async Task GetAsync_RequestHeadersAddCustomHeaders_HeaderAndValueSent(string name, string value, Uri uri) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add(name, value); using (HttpResponseMessage httpResponse = await client.GetAsync(uri)) { Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); string responseText = await httpResponse.Content.ReadAsStringAsync(); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, name, value)); } } }
public async Task PostAsync_CallMethod_NullContent(Uri remoteServer) { using (var client = new HttpClient()) { HttpContent obj = new StringContent(String.Empty); HttpResponseMessage response = await client.PostAsync(remoteServer, null); Assert.Equal(HttpStatusCode.OK, response.StatusCode); string responseContent = await response.Content.ReadAsStringAsync(); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseContent, dataKey, String.Empty)); _output.WriteLine(responseContent); } }
public async Task GetAsync_RequestHeadersAddCustomHeaders_HeaderAndValueSent(Configuration.Http.RemoteServer remoteServer, string name, string value, Uri uri) { using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer)) { _output.WriteLine($"name={name}, value={value}"); client.DefaultRequestHeaders.Add(name, value); using (HttpResponseMessage httpResponse = await client.GetAsync(uri)) { Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); string responseText = await httpResponse.Content.ReadAsStringAsync(); _output.WriteLine(responseText); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, name, value)); } } }
public async Task PostAsync_CallMethod_StringContent(Uri remoteServer) { using (var client = new HttpClient()) { string data = "Test String"; var stringContent = new StringContent(data, Encoding.UTF8, mediaTypeJson); HttpResponseMessage response = await client.PostAsync(remoteServer, stringContent); Assert.Equal(HttpStatusCode.OK, response.StatusCode); string responseContent = await response.Content.ReadAsStringAsync(); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseContent, dataKey, data)); _output.WriteLine(responseContent); } }
public async Task GetAsync_SetCookieContainer_CookieSent(string name, string value) { var handler = new HttpClientHandler(); var cookieContainer = new CookieContainer(); cookieContainer.Add(HttpTestServers.RemoteServerCookieUri, new Cookie(name, value)); handler.CookieContainer = cookieContainer; using (var client = new HttpClient(handler)) { HttpResponseMessage httpResponse = await client.GetAsync(HttpTestServers.RemoteServerCookieUri); Assert.Equal(httpResponse.StatusCode, HttpStatusCode.OK); string responseText = await httpResponse.Content.ReadAsStringAsync(); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, name, value)); } }
public async Task PostAsync_CallMethod_UploadFile(Uri remoteServer) { string fileName = Path.GetTempFileName(); string fileTitle = "fileToUpload"; string fileContent = "This file to test POST Scenario"; try { // open file to edit using (FileStream fs = File.Open(fileName, FileMode.OpenOrCreate)) { // Add some text to file byte[] author = new UTF8Encoding(true).GetBytes(fileContent); fs.Write(author, 0, author.Length); } using (var client = new HttpClient()) { var form = new MultipartFormDataContent(); var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); var content = new StreamContent(stream); content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = fileTitle, FileName = fileName }; form.Add(content); HttpResponseMessage response = await client.PostAsync(remoteServer, form); Assert.Equal(HttpStatusCode.OK, response.StatusCode); string responseContent = await response.Content.ReadAsStringAsync(); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseContent, fileTitle, fileContent)); _output.WriteLine(responseContent); } } finally { if (File.Exists(fileName)) { File.Delete(fileName); } } }
public async Task PostAsync_CallMethod_FormUrlEncodedContent(Uri remoteServer) { using (var client = new HttpClient()) { var values = new Dictionary <string, string> { { "thing1", "hello" }, { "thing2", "world" } }; var content = new FormUrlEncodedContent(values); HttpResponseMessage response = await client.PostAsync(remoteServer, content); Assert.Equal(HttpStatusCode.OK, response.StatusCode); string responseContent = await response.Content.ReadAsStringAsync(); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseContent, "thing1", "hello")); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseContent, "thing2", "world")); _output.WriteLine(responseContent); } }
public async Task GetAsync_SetCookieContainer_CookieSent(string cookieName, string cookieValue) { var handler = new HttpClientHandler(); var cookieContainer = new CookieContainer(); cookieContainer.Add(HttpTestServers.RemoteEchoServer, new Cookie(cookieName, cookieValue)); handler.CookieContainer = cookieContainer; using (var client = new HttpClient(handler)) { using (HttpResponseMessage httpResponse = await client.GetAsync(HttpTestServers.RemoteEchoServer)) { Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); string responseText = await httpResponse.Content.ReadAsStringAsync(); _output.WriteLine(responseText); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, cookieName, cookieValue)); } } }
public async Task GetAsync_RedirectResponseHasCookie_CookieSentToFinalUri(string cookieName, string cookieValue) { Uri uri = HttpTestServers.RedirectUriForDestinationUri( secure: false, destinationUri: HttpTestServers.RemoteEchoServer, hops: 1); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add( "X-SetCookie", string.Format("{0}={1};Path=/", cookieName, cookieValue)); using (HttpResponseMessage httpResponse = await client.GetAsync(uri)) { string responseText = await httpResponse.Content.ReadAsStringAsync(); _output.WriteLine(responseText); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, cookieName, cookieValue)); } } }
public async Task PostRewindableStreamContentMultipleTimes_StreamContentFullySent(Uri serverUri) { const string requestBody = "ABC"; using (var client = new HttpClient()) using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(requestBody))) { var content = new StreamContent(ms); for (int i = 1; i <= 3; i++) { HttpResponseMessage response = await client.PostAsync(serverUri, content); Assert.Equal(requestBody.Length, ms.Position); // Stream left at end after send. string responseBody = await response.Content.ReadAsStringAsync(); _output.WriteLine(responseBody); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseBody, "BodyContent", requestBody)); } } }
private void VerifyAuthentication(string response, bool authenticated, string user) { // Convert all strings to lowercase to compare. Windows treats domain and username as case-insensitive. response = response.ToLower(); user = user.ToLower(); _output.WriteLine(response); if (!authenticated) { Assert.True( TestHelper.JsonMessageContainsKeyValue(response, "authenticated", "false"), "authenticated == false"); } else { Assert.True( TestHelper.JsonMessageContainsKeyValue(response, "authenticated", "true"), "authenticated == true"); Assert.True( TestHelper.JsonMessageContainsKeyValue(response, "user", user), $"user == {user}"); } }
public async Task GetAsync_RequestHeadersAddCustomHeaders_HeaderAndEmptyValueSent(Configuration.Http.RemoteServer remoteServer, Uri uri) { if (IsWinHttpHandler && !PlatformDetection.IsWindows10Version1709OrGreater) { return; } string name = "X-Cust-Header-NoValue"; string value = ""; using (HttpClient client = CreateHttpClientForRemoteServer(remoteServer)) { _output.WriteLine($"name={name}, value={value}"); client.DefaultRequestHeaders.Add(name, value); using (HttpResponseMessage httpResponse = await client.GetAsync(uri)) { Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); string responseText = await httpResponse.Content.ReadAsStringAsync(); _output.WriteLine(responseText); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, name, value)); } } }