public async Task GetAsync_RedirectResponseHasCookie_CookieSentToFinalUri( CookieUsePolicy cookieUsePolicy, string cookieName, string cookieValue) { Uri uri = HttpTestServers.RedirectUriForDestinationUri(false, 302, HttpTestServers.RemoteEchoServer, 1); var handler = new WinHttpHandler(); handler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseWinInetProxy; handler.CookieUsePolicy = cookieUsePolicy; if (cookieUsePolicy == CookieUsePolicy.UseSpecifiedCookieContainer) { handler.CookieContainer = new CookieContainer(); } using (HttpClient client = new HttpClient(handler)) { 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(JsonMessageContainsKeyValue(responseText, cookieName, cookieValue)); } } }
public static IEnumerable <object[]> UseCallback_ValidCertificate_ExpectedValuesDuringCallback_Urls() { foreach (bool checkRevocation in new[] { true, false }) { yield return(new object[] { HttpTestServers.SecureRemoteEchoServer, checkRevocation }); yield return(new object[] { HttpTestServers.RedirectUriForDestinationUri(true, HttpTestServers.SecureRemoteEchoServer, 1), checkRevocation }); } }
public async Task GetAsync_ServerNeedsAuthAndNoCredential_StatusCodeUnauthorized() { using (var client = new HttpClient()) { Uri uri = HttpTestServers.BasicAuthUriForCreds(Username, Password); HttpResponseMessage response = await client.GetAsync(uri); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); } }
public async void GetAsync_CredentialIsNetworkCredentialUriRedirect_StatusCodeUnauthorized() { Uri redirectUri = HttpTestServers.RedirectUriForCreds(Username, Password); using (var handler = new HttpClientHandler()) { handler.Credentials = _credential; var client = new HttpClient(handler); HttpResponseMessage unAuthResponse = await client.GetAsync(redirectUri); Assert.Equal(HttpStatusCode.Unauthorized, unAuthResponse.StatusCode); } }
public async Task GetAsync_ServerNeedsAuthAndSetCredential_StatusCodeOK() { var handler = new HttpClientHandler(); handler.Credentials = _credential; using (var client = new HttpClient(handler)) { Uri uri = HttpTestServers.BasicAuthUriForCreds(Username, Password); HttpResponseMessage response = await client.GetAsync(uri); Assert.Equal(HttpStatusCode.OK, response.StatusCode); } }
public async Task GetAsync_CredentialIsNetworkCredentialUriRedirect_StatusCodeUnauthorized() { var handler = new HttpClientHandler(); handler.Credentials = _credential; using (var client = new HttpClient(handler)) { Uri redirectUri = HttpTestServers.RedirectUriForCreds(secure: false, userName: Username, password: Password); using (HttpResponseMessage unAuthResponse = await client.GetAsync(redirectUri)) { Assert.Equal(HttpStatusCode.Unauthorized, unAuthResponse.StatusCode); } } }
public async Task GetAsync_MaxAutomaticRedirectionsNServerHopsNPlus1_Throw(int hops) { var handler = new HttpClientHandler(); handler.MaxAutomaticRedirections = hops; using (var client = new HttpClient(handler)) { await Assert.ThrowsAsync <HttpRequestException>(() => client.GetAsync(HttpTestServers.RedirectUriForDestinationUri( secure: false, destinationUri: HttpTestServers.RemoteEchoServer, hops: (hops + 1)))); } }
public async Task GetAsync_AllowAutoRedirectTrue_RedirectFromHttpsToHttp_StatusCodeRedirect() { var handler = new HttpClientHandler(); handler.AllowAutoRedirect = true; using (var client = new HttpClient(handler)) { Uri uri = HttpTestServers.SecureRedirectUriForDestinationUri(HttpTestServers.RemoteGetServer); _output.WriteLine("Uri: {0}", uri); HttpResponseMessage response = await client.GetAsync(uri); Assert.Equal(HttpStatusCode.Redirect, response.StatusCode); } }
public async Task GetAsync_CallMethod_ExpectedStatusLine(HttpStatusCode statusCode, string reasonPhrase) { using (var client = new HttpClient()) { using (HttpResponseMessage response = await client.GetAsync(HttpTestServers.StatusCodeUri( false, (int)statusCode, reasonPhrase))) { Assert.Equal(statusCode, response.StatusCode); Assert.Equal(reasonPhrase, response.ReasonPhrase); } } }
public async Task UseDefaultCredentials_SetToFalseAndServerNeedsAuth_StatusCodeUnauthorized(bool useProxy) { var handler = new HttpClientHandler(); handler.UseProxy = useProxy; handler.UseDefaultCredentials = false; using (var client = new HttpClient(handler)) { Uri uri = HttpTestServers.NegotiateAuthUriForDefaultCreds(secure: false); _output.WriteLine("Uri: {0}", uri); using (HttpResponseMessage response = await client.GetAsync(uri)) { Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); } } }
public void GetAsync_CredentialIsCredentialCacheUriRedirect_HttpStatusCodeOK() { Uri uri = HttpTestServers.BasicAuthUriForCreds(Username, Password); Uri redirectUri = HttpTestServers.RedirectUriForCreds(Username, Password); var credentialCache = new CredentialCache(); credentialCache.Add(uri, "Basic", _credential); using (var handler = new HttpClientHandler()) { handler.Credentials = credentialCache; var client = new HttpClient(handler); var response = client.GetAsync(uri).Result; Assert.Equal(HttpStatusCode.OK, response.StatusCode); } }
public async Task UseCallback_RedirectandValidCertificate_ExpectedValuesDuringCallback() { Uri uri = HttpTestServers.RedirectUriForDestinationUri(true, HttpTestServers.SecureRemoteEchoServer, 1); var handler = new WinHttpHandler(); handler.ServerCertificateValidationCallback = CustomServerCertificateValidationCallback; using (var client = new HttpClient(handler)) { HttpResponseMessage response = await client.GetAsync(uri); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(_validationCallbackHistory.WasCalled); ConfirmValidCertificate(HttpTestServers.Host); } }
public async Task GetAsync_AllowAutoRedirectTrue_RedirectToUriWithParams_RequestMsgUriSet() { var handler = new HttpClientHandler(); handler.AllowAutoRedirect = true; Uri targetUri = HttpTestServers.BasicAuthUriForCreds(secure: false, userName: Username, password: Password); using (var client = new HttpClient(handler)) { Uri uri = HttpTestServers.RedirectUriForDestinationUri(secure: false, destinationUri: targetUri, hops: 1); _output.WriteLine("Uri: {0}", uri); using (HttpResponseMessage response = await client.GetAsync(uri)) { Assert.Equal(targetUri, response.RequestMessage.RequestUri); } } }
public async Task GetAsync_CallMethod_ExpectedStatusLine() { HttpStatusCode expectedStatusCode = HttpStatusCode.MethodNotAllowed; string expectedReasonPhrase = "Custom descrption"; using (var client = new HttpClient()) { using (HttpResponseMessage response = await client.GetAsync(HttpTestServers.StatusCodeUri( false, (int)expectedStatusCode, expectedReasonPhrase))) { Assert.Equal(expectedStatusCode, response.StatusCode); Assert.Equal(expectedReasonPhrase, response.ReasonPhrase); } } }
public async Task GetAsync_CredentialIsCredentialCacheUriRedirect_StatusCodeOK() { Uri uri = HttpTestServers.BasicAuthUriForCreds(Username, Password); Uri redirectUri = HttpTestServers.RedirectUriForCreds(Username, Password); var credentialCache = new CredentialCache(); credentialCache.Add(uri, "Basic", _credential); var handler = new HttpClientHandler(); handler.Credentials = credentialCache; using (var client = new HttpClient(handler)) { HttpResponseMessage response = await client.GetAsync(redirectUri); Assert.Equal(HttpStatusCode.OK, response.StatusCode); } }
public async Task GetAsync_AllowAutoRedirectTrue_RedirectFromHttpToHttps_StatusCodeOK() { var handler = new HttpClientHandler(); handler.AllowAutoRedirect = true; using (var client = new HttpClient(handler)) { Uri uri = HttpTestServers.RedirectUriForDestinationUri( secure: false, destinationUri: HttpTestServers.SecureRemoteEchoServer, hops: 1); _output.WriteLine("Uri: {0}", uri); using (HttpResponseMessage response = await client.GetAsync(uri)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); } } }
public async Task PostAsync_Redirect_ResultingGetFormattedCorrectly(bool secure) { const string ContentString = "This is the content string."; var content = new StringContent(ContentString); Uri redirectUri = HttpTestServers.RedirectUriForDestinationUri( secure, secure ? HttpTestServers.SecureRemoteEchoServer : HttpTestServers.RemoteEchoServer, 1); using (var client = new HttpClient()) using (HttpResponseMessage response = await client.PostAsync(redirectUri, content)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); string responseContent = await response.Content.ReadAsStringAsync(); Assert.DoesNotContain(ContentString, responseContent); Assert.DoesNotContain("Content-Length", responseContent); } }
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 GetAsync_CredentialIsCredentialCacheUriRedirect_StatusCodeOK() { Uri uri = HttpTestServers.BasicAuthUriForCreds(secure: false, userName: Username, password: Password); Uri redirectUri = HttpTestServers.RedirectUriForCreds(secure: false, userName: Username, password: Password); _output.WriteLine(uri.AbsoluteUri); _output.WriteLine(redirectUri.AbsoluteUri); var credentialCache = new CredentialCache(); credentialCache.Add(uri, "Basic", _credential); var handler = new HttpClientHandler(); handler.Credentials = credentialCache; using (var client = new HttpClient(handler)) { using (HttpResponseMessage response = await client.GetAsync(redirectUri)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); } } }
public async void GetAsync_MaxAutomaticRedirectionsNServerHopsNPlus1_Throw(int hops) { using (var handler = new HttpClientHandler()) { handler.MaxAutomaticRedirections = hops; var client = new HttpClient(handler); await Assert.ThrowsAsync <HttpRequestException>(() => client.GetAsync(HttpTestServers.RedirectUriHops(hops + 1))); } }