public static HttpAcceptHeader Parse(string acceptHeaderString) { HttpAcceptHeader ret = new HttpAcceptHeader(); foreach (string part in acceptHeaderString.Split(',').Select(s => s.Trim()).ToArray()) { try { // q value? string[] parts = part.Split(';'); string[] fileParts = parts[0].Split('/'); string type = fileParts[0]; string extension = fileParts[1]; double? qval = null; if (parts.Length > 1) { qval = double.Parse(parts[1].Substring(parts[1].IndexOf("=") + 1).Trim()); } // assign ret.AddAccept(type, extension, qval); } catch (Exception ex) { } } return ret; }
public void TestAcceptHeaderParser() { // Arrange string accept = "text/html,text/json;q=0.9,*/*;q=0.2"; HttpAcceptHeader expected = new HttpAcceptHeader(); expected.AddAccept("text", "html", null); expected.AddAccept("text", "json", 0.9); expected.AddAccept("*", "*", 0.2); // Act HttpAcceptHeader header = HttpAcceptHeader.Parse(accept); // Assert Assert.AreEqual(expected, header); }
public void TestAcceptHeaderEquality() { // Arrange HttpAcceptHeader a = new HttpAcceptHeader(); a.AddAccept("text", "html", null); a.AddAccept("text", "json", 0.9); a.AddAccept("*", "*", 0.2); HttpAcceptHeader b = new HttpAcceptHeader(); b.AddAccept("text", "html", null); b.AddAccept("text", "json", 0.9); b.AddAccept("*", "*", 0.2); // Assert Assert.AreEqual(a, b); }
public void TestAcceptHeaderHashEquality() { // Arrange HttpAcceptHeader a = new HttpAcceptHeader(); a.AddAccept("text", "html", null); a.AddAccept("text", "json", 0.9); a.AddAccept("*", "*", 0.2); HttpAcceptHeader b = new HttpAcceptHeader(); b.AddAccept("text", "html", null); b.AddAccept("text", "json", 0.9); b.AddAccept("*", "*", 0.2); // Act int ha = a.GetHashCode(); int hb = b.GetHashCode(); // Assert Assert.AreEqual(ha, hb); }