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 Header_BaseImplementation ()
430 HttpRequestMessage message = new HttpRequestMessage ();
431 HttpRequestHeaders headers = message.Headers;
433 headers.Add ("a", "a-value");
434 headers.Add ("b", new List<string> { "v1", "v2" });
435 headers.Add ("c", null as string);
436 headers.Add ("d", new string[0]);
438 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#0");
440 Assert.IsFalse (headers.Contains ("nn"), "#1a");
441 Assert.IsTrue (headers.Contains ("b"), "#1b");
443 var values = headers.GetValues ("b").ToList ();
444 Assert.AreEqual ("v1", values[0], "#2a");
445 Assert.AreEqual ("v2", values[1], "#2b");
447 Assert.IsFalse (headers.Remove ("p"), "#3a");
448 Assert.IsTrue (headers.Remove ("b"), "#3b");
449 Assert.IsFalse (headers.Contains ("b"), "#3b-c");
451 IEnumerable<string> values2;
452 Assert.IsTrue (headers.TryGetValues ("c", out values2));
453 values = values2.ToList ();
454 Assert.AreEqual ("", values[0], "#4a");
457 foreach (var i in headers) {
461 Assert.AreEqual (3, counter, "#5");
465 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
466 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#55");
468 values = headers.GetValues ("accept").ToList ();
469 Assert.AreEqual (2, values.Count, "#6");
470 Assert.AreEqual ("audio/x", values[0], "#6a");
471 Assert.AreEqual ("audio", values[1], "#6b");
472 Assert.AreEqual (1, headers.Accept.Count, "#6c");
476 Assert.IsTrue (headers.TryAddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" }), "#70");
477 values = headers.GetValues ("from").ToList ();
479 Assert.AreEqual (2, values.Count, "#7");
480 Assert.AreEqual ("a@a.com", values[0], "#7a");
481 Assert.AreEqual ("ssss@oo.com", values[1], "#7b");
482 Assert.AreEqual ("a@a.com", headers.From, "#7c");
486 Assert.IsTrue (headers.TryAddWithoutValidation ("Date", "wrong date"), "#8-0");
487 var value = headers.Date;
488 Assert.IsNull (headers.Date, "#8");
492 public void Headers_Invalid ()
494 HttpRequestMessage message = new HttpRequestMessage ();
495 HttpRequestHeaders headers = message.Headers;
498 headers.Add ("Allow", "");
500 } catch (InvalidOperationException) {
504 headers.Add (null, "");
506 } catch (ArgumentException) {
510 headers.Add ("mm", null as IEnumerable<string>);
512 } catch (ArgumentNullException) {
516 headers.Add ("accept", "audio");
518 } catch (FormatException) {
521 Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3"); ;
523 Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
526 headers.Contains (null);
528 } catch (ArgumentException) {
532 headers.GetValues (null);
534 } catch (ArgumentException) {
538 headers.GetValues ("bbbb");
540 } catch (InvalidOperationException) {
544 headers.Add ("from", new[] { "a@a.com", "ssss@oo.com" });
546 } catch (FormatException) {
549 Assert.IsTrue (headers.TryAddWithoutValidation ("from", "a@a.com"), "#7-0");
551 headers.Add ("from", "valid@w3.org");
553 } catch (FormatException) {
558 public void Headers_Response ()
560 HttpRequestMessage message = new HttpRequestMessage ();
561 HttpRequestHeaders headers = message.Headers;
563 headers.Add ("Age", "vv");
564 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
567 headers.TryAddWithoutValidation ("Age", "vv");
568 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
570 // .NET encloses the "Age: vv" with two whitespaces.
571 var normalized = Regex.Replace (message.ToString (), @"\s", "");
572 Assert.AreEqual ("Method:GET,RequestUri:'<null>',Version:1.1,Content:<null>,Headers:{Age:vv}", normalized, "#3");
576 public void Headers_ExpectContinue ()
578 HttpRequestMessage message = new HttpRequestMessage ();
579 HttpRequestHeaders headers = message.Headers;
580 Assert.IsNull (headers.ExpectContinue, "#1");
582 headers.ExpectContinue = false;
583 Assert.IsFalse (headers.ExpectContinue.Value, "#2");
587 headers.ExpectContinue = true;
588 headers.ExpectContinue = true;
589 headers.ExpectContinue = true;
590 headers.ExpectContinue = true;
591 Assert.IsTrue (headers.ExpectContinue.Value, "#3");
592 Assert.AreEqual (1, headers.GetValues ("expect").ToList ().Count, "#4");
595 headers.Expect.Add (new NameValueWithParametersHeaderValue ("100-conTinuE"));
596 Assert.IsTrue (headers.ExpectContinue.Value, "#5");
600 public void Headers_ConnectionClose ()
602 HttpRequestMessage message = new HttpRequestMessage ();
603 HttpRequestHeaders headers = message.Headers;
604 Assert.IsNull (headers.ConnectionClose, "#1");
606 headers.ConnectionClose = false;
607 Assert.IsFalse (headers.ConnectionClose.Value, "#2");
611 headers.ConnectionClose = true;
612 Assert.IsTrue (headers.ConnectionClose.Value, "#3");
615 headers.Connection.Add ("Close");
616 Assert.IsTrue (headers.ConnectionClose.Value, "#4");
620 public void Headers_From_Invalid ()
622 HttpRequestMessage message = new HttpRequestMessage ();
623 HttpRequestHeaders headers = message.Headers;
629 } catch (FormatException) {
632 headers.From = "test";
634 } catch (FormatException) {
639 public void Headers_TransferEncodingChunked ()
641 HttpRequestMessage message = new HttpRequestMessage ();
642 HttpRequestHeaders headers = message.Headers;
643 Assert.IsNull (headers.TransferEncodingChunked, "#1");
645 headers.TransferEncodingChunked = false;
646 Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
650 headers.TransferEncodingChunked = true;
651 Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
652 Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
656 public void Properties ()
658 var c = new HttpRequestMessage ();
660 c.Method = HttpMethod.Post;
661 c.Properties.Add ("a", "test");
663 c.Version = HttpVersion.Version10;
665 Assert.IsNull (c.Content, "#1");
666 Assert.AreEqual (HttpMethod.Post, c.Method, "#2");
667 Assert.AreEqual ("test", c.Properties["a"], "#3");
668 Assert.IsNull (c.RequestUri, "#4");
669 Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
673 public void Properties_Invalid ()
675 var c = new HttpRequestMessage ();
679 } catch (ArgumentNullException) {
683 c.RequestUri = new Uri ("ftp://xamarin.com");
685 } catch (ArgumentException) {
691 } catch (ArgumentNullException) {