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_RelativeUriString ()
105 var client = new HttpClient ();
106 client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
107 var req = new HttpRequestMessage (HttpMethod.Get, "Computer");
108 // HttpRequestMessage does not rewrite it here.
109 Assert.IsFalse (req.RequestUri.IsAbsoluteUri);
113 public void Headers ()
115 HttpRequestMessage message = new HttpRequestMessage ();
116 HttpRequestHeaders headers = message.Headers;
118 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
119 headers.AcceptCharset.Add (new StringWithQualityHeaderValue ("str-v", 0.002));
120 headers.AcceptEncoding.Add (new StringWithQualityHeaderValue ("str-enc", 0.44));
121 headers.AcceptLanguage.Add (new StringWithQualityHeaderValue ("str-lang", 0.41));
122 headers.Authorization = new AuthenticationHeaderValue ("sh-aut", "par");
123 headers.CacheControl = new CacheControlHeaderValue () {
124 MaxAge = TimeSpan.MaxValue
126 headers.Connection.Add ("test-value");
127 headers.ConnectionClose = true;
128 headers.Date = new DateTimeOffset (DateTime.Today);
129 headers.Expect.Add (new NameValueWithParametersHeaderValue ("en", "ev"));
130 headers.ExpectContinue = true;
131 headers.From = "webmaster@w3.org";
132 headers.Host = "host";
133 headers.IfMatch.Add (new EntityTagHeaderValue ("\"tag\"", true));
134 headers.IfModifiedSince = new DateTimeOffset (DateTime.Today);
135 headers.IfNoneMatch.Add (new EntityTagHeaderValue ("\"tag2\"", true));
136 headers.IfRange = new RangeConditionHeaderValue (new DateTimeOffset (DateTime.Today));
137 headers.IfUnmodifiedSince = new DateTimeOffset? (DateTimeOffset.Now);
138 headers.MaxForwards = 0x15b3;
139 headers.Pragma.Add (new NameValueHeaderValue ("name", "value"));
140 headers.ProxyAuthorization = new AuthenticationHeaderValue ("s", "p");
141 headers.Range = new RangeHeaderValue (5L, 30L);
142 headers.Referrer = new Uri ("http://xamarin.com");
143 headers.TE.Add (new TransferCodingWithQualityHeaderValue ("TE", 0.3));
144 headers.Trailer.Add ("value");
145 headers.TransferEncoding.Add (new TransferCodingHeaderValue ("tchv"));
146 headers.TransferEncodingChunked = true;
147 headers.Upgrade.Add (new ProductHeaderValue ("prod", "ver"));
148 headers.UserAgent.Add (new ProductInfoHeaderValue ("(comment)"));
149 headers.Via.Add (new ViaHeaderValue ("protocol", "rec-by"));
150 headers.Warning.Add (new WarningHeaderValue (5, "agent", "\"txt\""));
153 headers.Add ("authorization", "");
154 Assert.Fail ("Authorization");
155 } catch (FormatException) {
159 headers.Add ("connection", "extra ß ");
160 Assert.Fail ("Date");
161 } catch (FormatException) {
165 headers.Add ("date", "");
166 Assert.Fail ("Date");
167 } catch (FormatException) {
171 headers.Add ("from", "a@w3.org");
172 Assert.Fail ("From");
173 } catch (FormatException) {
177 headers.Add ("hOst", "host");
178 Assert.Fail ("Host");
179 } catch (FormatException) {
183 headers.Add ("if-modified-since", "");
184 Assert.Fail ("if-modified-since");
185 } catch (FormatException) {
189 headers.Add ("if-range", "");
190 Assert.Fail ("if-range");
191 } catch (FormatException) {
195 headers.Add ("if-unmodified-since", "");
196 Assert.Fail ("if-unmodified-since");
197 } catch (FormatException) {
201 headers.Add ("max-forwards", "");
202 Assert.Fail ("max-forwards");
203 } catch (FormatException) {
207 headers.Add ("proxy-authorization", "");
208 Assert.Fail ("proxy-authorization");
209 } catch (FormatException) {
213 headers.Add ("range", "");
214 } catch (FormatException) {
218 headers.Add ("referer", "");
219 Assert.Fail ("referer");
220 } catch (FormatException) {
224 headers.Add ("pragma", "nocache,RequestID=1,g=");
225 Assert.Fail ("pragma");
226 } catch (FormatException) {
229 headers.Add ("accept", "audio/y");
230 headers.Add ("accept-charset", "achs");
231 headers.Add ("accept-encoding", "aenc");
232 headers.Add ("accept-language", "alan");
233 headers.Add ("expect", "exp");
234 headers.Add ("if-match", "\"v\"");
235 headers.Add ("if-none-match", "\"v2\"");
236 headers.Add ("TE", "0.8");
237 headers.Add ("trailer", "value2");
238 headers.Add ("transfer-encoding", "ttt");
239 headers.Add ("upgrade", "uuu");
240 headers.Add ("user-agent", "uaua");
241 headers.Add ("via", "prot v");
242 headers.Add ("warning", "4 ww \"t\"");
243 headers.Add ("pragma", "nocache,R=1,g");
245 Assert.IsTrue (headers.Accept.SequenceEqual (
247 new MediaTypeWithQualityHeaderValue ("audio/x"),
248 new MediaTypeWithQualityHeaderValue ("audio/y")
252 Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
254 new StringWithQualityHeaderValue ("str-v", 0.002),
255 new StringWithQualityHeaderValue ("achs")
259 Assert.IsTrue (headers.AcceptEncoding.SequenceEqual (
261 new StringWithQualityHeaderValue ("str-enc", 0.44),
262 new StringWithQualityHeaderValue ("aenc")
266 Assert.IsTrue (headers.AcceptLanguage.SequenceEqual (
268 new StringWithQualityHeaderValue ("str-lang", 0.41),
269 new StringWithQualityHeaderValue ("alan")
273 Assert.AreEqual (new AuthenticationHeaderValue ("sh-aut", "par"), headers.Authorization);
275 var cch = new CacheControlHeaderValue () {
276 MaxAge = TimeSpan.MaxValue,
279 Assert.AreEqual (cch, headers.CacheControl);
281 Assert.IsTrue (headers.Connection.SequenceEqual (
282 new string[] { "test-value", "close" }));
284 Assert.AreEqual (headers.Date, new DateTimeOffset (DateTime.Today));
286 Assert.IsTrue (headers.Expect.SequenceEqual (
288 new NameValueWithParametersHeaderValue ("en", "ev"),
289 new NameValueWithParametersHeaderValue ("100-continue"),
290 new NameValueWithParametersHeaderValue ("exp")
293 Assert.AreEqual (headers.From, "webmaster@w3.org");
295 Assert.IsTrue (headers.IfMatch.SequenceEqual (
296 new EntityTagHeaderValue[] {
297 new EntityTagHeaderValue ("\"tag\"", true),
298 new EntityTagHeaderValue ("\"v\"", false)
302 Assert.AreEqual (headers.IfModifiedSince, new DateTimeOffset (DateTime.Today));
303 Assert.IsTrue (headers.IfNoneMatch.SequenceEqual (new EntityTagHeaderValue[] { new EntityTagHeaderValue ("\"tag2\"", true), new EntityTagHeaderValue ("\"v2\"", false) }));
304 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.IfRange.Date);
305 Assert.AreEqual (headers.MaxForwards, 0x15b3);
306 Assert.AreEqual ("p", headers.ProxyAuthorization.Parameter);
307 Assert.AreEqual ("s", headers.ProxyAuthorization.Scheme);
308 Assert.AreEqual (5, headers.Range.Ranges.First ().From);
309 Assert.AreEqual (30, headers.Range.Ranges.First ().To);
310 Assert.AreEqual ("bytes", headers.Range.Unit);
311 Assert.AreEqual (headers.Referrer, new Uri ("http://xamarin.com"));
312 Assert.IsTrue (headers.TE.SequenceEqual (new TransferCodingWithQualityHeaderValue[] { new TransferCodingWithQualityHeaderValue ("TE", 0.3), new TransferCodingWithQualityHeaderValue ("0.8") }), "29");
313 Assert.IsTrue (headers.Trailer.SequenceEqual (
318 Assert.IsTrue (headers.TransferEncoding.SequenceEqual (
320 new TransferCodingHeaderValue ("tchv"),
321 new TransferCodingHeaderValue ("chunked"),
322 new TransferCodingHeaderValue ("ttt")
326 Assert.IsTrue (headers.Upgrade.SequenceEqual (
328 new ProductHeaderValue ("prod", "ver"),
329 new ProductHeaderValue ("uuu")
333 Assert.IsTrue (headers.UserAgent.SequenceEqual (
335 new ProductInfoHeaderValue ("(comment)"),
336 new ProductInfoHeaderValue ("uaua", null)
340 Assert.IsTrue (headers.Via.SequenceEqual (
342 new ViaHeaderValue ("protocol", "rec-by"),
343 new ViaHeaderValue ("prot", "v")
347 Assert.IsTrue (headers.Warning.SequenceEqual (
349 new WarningHeaderValue (5, "agent", "\"txt\""),
350 new WarningHeaderValue (4, "ww", "\"t\"")
354 Assert.IsTrue (headers.Pragma.SequenceEqual (
356 new NameValueHeaderValue ("name", "value"),
357 new NameValueHeaderValue ("nocache", null),
358 new NameValueHeaderValue ("R", "1"),
359 new NameValueHeaderValue ("g", null)
365 public void Headers_MultiValues ()
367 HttpRequestMessage message = new HttpRequestMessage ();
368 HttpRequestHeaders headers = message.Headers;
370 headers.Add ("Accept", "application/vnd.citrix.requesttokenresponse+xml, application/vnd.citrix.requesttokenchoices+xml");
371 headers.Add ("Accept-Charset", "aa ;Q=0,bb;Q=1");
372 headers.Add ("Expect", "x=1; v, y=5");
373 headers.Add ("If-Match", "\"a\",*, \"b\",*");
374 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");
376 Assert.AreEqual (2, headers.Accept.Count, "#1a");
377 Assert.IsTrue (headers.Accept.SequenceEqual (
379 new MediaTypeWithQualityHeaderValue ("application/vnd.citrix.requesttokenresponse+xml"),
380 new MediaTypeWithQualityHeaderValue ("application/vnd.citrix.requesttokenchoices+xml"),
384 Assert.AreEqual (2, headers.AcceptCharset.Count, "#2a");
385 Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
387 new StringWithQualityHeaderValue ("aa", 0),
388 new StringWithQualityHeaderValue ("bb", 1),
392 Assert.AreEqual (2, headers.Expect.Count, "#3a");
393 var expect_expected = new[] {
394 new NameValueWithParametersHeaderValue ("x", "1") {
396 new NameValueWithParametersHeaderValue ("y", "5"),
398 expect_expected [0].Parameters.Add (new NameValueHeaderValue ("v"));
399 Assert.IsTrue (headers.Expect.SequenceEqual (
403 Assert.AreEqual (4, headers.IfMatch.Count, "#4a");
404 Assert.IsTrue (headers.IfMatch.SequenceEqual (
406 new EntityTagHeaderValue ("\"a\""),
407 EntityTagHeaderValue.Any,
408 new EntityTagHeaderValue ("\"b\""),
409 EntityTagHeaderValue.Any
413 Assert.AreEqual (6, headers.UserAgent.Count, "#10a");
415 Assert.IsTrue (headers.UserAgent.SequenceEqual (
417 new ProductInfoHeaderValue ("Mozilla", "5.0"),
418 new ProductInfoHeaderValue ("(Macintosh; Intel Mac OS X 10_8_4)"),
419 new ProductInfoHeaderValue ("AppleWebKit", "537.36"),
420 new ProductInfoHeaderValue ("(KHTML, like Gecko)"),
421 new ProductInfoHeaderValue ("Chrome", "29.0.1547.62"),
422 new ProductInfoHeaderValue ("Safari", "537.36")
428 public void Headers_UserAgentExtra ()
430 HttpRequestMessage message = new HttpRequestMessage ();
431 var headers = message.Headers;
433 headers.Add ("User-Agent", "MonoDevelop (Unix 3.13.0; amd64; en-US; Octokit 0.3.4)");
435 var se = headers.UserAgent.SequenceEqual (
437 new ProductInfoHeaderValue ("MonoDevelop", null),
438 new ProductInfoHeaderValue ("(Unix 3.13.0; amd64; en-US; Octokit 0.3.4)")
443 public void Header_BaseImplementation ()
445 HttpRequestMessage message = new HttpRequestMessage ();
446 HttpRequestHeaders headers = message.Headers;
448 headers.Add ("a", "a-value");
449 headers.Add ("b", new List<string> { "v1", "v2" });
450 headers.Add ("c", null as string);
451 headers.Add ("d", new string[0]);
453 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#0");
455 Assert.IsFalse (headers.Contains ("nn"), "#1a");
456 Assert.IsTrue (headers.Contains ("b"), "#1b");
458 var values = headers.GetValues ("b").ToList ();
459 Assert.AreEqual ("v1", values[0], "#2a");
460 Assert.AreEqual ("v2", values[1], "#2b");
462 Assert.IsFalse (headers.Remove ("p"), "#3a");
463 Assert.IsTrue (headers.Remove ("b"), "#3b");
464 Assert.IsFalse (headers.Contains ("b"), "#3b-c");
466 IEnumerable<string> values2;
467 Assert.IsTrue (headers.TryGetValues ("c", out values2));
468 values = values2.ToList ();
469 Assert.AreEqual ("", values[0], "#4a");
472 foreach (var i in headers) {
476 Assert.AreEqual (3, counter, "#5");
480 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
481 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#55");
483 values = headers.GetValues ("accept").ToList ();
484 Assert.AreEqual (2, values.Count, "#6");
485 Assert.AreEqual ("audio/x", values[0], "#6a");
486 Assert.AreEqual ("audio", values[1], "#6b");
487 Assert.AreEqual (1, headers.Accept.Count, "#6c");
491 Assert.IsTrue (headers.TryAddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" }), "#70");
492 values = headers.GetValues ("from").ToList ();
494 Assert.AreEqual (2, values.Count, "#7");
495 Assert.AreEqual ("a@a.com", values[0], "#7a");
496 Assert.AreEqual ("ssss@oo.com", values[1], "#7b");
497 Assert.AreEqual ("a@a.com", headers.From, "#7c");
501 Assert.IsTrue (headers.TryAddWithoutValidation ("Date", "wrong date"), "#8-0");
502 var value = headers.Date;
503 Assert.IsNull (headers.Date, "#8");
507 public void Headers_Invalid ()
509 HttpRequestMessage message = new HttpRequestMessage ();
510 HttpRequestHeaders headers = message.Headers;
513 headers.Add ("Allow", "");
515 } catch (InvalidOperationException) {
519 headers.Add (null, "");
521 } catch (ArgumentException) {
525 headers.Add ("mm", null as IEnumerable<string>);
527 } catch (ArgumentNullException) {
531 headers.Add ("accept", "audio");
533 } catch (FormatException) {
536 Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3"); ;
538 Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
541 headers.Contains (null);
543 } catch (ArgumentException) {
547 headers.GetValues (null);
549 } catch (ArgumentException) {
553 headers.GetValues ("bbbb");
555 } catch (InvalidOperationException) {
559 headers.Add ("from", new[] { "a@a.com", "ssss@oo.com" });
561 } catch (FormatException) {
564 Assert.IsTrue (headers.TryAddWithoutValidation ("from", "a@a.com"), "#7-0");
566 headers.Add ("from", "valid@w3.org");
568 } catch (FormatException) {
573 public void Headers_Response ()
575 HttpRequestMessage message = new HttpRequestMessage ();
576 HttpRequestHeaders headers = message.Headers;
578 headers.Add ("Age", "vv");
579 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
582 headers.TryAddWithoutValidation ("Age", "vv");
583 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
585 // .NET encloses the "Age: vv" with two whitespaces.
586 var normalized = Regex.Replace (message.ToString (), @"\s", "");
587 Assert.AreEqual ("Method:GET,RequestUri:'<null>',Version:1.1,Content:<null>,Headers:{Age:vv}", normalized, "#3");
591 public void Headers_ExpectContinue ()
593 HttpRequestMessage message = new HttpRequestMessage ();
594 HttpRequestHeaders headers = message.Headers;
595 Assert.IsNull (headers.ExpectContinue, "#1");
597 headers.ExpectContinue = false;
598 Assert.IsFalse (headers.ExpectContinue.Value, "#2");
602 headers.ExpectContinue = true;
603 headers.ExpectContinue = true;
604 headers.ExpectContinue = true;
605 headers.ExpectContinue = true;
606 Assert.IsTrue (headers.ExpectContinue.Value, "#3");
607 Assert.AreEqual (1, headers.GetValues ("expect").ToList ().Count, "#4");
610 headers.Expect.Add (new NameValueWithParametersHeaderValue ("100-conTinuE"));
611 Assert.IsTrue (headers.ExpectContinue.Value, "#5");
615 public void Headers_ConnectionClose ()
617 HttpRequestMessage message = new HttpRequestMessage ();
618 HttpRequestHeaders headers = message.Headers;
619 Assert.IsNull (headers.ConnectionClose, "#1");
621 headers.ConnectionClose = false;
622 Assert.IsFalse (headers.ConnectionClose.Value, "#2");
626 headers.ConnectionClose = true;
627 Assert.IsTrue (headers.ConnectionClose.Value, "#3");
630 headers.Connection.Add ("Close");
631 Assert.IsTrue (headers.ConnectionClose.Value, "#4");
635 public void Headers_From_Invalid ()
637 HttpRequestMessage message = new HttpRequestMessage ();
638 HttpRequestHeaders headers = message.Headers;
644 } catch (FormatException) {
647 headers.From = "test";
649 } catch (FormatException) {
654 public void Headers_TransferEncodingChunked ()
656 HttpRequestMessage message = new HttpRequestMessage ();
657 HttpRequestHeaders headers = message.Headers;
658 Assert.IsNull (headers.TransferEncodingChunked, "#1");
660 headers.TransferEncodingChunked = false;
661 Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
665 headers.TransferEncodingChunked = true;
666 Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
667 Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
671 public void Properties ()
673 var c = new HttpRequestMessage ();
675 c.Method = HttpMethod.Post;
676 c.Properties.Add ("a", "test");
678 c.Version = HttpVersion.Version10;
680 Assert.IsNull (c.Content, "#1");
681 Assert.AreEqual (HttpMethod.Post, c.Method, "#2");
682 Assert.AreEqual ("test", c.Properties["a"], "#3");
683 Assert.IsNull (c.RequestUri, "#4");
684 Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
688 public void Properties_Invalid ()
690 var c = new HttpRequestMessage ();
694 } catch (ArgumentNullException) {
698 c.RequestUri = new Uri ("ftp://xamarin.com");
700 } catch (ArgumentException) {
706 } catch (ArgumentNullException) {