using System;
using System.Collections;
using System.Collections.Generic;
+using System.Text.RegularExpressions;
using NUnit.Framework;
using System.Net.Http;
using System.Net;
Assert.AreEqual (3, i, "#10");
}
+ [Test]
+ public void Headers_MultiValues ()
+ {
+ var message = new HttpResponseMessage ();
+ var headers = message.Headers;
+
+ headers.Add ("Proxy-Authenticate", "x, y, z,i");
+ headers.Add ("Upgrade", "HTTP/2.0, SHTTP/1.3, IRC, RTA/x11");
+ headers.Add ("Via", "1.0 fred, 1.1 nowhere.com (Apache/1.1)");
+ headers.Add ("Warning", "199 Miscellaneous \"w\", 200 a \"b\"");
+
+ Assert.AreEqual (4, headers.ProxyAuthenticate.Count, "#1a");
+ Assert.IsTrue (headers.ProxyAuthenticate.SequenceEqual (
+ new[] {
+ new AuthenticationHeaderValue ("x"),
+
+ new AuthenticationHeaderValue ("y"),
+ new AuthenticationHeaderValue ("z"),
+ new AuthenticationHeaderValue ("i")
+ }
+ ), "#1b");
+
+
+ Assert.AreEqual (4, headers.Upgrade.Count, "#2a");
+ Assert.IsTrue (headers.Upgrade.SequenceEqual (
+ new[] {
+ new ProductHeaderValue ("HTTP", "2.0"),
+ new ProductHeaderValue ("SHTTP", "1.3"),
+ new ProductHeaderValue ("IRC"),
+ new ProductHeaderValue ("RTA", "x11")
+ }
+ ), "#2b");
+
+ Assert.AreEqual (2, headers.Via.Count, "#3a");
+ Assert.IsTrue (headers.Via.SequenceEqual (
+ new[] {
+ new ViaHeaderValue ("1.0", "fred"),
+ new ViaHeaderValue ("1.1", "nowhere.com", null, "(Apache/1.1)")
+ }
+ ), "#2b");
+
+ Assert.AreEqual (2, headers.Warning.Count, "#4a");
+ Assert.IsTrue (headers.Warning.SequenceEqual (
+ new[] {
+ new WarningHeaderValue (199, "Miscellaneous", "\"w\""),
+ new WarningHeaderValue (200, "a", "\"b\"")
+ }
+ ), "#4b");
+ }
+
[Test]
public void Header_BaseImplementation ()
{
headers.Add ("c", null as string);
headers.Add ("d", new string[0]);
- headers.AddWithoutValidation ("cache-control", "audio");
+ headers.TryAddWithoutValidation ("cache-control", "audio");
Assert.IsFalse (headers.Contains ("nn"), "#1a");
Assert.IsTrue (headers.Contains ("b"), "#1b");
} catch (InvalidOperationException) {
}
- try {
- headers.AddWithoutValidation ("Allow", "");
- Assert.Fail ("#3");
- } catch (InvalidOperationException) {
- }
+ Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3");
- try {
- headers.AddWithoutValidation (null, "");
- Assert.Fail ("#4");
- } catch (ArgumentException) {
- }
+ Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
try {
headers.Contains (null);
} catch (FormatException) {
}
- headers.AddWithoutValidation ("location", "a@a.com");
+ headers.TryAddWithoutValidation ("location", "a@a.com");
try {
headers.Add ("location", "w3.org");
Assert.Fail ("#7b");
Assert.AreEqual ("audio", headers.GetValues ("Accept").First (), "#1");
headers.Clear ();
- headers.AddWithoutValidation ("accept", "audio");
+ Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#2a");
Assert.AreEqual ("audio", headers.GetValues ("Accept").First (), "#2");
}
headers.Connection.Add ("Close");
Assert.IsTrue (headers.ConnectionClose.Value, "#4");
- Assert.AreEqual ("StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: <null>, Headers:\r\n{\r\nConnection: Close\r\n}", message.ToString (), "#5");
+ // .NET encloses the "Connection: Close" with two whitespaces.
+ var normalized = Regex.Replace (message.ToString (), @"\s", "");
+ Assert.AreEqual ("StatusCode:200,ReasonPhrase:'OK',Version:1.1,Content:<null>,Headers:{Connection:Close}", normalized, "#5");
}
[Test]
{
HttpResponseMessage message = new HttpResponseMessage ();
HttpResponseHeaders headers = message.Headers;
- headers.AddWithoutValidation ("location", "http://w3.org");
+ headers.TryAddWithoutValidation ("location", "http://w3.org");
Assert.AreEqual (new Uri ("http://w3.org"), headers.Location);
}
{
HttpResponseMessage message = new HttpResponseMessage ();
HttpResponseHeaders headers = message.Headers;
- headers.AddWithoutValidation ("Transfer-Encoding", "mmm");
- headers.AddWithoutValidation ("Transfer-Encoding", "▀");
- headers.AddWithoutValidation ("Transfer-Encoding", "zz");
+ headers.TryAddWithoutValidation ("Transfer-Encoding", "mmm");
+ headers.TryAddWithoutValidation ("Transfer-Encoding", "▀");
+ headers.TryAddWithoutValidation ("Transfer-Encoding", "zz");
var a = headers.TransferEncoding;
Assert.AreEqual (2, a.Count, "#1");
Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
headers.Clear ();
- headers.AddWithoutValidation ("Transfer-Encoding", "value");
+ Assert.IsTrue (headers.TryAddWithoutValidation ("Transfer-Encoding", "value"), "#4-0");
// Assert.AreEqual (false, headers.TransferEncodingChunked, "#4");
headers.Clear ();
- headers.AddWithoutValidation ("Transfer-Encoding", "chunked");
+ Assert.IsTrue (headers.TryAddWithoutValidation ("Transfer-Encoding", "chunked"), "#5-0");
Assert.AreEqual (true, headers.TransferEncodingChunked, "#5");
message = new HttpResponseMessage ();
headers = message.Headers;
- headers.AddWithoutValidation ("Transfer-Encoding", "ß");
+ Assert.IsTrue (headers.TryAddWithoutValidation ("Transfer-Encoding", "ß"), "#6-0");
Assert.IsNull (headers.TransferEncodingChunked, "#6");
}