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 System.Text.RegularExpressions;
33 using NUnit.Framework;
34 using System.Net.Http;
36 using System.Net.Http.Headers;
40 namespace MonoTests.System.Net.Http
43 public class HttpRequestMessageTest
46 public void Ctor_Invalid ()
49 new HttpRequestMessage (null, "");
51 } catch (ArgumentNullException) {
56 public void Ctor_Default ()
58 var m = new HttpRequestMessage ();
59 Assert.IsNull (m.Content, "#1");
60 Assert.IsNotNull (m.Headers, "#2");
61 Assert.AreEqual (HttpMethod.Get, m.Method, "#3");
62 Assert.IsNotNull (m.Properties, "#4");
63 Assert.IsNull (m.RequestUri, "#5");
64 Assert.AreEqual (new Version (1, 1), m.Version, "#6");
66 Assert.AreEqual ("Method: GET, RequestUri: '<null>', Version: 1.1, Content: <null>, Headers:\r\n{\r\n}", m.ToString (), "#7");
70 public void Ctor_Uri ()
72 var c = new HttpRequestMessage (HttpMethod.Get, new Uri ("http://xamarin.com"));
73 Assert.AreEqual ("http://xamarin.com/", c.RequestUri.AbsoluteUri, "#1");
75 c = new HttpRequestMessage (HttpMethod.Get, new Uri ("https://xamarin.com"));
76 Assert.AreEqual ("https://xamarin.com/", c.RequestUri.AbsoluteUri, "#2");
78 c = new HttpRequestMessage (HttpMethod.Get, new Uri ("HTTP://xamarin.com:8080"));
79 Assert.AreEqual ("http://xamarin.com:8080/", c.RequestUri.AbsoluteUri, "#3");
81 var a = Uri.UriSchemeHttps;
82 var b = new Uri ("http://xamarin.com");
85 new HttpRequestMessage (HttpMethod.Get, new Uri ("ftp://xamarin.com"));
87 } catch (ArgumentException) {
92 public void Ctor_RelativeUri ()
94 var client = new HttpClient ();
95 client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
96 var uri = new Uri ("Computer", UriKind.Relative);
97 var req = new HttpRequestMessage (HttpMethod.Get, uri);
98 // HttpRequestMessage does not rewrite it here.
99 Assert.AreEqual (req.RequestUri, uri);
103 public void Ctor_RelativeOrAbsoluteUri ()
105 var uri = new Uri ("/", UriKind.RelativeOrAbsolute);
106 new HttpRequestMessage (HttpMethod.Get, uri);
108 uri = new Uri ("file://", UriKind.RelativeOrAbsolute);
110 new HttpRequestMessage (HttpMethod.Get, uri);
112 } catch (ArgumentException) {
117 public void Ctor_RelativeUriString ()
119 var client = new HttpClient ();
120 client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
121 var req = new HttpRequestMessage (HttpMethod.Get, "Computer");
122 // HttpRequestMessage does not rewrite it here.
123 Assert.IsFalse (req.RequestUri.IsAbsoluteUri);
127 public void Ctor_RelativeOrAbsoluteUriString ()
129 new HttpRequestMessage (HttpMethod.Get, "/");
132 new HttpRequestMessage (HttpMethod.Get, "file://");
134 } catch (ArgumentException) {
139 public void Headers ()
141 HttpRequestMessage message = new HttpRequestMessage ();
142 HttpRequestHeaders headers = message.Headers;
144 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
145 headers.AcceptCharset.Add (new StringWithQualityHeaderValue ("str-v", 0.002));
146 headers.AcceptEncoding.Add (new StringWithQualityHeaderValue ("str-enc", 0.44));
147 headers.AcceptLanguage.Add (new StringWithQualityHeaderValue ("str-lang", 0.41));
148 headers.Authorization = new AuthenticationHeaderValue ("sh-aut", "par");
149 headers.CacheControl = new CacheControlHeaderValue () {
150 MaxAge = TimeSpan.MaxValue
152 headers.Connection.Add ("test-value");
153 headers.ConnectionClose = true;
154 headers.Date = new DateTimeOffset (DateTime.Today);
155 headers.Expect.Add (new NameValueWithParametersHeaderValue ("en", "ev"));
156 headers.ExpectContinue = true;
157 headers.From = "webmaster@w3.org";
158 headers.Host = "host";
159 headers.IfMatch.Add (new EntityTagHeaderValue ("\"tag\"", true));
160 headers.IfModifiedSince = new DateTimeOffset (DateTime.Today);
161 headers.IfNoneMatch.Add (new EntityTagHeaderValue ("\"tag2\"", true));
162 headers.IfRange = new RangeConditionHeaderValue (new DateTimeOffset (DateTime.Today));
163 headers.IfUnmodifiedSince = new DateTimeOffset? (DateTimeOffset.Now);
164 headers.MaxForwards = 0x15b3;
165 headers.Pragma.Add (new NameValueHeaderValue ("name", "value"));
166 headers.ProxyAuthorization = new AuthenticationHeaderValue ("s", "p");
167 headers.Range = new RangeHeaderValue (5L, 30L);
168 headers.Referrer = new Uri ("http://xamarin.com");
169 headers.TE.Add (new TransferCodingWithQualityHeaderValue ("TE", 0.3));
170 headers.Trailer.Add ("value");
171 headers.TransferEncoding.Add (new TransferCodingHeaderValue ("tchv"));
172 headers.TransferEncodingChunked = true;
173 headers.Upgrade.Add (new ProductHeaderValue ("prod", "ver"));
174 headers.UserAgent.Add (new ProductInfoHeaderValue ("(comment)"));
175 headers.Via.Add (new ViaHeaderValue ("protocol", "rec-by"));
176 headers.Warning.Add (new WarningHeaderValue (5, "agent", "\"txt\""));
179 headers.Add ("authorization", "");
180 Assert.Fail ("Authorization");
181 } catch (FormatException) {
185 headers.Add ("connection", "extra ß ");
186 Assert.Fail ("Date");
187 } catch (FormatException) {
191 headers.Add ("date", "");
192 Assert.Fail ("Date");
193 } catch (FormatException) {
197 headers.Add ("from", "a@w3.org");
198 Assert.Fail ("From");
199 } catch (FormatException) {
203 headers.Add ("hOst", "host");
204 Assert.Fail ("Host");
205 } catch (FormatException) {
209 headers.Add ("if-modified-since", "");
210 Assert.Fail ("if-modified-since");
211 } catch (FormatException) {
215 headers.Add ("if-range", "");
216 Assert.Fail ("if-range");
217 } catch (FormatException) {
221 headers.Add ("if-unmodified-since", "");
222 Assert.Fail ("if-unmodified-since");
223 } catch (FormatException) {
227 headers.Add ("max-forwards", "");
228 Assert.Fail ("max-forwards");
229 } catch (FormatException) {
233 headers.Add ("proxy-authorization", "");
234 Assert.Fail ("proxy-authorization");
235 } catch (FormatException) {
239 headers.Add ("range", "");
240 } catch (FormatException) {
244 headers.Add ("referer", "");
245 Assert.Fail ("referer");
246 } catch (FormatException) {
250 headers.Add ("pragma", "nocache,RequestID=1,g=");
251 Assert.Fail ("pragma");
252 } catch (FormatException) {
255 headers.Add ("accept", "audio/y");
256 headers.Add ("accept-charset", "achs");
257 headers.Add ("accept-encoding", "aenc");
258 headers.Add ("accept-language", "alan");
259 headers.Add ("expect", "exp");
260 headers.Add ("if-match", "\"v\"");
261 headers.Add ("if-none-match", "\"v2\"");
262 headers.Add ("TE", "0.8");
263 headers.Add ("trailer", "value2");
264 headers.Add ("transfer-encoding", "ttt");
265 headers.Add ("upgrade", "uuu");
266 headers.Add ("user-agent", "uaua");
267 headers.Add ("via", "prot v");
268 headers.Add ("warning", "4 ww \"t\"");
269 headers.Add ("pragma", "nocache,R=1,g");
271 Assert.IsTrue (headers.Accept.SequenceEqual (
273 new MediaTypeWithQualityHeaderValue ("audio/x"),
274 new MediaTypeWithQualityHeaderValue ("audio/y")
278 Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
280 new StringWithQualityHeaderValue ("str-v", 0.002),
281 new StringWithQualityHeaderValue ("achs")
285 Assert.IsTrue (headers.AcceptEncoding.SequenceEqual (
287 new StringWithQualityHeaderValue ("str-enc", 0.44),
288 new StringWithQualityHeaderValue ("aenc")
292 Assert.IsTrue (headers.AcceptLanguage.SequenceEqual (
294 new StringWithQualityHeaderValue ("str-lang", 0.41),
295 new StringWithQualityHeaderValue ("alan")
299 Assert.AreEqual (new AuthenticationHeaderValue ("sh-aut", "par"), headers.Authorization);
301 var cch = new CacheControlHeaderValue () {
302 MaxAge = TimeSpan.MaxValue,
305 Assert.AreEqual (cch, headers.CacheControl);
307 Assert.IsTrue (headers.Connection.SequenceEqual (
308 new string[] { "test-value", "close" }));
310 Assert.AreEqual (headers.Date, new DateTimeOffset (DateTime.Today));
312 Assert.IsTrue (headers.Expect.SequenceEqual (
314 new NameValueWithParametersHeaderValue ("en", "ev"),
315 new NameValueWithParametersHeaderValue ("100-continue"),
316 new NameValueWithParametersHeaderValue ("exp")
319 Assert.AreEqual (headers.From, "webmaster@w3.org");
321 Assert.IsTrue (headers.IfMatch.SequenceEqual (
322 new EntityTagHeaderValue[] {
323 new EntityTagHeaderValue ("\"tag\"", true),
324 new EntityTagHeaderValue ("\"v\"", false)
328 Assert.AreEqual (headers.IfModifiedSince, new DateTimeOffset (DateTime.Today));
329 Assert.IsTrue (headers.IfNoneMatch.SequenceEqual (new EntityTagHeaderValue[] { new EntityTagHeaderValue ("\"tag2\"", true), new EntityTagHeaderValue ("\"v2\"", false) }));
330 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.IfRange.Date);
331 Assert.AreEqual (headers.MaxForwards, 0x15b3);
332 Assert.AreEqual ("p", headers.ProxyAuthorization.Parameter);
333 Assert.AreEqual ("s", headers.ProxyAuthorization.Scheme);
334 Assert.AreEqual (5, headers.Range.Ranges.First ().From);
335 Assert.AreEqual (30, headers.Range.Ranges.First ().To);
336 Assert.AreEqual ("bytes", headers.Range.Unit);
337 Assert.AreEqual (headers.Referrer, new Uri ("http://xamarin.com"));
338 Assert.IsTrue (headers.TE.SequenceEqual (new TransferCodingWithQualityHeaderValue[] { new TransferCodingWithQualityHeaderValue ("TE", 0.3), new TransferCodingWithQualityHeaderValue ("0.8") }), "29");
339 Assert.IsTrue (headers.Trailer.SequenceEqual (
344 Assert.IsTrue (headers.TransferEncoding.SequenceEqual (
346 new TransferCodingHeaderValue ("tchv"),
347 new TransferCodingHeaderValue ("chunked"),
348 new TransferCodingHeaderValue ("ttt")
352 Assert.IsTrue (headers.Upgrade.SequenceEqual (
354 new ProductHeaderValue ("prod", "ver"),
355 new ProductHeaderValue ("uuu")
359 Assert.IsTrue (headers.UserAgent.SequenceEqual (
361 new ProductInfoHeaderValue ("(comment)"),
362 new ProductInfoHeaderValue ("uaua", null)
366 Assert.IsTrue (headers.Via.SequenceEqual (
368 new ViaHeaderValue ("protocol", "rec-by"),
369 new ViaHeaderValue ("prot", "v")
373 Assert.IsTrue (headers.Warning.SequenceEqual (
375 new WarningHeaderValue (5, "agent", "\"txt\""),
376 new WarningHeaderValue (4, "ww", "\"t\"")
380 Assert.IsTrue (headers.Pragma.SequenceEqual (
382 new NameValueHeaderValue ("name", "value"),
383 new NameValueHeaderValue ("nocache", null),
384 new NameValueHeaderValue ("R", "1"),
385 new NameValueHeaderValue ("g", null)
391 public void Headers_MultiValues ()
393 HttpRequestMessage message = new HttpRequestMessage ();
394 HttpRequestHeaders headers = message.Headers;
396 headers.Add ("Accept", "application/vnd.citrix.requesttokenresponse+xml, application/vnd.citrix.requesttokenchoices+xml");
397 headers.Add ("Accept-Charset", "aa ;Q=0,bb;Q=1");
398 headers.Add ("Expect", "x=1; v, y=5");
399 headers.Add ("If-Match", "\"a\",*, \"b\",*");
400 headers.Add ("user-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36");
402 Assert.AreEqual (2, headers.Accept.Count, "#1a");
403 Assert.IsTrue (headers.Accept.SequenceEqual (
405 new MediaTypeWithQualityHeaderValue ("application/vnd.citrix.requesttokenresponse+xml"),
406 new MediaTypeWithQualityHeaderValue ("application/vnd.citrix.requesttokenchoices+xml"),
410 Assert.AreEqual (2, headers.AcceptCharset.Count, "#2a");
411 Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
413 new StringWithQualityHeaderValue ("aa", 0),
414 new StringWithQualityHeaderValue ("bb", 1),
418 Assert.AreEqual (2, headers.Expect.Count, "#3a");
419 var expect_expected = new[] {
420 new NameValueWithParametersHeaderValue ("x", "1") {
422 new NameValueWithParametersHeaderValue ("y", "5"),
424 expect_expected [0].Parameters.Add (new NameValueHeaderValue ("v"));
425 Assert.IsTrue (headers.Expect.SequenceEqual (
429 Assert.AreEqual (4, headers.IfMatch.Count, "#4a");
430 Assert.IsTrue (headers.IfMatch.SequenceEqual (
432 new EntityTagHeaderValue ("\"a\""),
433 EntityTagHeaderValue.Any,
434 new EntityTagHeaderValue ("\"b\""),
435 EntityTagHeaderValue.Any
439 Assert.AreEqual (6, headers.UserAgent.Count, "#10a");
441 Assert.IsTrue (headers.UserAgent.SequenceEqual (
443 new ProductInfoHeaderValue ("Mozilla", "5.0"),
444 new ProductInfoHeaderValue ("(Macintosh; Intel Mac OS X 10_8_4)"),
445 new ProductInfoHeaderValue ("AppleWebKit", "537.36"),
446 new ProductInfoHeaderValue ("(KHTML, like Gecko)"),
447 new ProductInfoHeaderValue ("Chrome", "29.0.1547.62"),
448 new ProductInfoHeaderValue ("Safari", "537.36")
454 public void Headers_UserAgentExtra ()
456 HttpRequestMessage message = new HttpRequestMessage ();
457 var headers = message.Headers;
459 headers.Add ("User-Agent", "MonoDevelop (Unix 3.13.0; amd64; en-US; Octokit 0.3.4)");
461 var se = headers.UserAgent.SequenceEqual (
463 new ProductInfoHeaderValue ("MonoDevelop", null),
464 new ProductInfoHeaderValue ("(Unix 3.13.0; amd64; en-US; Octokit 0.3.4)")
469 public void Header_BaseImplementation ()
471 HttpRequestMessage message = new HttpRequestMessage ();
472 HttpRequestHeaders headers = message.Headers;
474 headers.Add ("a", "a-value");
475 headers.Add ("b", new List<string> { "v1", "v2" });
476 headers.Add ("c", null as string);
477 headers.Add ("d", new string[0]);
479 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#0");
481 Assert.IsFalse (headers.Contains ("nn"), "#1a");
482 Assert.IsTrue (headers.Contains ("b"), "#1b");
484 var values = headers.GetValues ("b").ToList ();
485 Assert.AreEqual ("v1", values[0], "#2a");
486 Assert.AreEqual ("v2", values[1], "#2b");
488 Assert.IsFalse (headers.Remove ("p"), "#3a");
489 Assert.IsTrue (headers.Remove ("b"), "#3b");
490 Assert.IsFalse (headers.Contains ("b"), "#3b-c");
492 IEnumerable<string> values2;
493 Assert.IsTrue (headers.TryGetValues ("c", out values2));
494 values = values2.ToList ();
495 Assert.AreEqual ("", values[0], "#4a");
498 foreach (var i in headers) {
502 Assert.AreEqual (3, counter, "#5");
506 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
507 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#55");
509 values = headers.GetValues ("accept").ToList ();
510 Assert.AreEqual (2, values.Count, "#6");
511 Assert.AreEqual ("audio/x", values[0], "#6a");
512 Assert.AreEqual ("audio", values[1], "#6b");
513 Assert.AreEqual (1, headers.Accept.Count, "#6c");
517 Assert.IsTrue (headers.TryAddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" }), "#70");
518 values = headers.GetValues ("from").ToList ();
520 Assert.AreEqual (2, values.Count, "#7");
521 Assert.AreEqual ("a@a.com", values[0], "#7a");
522 Assert.AreEqual ("ssss@oo.com", values[1], "#7b");
523 Assert.AreEqual ("a@a.com", headers.From, "#7c");
527 Assert.IsTrue (headers.TryAddWithoutValidation ("Date", "wrong date"), "#8-0");
528 var value = headers.Date;
529 Assert.IsNull (headers.Date, "#8");
533 public void Headers_Invalid ()
535 HttpRequestMessage message = new HttpRequestMessage ();
536 HttpRequestHeaders headers = message.Headers;
539 headers.Add ("Allow", "");
541 } catch (InvalidOperationException) {
545 headers.Add (null, "");
547 } catch (ArgumentException) {
551 headers.Add ("mm", null as IEnumerable<string>);
553 } catch (ArgumentNullException) {
557 headers.Add ("accept", "audio");
559 } catch (FormatException) {
562 Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3"); ;
564 Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
567 headers.Contains (null);
569 } catch (ArgumentException) {
573 headers.GetValues (null);
575 } catch (ArgumentException) {
579 headers.GetValues ("bbbb");
581 } catch (InvalidOperationException) {
585 headers.Add ("from", new[] { "a@a.com", "ssss@oo.com" });
587 } catch (FormatException) {
590 Assert.IsTrue (headers.TryAddWithoutValidation ("from", "a@a.com"), "#7-0");
592 headers.Add ("from", "valid@w3.org");
594 } catch (FormatException) {
599 public void Headers_Response ()
601 HttpRequestMessage message = new HttpRequestMessage ();
602 HttpRequestHeaders headers = message.Headers;
604 headers.Add ("Age", "vv");
605 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
608 headers.TryAddWithoutValidation ("Age", "vv");
609 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
611 // .NET encloses the "Age: vv" with two whitespaces.
612 var normalized = Regex.Replace (message.ToString (), @"\s", "");
613 Assert.AreEqual ("Method:GET,RequestUri:'<null>',Version:1.1,Content:<null>,Headers:{Age:vv}", normalized, "#3");
617 public void Headers_ExpectContinue ()
619 HttpRequestMessage message = new HttpRequestMessage ();
620 HttpRequestHeaders headers = message.Headers;
621 Assert.IsNull (headers.ExpectContinue, "#1");
623 headers.ExpectContinue = false;
624 Assert.IsFalse (headers.ExpectContinue.Value, "#2");
628 headers.ExpectContinue = true;
629 headers.ExpectContinue = true;
630 headers.ExpectContinue = true;
631 headers.ExpectContinue = true;
632 Assert.IsTrue (headers.ExpectContinue.Value, "#3");
633 Assert.AreEqual (1, headers.GetValues ("expect").ToList ().Count, "#4");
636 headers.Expect.Add (new NameValueWithParametersHeaderValue ("100-conTinuE"));
637 Assert.IsTrue (headers.ExpectContinue.Value, "#5");
641 public void Headers_ConnectionClose ()
643 HttpRequestMessage message = new HttpRequestMessage ();
644 HttpRequestHeaders headers = message.Headers;
645 Assert.IsNull (headers.ConnectionClose, "#1");
647 headers.ConnectionClose = false;
648 Assert.IsFalse (headers.ConnectionClose.Value, "#2");
652 headers.ConnectionClose = true;
653 Assert.IsTrue (headers.ConnectionClose.Value, "#3");
656 headers.Connection.Add ("Close");
657 Assert.IsTrue (headers.ConnectionClose.Value, "#4");
661 public void Headers_From_Invalid ()
663 HttpRequestMessage message = new HttpRequestMessage ();
664 HttpRequestHeaders headers = message.Headers;
670 } catch (FormatException) {
673 headers.From = "test";
675 } catch (FormatException) {
680 public void Headers_TransferEncodingChunked ()
682 HttpRequestMessage message = new HttpRequestMessage ();
683 HttpRequestHeaders headers = message.Headers;
684 Assert.IsNull (headers.TransferEncodingChunked, "#1");
686 headers.TransferEncodingChunked = false;
687 Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
691 headers.TransferEncodingChunked = true;
692 Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
693 Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
697 public void Properties ()
699 var c = new HttpRequestMessage ();
701 c.Method = HttpMethod.Post;
702 c.Properties.Add ("a", "test");
704 c.Version = HttpVersion.Version10;
706 Assert.IsNull (c.Content, "#1");
707 Assert.AreEqual (HttpMethod.Post, c.Method, "#2");
708 Assert.AreEqual ("test", c.Properties["a"], "#3");
709 Assert.IsNull (c.RequestUri, "#4");
710 Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
714 public void Properties_Invalid ()
716 var c = new HttpRequestMessage ();
720 } catch (ArgumentNullException) {
724 c.RequestUri = new Uri ("ftp://xamarin.com");
726 } catch (ArgumentException) {
732 } catch (ArgumentNullException) {