2 // HttpRequestMessageTest.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin, Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Collections.Generic;
32 using NUnit.Framework;
33 using System.Net.Http;
35 using System.Net.Http.Headers;
38 namespace MonoTests.System.Net.Http
41 public class HttpRequestMessageTest
44 public void Ctor_Invalid ()
47 new HttpRequestMessage (null, "");
49 } catch (ArgumentNullException) {
54 public void Ctor_Default ()
56 var m = new HttpRequestMessage ();
57 Assert.IsNull (m.Content, "#1");
58 Assert.IsNotNull (m.Headers, "#2");
59 Assert.AreEqual (HttpMethod.Get, m.Method, "#3");
60 Assert.IsNotNull (m.Properties, "#4");
61 Assert.IsNull (m.RequestUri, "#5");
62 Assert.AreEqual (new Version (1, 1), m.Version, "#6");
66 public void Ctor_Uri ()
68 var c = new HttpRequestMessage (HttpMethod.Get, new Uri ("http://xamarin.com"));
69 Assert.AreEqual ("http://xamarin.com/", c.RequestUri.AbsoluteUri, "#1");
71 c = new HttpRequestMessage (HttpMethod.Get, new Uri ("https://xamarin.com"));
72 Assert.AreEqual ("https://xamarin.com/", c.RequestUri.AbsoluteUri, "#2");
74 c = new HttpRequestMessage (HttpMethod.Get, new Uri ("HTTP://xamarin.com:8080"));
75 Assert.AreEqual ("http://xamarin.com:8080/", c.RequestUri.AbsoluteUri, "#3");
77 var a = Uri.UriSchemeHttps;
78 var b = new Uri ("http://xamarin.com");
81 new HttpRequestMessage (HttpMethod.Get, new Uri ("ftp://xamarin.com"));
83 } catch (ArgumentException) {
88 public void Headers ()
90 HttpRequestMessage message = new HttpRequestMessage ();
91 HttpRequestHeaders headers = message.Headers;
93 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
94 headers.AcceptCharset.Add (new StringWithQualityHeaderValue ("str-v", 0.002));
95 headers.AcceptEncoding.Add (new StringWithQualityHeaderValue ("str-enc", 0.44));
96 headers.AcceptLanguage.Add (new StringWithQualityHeaderValue ("str-lang", 0.41));
97 headers.Authorization = new AuthenticationHeaderValue ("sh-aut", "par");
98 headers.CacheControl = new CacheControlHeaderValue () {
99 MaxAge = TimeSpan.MaxValue
101 headers.Connection.Add ("test-value");
102 headers.ConnectionClose = true;
103 headers.Date = new DateTimeOffset (DateTime.Today);
104 headers.Expect.Add (new NameValueWithParametersHeaderValue ("en", "ev"));
105 headers.ExpectContinue = true;
106 headers.From = "webmaster@w3.org";
107 headers.Host = "host";
108 headers.IfMatch.Add (new EntityTagHeaderValue ("\"tag\"", true));
109 headers.IfModifiedSince = new DateTimeOffset (DateTime.Today);
110 headers.IfNoneMatch.Add (new EntityTagHeaderValue ("\"tag2\"", true));
111 headers.IfRange = new RangeConditionHeaderValue (new DateTimeOffset (DateTime.Today));
112 headers.IfUnmodifiedSince = new DateTimeOffset? (DateTimeOffset.Now);
113 headers.MaxForwards = 0x15b3;
114 headers.Pragma.Add (new NameValueHeaderValue ("name", "value"));
115 headers.ProxyAuthorization = new AuthenticationHeaderValue ("s", "p");
116 headers.Range = new RangeHeaderValue (5L, 30L);
117 headers.Referrer = new Uri ("http://xamarin.com");
118 headers.TE.Add (new TransferCodingWithQualityHeaderValue ("TE", 0.3));
119 headers.Trailer.Add ("value");
120 headers.TransferEncoding.Add (new TransferCodingHeaderValue ("tchv"));
121 headers.TransferEncodingChunked = true;
122 headers.Upgrade.Add (new ProductHeaderValue ("prod", "ver"));
123 headers.UserAgent.Add (new ProductInfoHeaderValue ("(comment)"));
124 headers.Via.Add (new ViaHeaderValue ("protocol", "rec-by"));
125 headers.Warning.Add (new WarningHeaderValue (5, "agent", "\"txt\""));
128 headers.Add ("authorization", "");
129 Assert.Fail ("Authorization");
130 } catch (FormatException) {
134 headers.Add ("connection", "extra ß ");
135 Assert.Fail ("Date");
136 } catch (FormatException) {
140 headers.Add ("date", "");
141 Assert.Fail ("Date");
142 } catch (FormatException) {
146 headers.Add ("from", "a@w3.org");
147 Assert.Fail ("From");
148 } catch (FormatException) {
152 headers.Add ("hOst", "host");
153 Assert.Fail ("Host");
154 } catch (FormatException) {
158 headers.Add ("if-modified-since", "");
159 Assert.Fail ("if-modified-since");
160 } catch (FormatException) {
164 headers.Add ("if-range", "");
165 Assert.Fail ("if-range");
166 } catch (FormatException) {
170 headers.Add ("if-unmodified-since", "");
171 Assert.Fail ("if-unmodified-since");
172 } catch (FormatException) {
176 headers.Add ("max-forwards", "");
177 Assert.Fail ("max-forwards");
178 } catch (FormatException) {
182 headers.Add ("proxy-authorization", "");
183 Assert.Fail ("proxy-authorization");
184 } catch (FormatException) {
188 headers.Add ("range", "");
189 } catch (FormatException) {
193 headers.Add ("referer", "");
194 Assert.Fail ("referer");
195 } catch (FormatException) {
198 headers.Add ("accept", "audio/y");
199 headers.Add ("accept-charset", "achs");
200 headers.Add ("accept-encoding", "aenc");
201 headers.Add ("accept-language", "alan");
202 headers.Add ("expect", "exp");
203 headers.Add ("if-match", "\"v\"");
204 headers.Add ("if-none-match", "\"v2\"");
205 headers.Add ("pragma", "p");
206 headers.Add ("TE", "0.8");
207 headers.Add ("trailer", "value2");
208 headers.Add ("transfer-encoding", "ttt");
209 headers.Add ("upgrade", "uuu");
210 headers.Add ("user-agent", "uaua");
211 headers.Add ("via", "prot v");
212 headers.Add ("warning", "4 ww \"t\"");
214 Assert.IsTrue (headers.Accept.SequenceEqual (
216 new MediaTypeWithQualityHeaderValue ("audio/x"),
217 new MediaTypeWithQualityHeaderValue ("audio/y")
221 Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
223 new StringWithQualityHeaderValue ("str-v", 0.002),
224 new StringWithQualityHeaderValue ("achs")
228 Assert.IsTrue (headers.AcceptEncoding.SequenceEqual (
230 new StringWithQualityHeaderValue ("str-enc", 0.44),
231 new StringWithQualityHeaderValue ("aenc")
235 Assert.IsTrue (headers.AcceptLanguage.SequenceEqual (
237 new StringWithQualityHeaderValue ("str-lang", 0.41),
238 new StringWithQualityHeaderValue ("alan")
242 Assert.AreEqual (new AuthenticationHeaderValue ("sh-aut", "par"), headers.Authorization);
244 var cch = new CacheControlHeaderValue () {
245 MaxAge = TimeSpan.MaxValue,
248 Assert.AreEqual (cch, headers.CacheControl);
250 Assert.IsTrue (headers.Connection.SequenceEqual (
251 new string[] { "test-value", "close" }));
253 Assert.AreEqual (headers.Date, new DateTimeOffset (DateTime.Today));
255 Assert.IsTrue (headers.Expect.SequenceEqual (
257 new NameValueWithParametersHeaderValue ("en", "ev"),
258 new NameValueWithParametersHeaderValue ("100-continue"),
259 new NameValueWithParametersHeaderValue ("exp")
262 Assert.AreEqual (headers.From, "webmaster@w3.org");
264 Assert.IsTrue (headers.IfMatch.SequenceEqual (
265 new EntityTagHeaderValue[] {
266 new EntityTagHeaderValue ("\"tag\"", true),
267 new EntityTagHeaderValue ("\"v\"", false)
271 Assert.AreEqual (headers.IfModifiedSince, new DateTimeOffset (DateTime.Today));
272 Assert.IsTrue (headers.IfNoneMatch.SequenceEqual (new EntityTagHeaderValue[] { new EntityTagHeaderValue ("\"tag2\"", true), new EntityTagHeaderValue ("\"v2\"", false) }));
273 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.IfRange.Date);
274 Assert.AreEqual (headers.MaxForwards, 0x15b3);
275 Assert.IsTrue (headers.Pragma.SequenceEqual (new NameValueHeaderValue[] { new NameValueHeaderValue ("name", "value"), new NameValueHeaderValue ("p", null) }));
276 Assert.AreEqual ("p", headers.ProxyAuthorization.Parameter);
277 Assert.AreEqual ("s", headers.ProxyAuthorization.Scheme);
278 Assert.AreEqual (5, headers.Range.Ranges.First ().From);
279 Assert.AreEqual (30, headers.Range.Ranges.First ().To);
280 Assert.AreEqual ("bytes", headers.Range.Unit);
281 Assert.AreEqual (headers.Referrer, new Uri ("http://xamarin.com"));
282 Assert.IsTrue (headers.TE.SequenceEqual (new TransferCodingWithQualityHeaderValue[] { new TransferCodingWithQualityHeaderValue ("TE", 0.3), new TransferCodingWithQualityHeaderValue ("0.8") }), "29");
283 Assert.IsTrue (headers.Trailer.SequenceEqual (
288 Assert.IsTrue (headers.TransferEncoding.SequenceEqual (
290 new TransferCodingHeaderValue ("tchv"),
291 new TransferCodingHeaderValue ("chunked"),
292 new TransferCodingHeaderValue ("ttt")
296 Assert.IsTrue (headers.Upgrade.SequenceEqual (
298 new ProductHeaderValue ("prod", "ver"),
299 new ProductHeaderValue ("uuu")
303 Assert.IsTrue (headers.UserAgent.SequenceEqual (
305 new ProductInfoHeaderValue ("(comment)"),
306 new ProductInfoHeaderValue ("uaua", null)
310 Assert.IsTrue (headers.Via.SequenceEqual (
312 new ViaHeaderValue ("protocol", "rec-by"),
313 new ViaHeaderValue ("prot", "v")
317 Assert.IsTrue (headers.Warning.SequenceEqual (
319 new WarningHeaderValue (5, "agent", "\"txt\""),
320 new WarningHeaderValue (4, "ww", "\"t\"")
327 public void Header_BaseImplementation ()
329 HttpRequestMessage message = new HttpRequestMessage ();
330 HttpRequestHeaders headers = message.Headers;
332 headers.Add ("a", "a-value");
333 headers.Add ("b", new List<string> { "v1", "v2" });
334 headers.Add ("c", null as string);
335 headers.Add ("d", new string[0]);
337 headers.AddWithoutValidation ("accept", "audio");
339 Assert.IsFalse (headers.Contains ("nn"), "#1a");
340 Assert.IsTrue (headers.Contains ("b"), "#1b");
342 var values = headers.GetValues ("b").ToList ();
343 Assert.AreEqual ("v1", values[0], "#2a");
344 Assert.AreEqual ("v2", values[1], "#2b");
346 Assert.IsFalse (headers.Remove ("p"), "#3a");
347 Assert.IsTrue (headers.Remove ("b"), "#3b");
348 Assert.IsFalse (headers.Contains ("b"), "#3b-c");
350 IEnumerable<string> values2;
351 Assert.IsTrue (headers.TryGetValues ("c", out values2));
352 values = values2.ToList ();
353 Assert.AreEqual ("", values[0], "#4a");
356 foreach (var i in headers) {
360 Assert.AreEqual (3, counter, "#5");
364 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
365 headers.AddWithoutValidation ("accept", "audio");
367 values = headers.GetValues ("accept").ToList ();
368 Assert.AreEqual (2, values.Count, "#6");
369 Assert.AreEqual ("audio/x", values[0], "#6a");
370 Assert.AreEqual ("audio", values[1], "#6b");
371 Assert.AreEqual (1, headers.Accept.Count, "#6c");
375 headers.AddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" });
376 values = headers.GetValues ("from").ToList ();
378 Assert.AreEqual (2, values.Count, "#7");
379 Assert.AreEqual ("a@a.com", values[0], "#7a");
380 Assert.AreEqual ("ssss@oo.com", values[1], "#7b");
381 Assert.AreEqual ("a@a.com", headers.From, "#7c");
385 headers.AddWithoutValidation ("Date", "wrong date");
386 var value = headers.Date;
387 Assert.IsNull (headers.Date, "#8");
391 public void Headers_Invalid ()
393 HttpRequestMessage message = new HttpRequestMessage ();
394 HttpRequestHeaders headers = message.Headers;
397 headers.Add ("Allow", "");
399 } catch (InvalidOperationException) {
403 headers.Add (null, "");
405 } catch (ArgumentException) {
409 headers.Add ("mm", null as IEnumerable<string>);
411 } catch (ArgumentNullException) {
415 headers.Add ("accept", "audio");
417 } catch (FormatException) {
421 headers.AddWithoutValidation ("Allow", "");
423 } catch (InvalidOperationException) {
427 headers.AddWithoutValidation (null, "");
429 } catch (ArgumentException) {
433 headers.Contains (null);
435 } catch (ArgumentException) {
439 headers.GetValues (null);
441 } catch (ArgumentException) {
445 headers.GetValues ("bbbb");
447 } catch (InvalidOperationException) {
451 headers.Add ("from", new[] { "a@a.com", "ssss@oo.com" });
453 } catch (FormatException) {
456 headers.AddWithoutValidation ("from", "a@a.com");
458 headers.Add ("from", "valid@w3.org");
460 } catch (FormatException) {
465 public void Headers_Response ()
467 HttpRequestMessage message = new HttpRequestMessage ();
468 HttpRequestHeaders headers = message.Headers;
470 headers.Add ("Age", "vv");
471 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
474 headers.AddWithoutValidation ("Age", "vv");
475 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
479 public void Headers_ExpectContinue ()
481 HttpRequestMessage message = new HttpRequestMessage ();
482 HttpRequestHeaders headers = message.Headers;
483 Assert.IsNull (headers.ExpectContinue, "#1");
485 headers.ExpectContinue = false;
486 Assert.IsFalse (headers.ExpectContinue.Value, "#2");
490 headers.ExpectContinue = true;
491 headers.ExpectContinue = true;
492 headers.ExpectContinue = true;
493 headers.ExpectContinue = true;
494 Assert.IsTrue (headers.ExpectContinue.Value, "#3");
495 Assert.AreEqual (1, headers.GetValues ("expect").ToList ().Count, "#4");
498 headers.Expect.Add (new NameValueWithParametersHeaderValue ("100-conTinuE"));
499 Assert.IsTrue (headers.ExpectContinue.Value, "#5");
503 public void Headers_ConnectionClose ()
505 HttpRequestMessage message = new HttpRequestMessage ();
506 HttpRequestHeaders headers = message.Headers;
507 Assert.IsNull (headers.ConnectionClose, "#1");
509 headers.ConnectionClose = false;
510 Assert.IsFalse (headers.ConnectionClose.Value, "#2");
514 headers.ConnectionClose = true;
515 Assert.IsTrue (headers.ConnectionClose.Value, "#3");
518 headers.Connection.Add ("Close");
519 Assert.IsTrue (headers.ConnectionClose.Value, "#4");
523 public void Headers_From_Invalid ()
525 HttpRequestMessage message = new HttpRequestMessage ();
526 HttpRequestHeaders headers = message.Headers;
532 } catch (FormatException) {
535 headers.From = "test";
537 } catch (FormatException) {
542 public void Headers_TransferEncodingChunked ()
544 HttpRequestMessage message = new HttpRequestMessage ();
545 HttpRequestHeaders headers = message.Headers;
546 Assert.IsNull (headers.TransferEncodingChunked, "#1");
548 headers.TransferEncodingChunked = false;
549 Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
553 headers.TransferEncodingChunked = true;
554 Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
555 Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
559 public void Properties ()
561 var c = new HttpRequestMessage ();
563 c.Method = HttpMethod.Post;
564 c.Properties.Add ("a", "test");
566 c.Version = HttpVersion.Version10;
568 Assert.IsNull (c.Content, "#1");
569 Assert.AreEqual (HttpMethod.Post, c.Method, "#2");
570 Assert.AreEqual ("test", c.Properties["a"], "#3");
571 Assert.IsNull (c.RequestUri, "#4");
572 Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
576 public void Properties_Invalid ()
578 var c = new HttpRequestMessage ();
582 } catch (ArgumentNullException) {
586 c.RequestUri = new Uri ("ftp://xamarin.com");
588 } catch (ArgumentException) {
594 } catch (ArgumentNullException) {