public void CreateQueryString() { var args = new Dictionary <string, string>(); args.Add("a", "b"); args.Add("c/d", "e/f"); Assert.AreEqual("a=b&c%2fd=e%2ff", UriUtil.CreateQueryString(args)); }
void resign(ref Uri uri) { UriBuilder builder = new UriBuilder(uri); NameValueCollection nvc = HttpUtility.ParseQueryString(builder.Query); TestSupport.Resign(nvc, TestSupport.RelyingPartyStore); builder.Query = UriUtil.CreateQueryString(nvc); uri = builder.Uri; }
void setQueryParameter(ref Uri uri, string parameter, string newValue) { UriBuilder builder = new UriBuilder(uri); NameValueCollection nvc = HttpUtility.ParseQueryString(builder.Query); nvc[parameter] = newValue; builder.Query = UriUtil.CreateQueryString(nvc); uri = builder.Uri; }
void removeQueryParameter(ref Uri uri, string parameterToRemove) { UriBuilder builder = new UriBuilder(uri); NameValueCollection nvc = HttpUtility.ParseQueryString(builder.Query); nvc.Remove(parameterToRemove); builder.Query = UriUtil.CreateQueryString(nvc); uri = builder.Uri; }
void removeReturnToParameter(ref Uri uri, string parameterToRemove) { UriBuilder builder = new UriBuilder(uri); var args = Util.NameValueCollectionToDictionary( HttpUtility.ParseQueryString(builder.Query)); Protocol protocol = Protocol.Detect(args); UriBuilder return_to = new UriBuilder(protocol.openid.return_to); var returnToArgs = Util.NameValueCollectionToDictionary( HttpUtility.ParseQueryString(return_to.Query)); returnToArgs.Remove(parameterToRemove); return_to.Query = UriUtil.CreateQueryString(returnToArgs); args[protocol.openid.return_to] = return_to.ToString(); builder.Query = UriUtil.CreateQueryString(args); uri = builder.Uri; }
void parameterizedWebClientTest(Identifier identityUrl, AuthenticationRequestMode requestMode, AuthenticationStatus expectedResult) { Uri redirectToProviderUrl; HttpWebRequest rpRequest = (HttpWebRequest)WebRequest.Create(TestSupport.GetFullUrl(TestSupport.ConsumerPage)); NameValueCollection query = new NameValueCollection(); using (HttpWebResponse response = (HttpWebResponse)rpRequest.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { Regex regex = new Regex(@"\<input\b.*\bname=""(\w+)"".*\bvalue=""([^""]+)""", RegexOptions.IgnoreCase); while (!sr.EndOfStream) { string line = sr.ReadLine(); Match m = regex.Match(line); if (m.Success) { query[m.Groups[1].Value] = m.Groups[2].Value; } } } } query["OpenIdTextBox1$wrappedTextBox"] = identityUrl; rpRequest = (HttpWebRequest)WebRequest.Create(TestSupport.GetFullUrl(TestSupport.ConsumerPage)); rpRequest.Method = "POST"; rpRequest.AllowAutoRedirect = false; string queryString = UriUtil.CreateQueryString(query); rpRequest.ContentLength = queryString.Length; rpRequest.ContentType = "application/x-www-form-urlencoded"; using (StreamWriter sw = new StreamWriter(rpRequest.GetRequestStream())) { sw.Write(queryString); } using (HttpWebResponse response = (HttpWebResponse)rpRequest.GetResponse()) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { string doc = sr.ReadToEnd(); Debug.WriteLine(doc); } redirectToProviderUrl = new Uri(response.Headers[HttpResponseHeader.Location]); } HttpWebRequest providerRequest = (HttpWebRequest)WebRequest.Create(redirectToProviderUrl); providerRequest.AllowAutoRedirect = false; Uri redirectUrl; try { using (HttpWebResponse providerResponse = (HttpWebResponse)providerRequest.GetResponse()) { Assert.AreEqual(HttpStatusCode.Redirect, providerResponse.StatusCode); redirectUrl = new Uri(providerResponse.Headers[HttpResponseHeader.Location]); } } catch (WebException ex) { TestSupport.Logger.Error("WebException", ex); if (ex.Response != null) { using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream())) { TestSupport.Logger.ErrorFormat("Response stream follows: {0}", sr.ReadToEnd()); } } throw; } rpRequest = (HttpWebRequest)WebRequest.Create(redirectUrl); rpRequest.AllowAutoRedirect = false; using (HttpWebResponse response = (HttpWebResponse)rpRequest.GetResponse()) { Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode); // redirect on login } // Try replay attack if (expectedResult == AuthenticationStatus.Authenticated) { // This simulates a network sniffing user who caught the // authenticating query en route to either the user agent or // the consumer, and tries the same query to the consumer in an // attempt to spoof the identity of the authenticating user. rpRequest = (HttpWebRequest)WebRequest.Create(redirectUrl); rpRequest.AllowAutoRedirect = false; using (HttpWebResponse response = (HttpWebResponse)rpRequest.GetResponse()) { Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); // error message } } }
public void CreateQueryStringNullDictionary() { UriUtil.CreateQueryString((IDictionary <string, string>)null); }
public void CreateQueryStringNullNvc() { UriUtil.CreateQueryString((NameValueCollection)null); }
public void CreateQueryStringEmptyCollection() { Assert.AreEqual(0, UriUtil.CreateQueryString(new Dictionary <string, string>()).Length); }