public virtual void TestExtractTokenFail() { HttpURLConnection conn = Org.Mockito.Mockito.Mock <HttpURLConnection>(); Org.Mockito.Mockito.When(conn.GetResponseCode()).ThenReturn(HttpURLConnection.HttpUnauthorized ); string tokenStr = "foo"; IDictionary <string, IList <string> > headers = new Dictionary <string, IList <string> >(); IList <string> cookies = new AList <string>(); cookies.AddItem(AuthenticatedURL.AuthCookie + "=" + tokenStr); headers["Set-Cookie"] = cookies; Org.Mockito.Mockito.When(conn.GetHeaderFields()).ThenReturn(headers); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); token.Set("bar"); try { AuthenticatedURL.ExtractToken(conn, token); NUnit.Framework.Assert.Fail(); } catch (AuthenticationException) { // Expected NUnit.Framework.Assert.IsFalse(token.IsSet()); } catch (Exception) { NUnit.Framework.Assert.Fail(); } }
public virtual void TestInjectToken() { HttpURLConnection conn = Org.Mockito.Mockito.Mock <HttpURLConnection>(); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); token.Set("foo"); AuthenticatedURL.InjectToken(conn, token); Org.Mockito.Mockito.Verify(conn).AddRequestProperty(Org.Mockito.Mockito.Eq("Cookie" ), Org.Mockito.Mockito.AnyString()); }
/// <summary>Helper method that extracts an authentication token received from a connection. /// </summary> /// <remarks> /// Helper method that extracts an authentication token received from a connection. /// <p> /// This method is used by /// <see cref="Authenticator"/> /// implementations. /// </remarks> /// <param name="conn">connection to extract the authentication token from.</param> /// <param name="token">the authentication token.</param> /// <exception cref="System.IO.IOException">if an IO error occurred.</exception> /// <exception cref="AuthenticationException">if an authentication exception occurred. /// </exception> /// <exception cref="Org.Apache.Hadoop.Security.Authentication.Client.AuthenticationException /// "/> public static void ExtractToken(HttpURLConnection conn, AuthenticatedURL.Token token ) { int respCode = conn.GetResponseCode(); if (respCode == HttpURLConnection.HttpOk || respCode == HttpURLConnection.HttpCreated || respCode == HttpURLConnection.HttpAccepted) { IDictionary <string, IList <string> > headers = conn.GetHeaderFields(); IList <string> cookies = headers["Set-Cookie"]; if (cookies != null) { foreach (string cookie in cookies) { if (cookie.StartsWith(AuthCookieEq)) { string value = Runtime.Substring(cookie, AuthCookieEq.Length); int separator = value.IndexOf(";"); if (separator > -1) { value = Runtime.Substring(value, 0, separator); } if (value.Length > 0) { token.Set(value); } } } } } else { token.Set(null); throw new AuthenticationException("Authentication failed, status: " + conn.GetResponseCode () + ", message: " + conn.GetResponseMessage()); } }