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");
64 Assert.AreEqual ("Method: GET, RequestUri: '<null>', Version: 1.1, Content: <null>, Headers:\r\n{\r\n}", m.ToString (), "#7");
68 public void Ctor_Uri ()
70 var c = new HttpRequestMessage (HttpMethod.Get, new Uri ("http://xamarin.com"));
71 Assert.AreEqual ("http://xamarin.com/", c.RequestUri.AbsoluteUri, "#1");
73 c = new HttpRequestMessage (HttpMethod.Get, new Uri ("https://xamarin.com"));
74 Assert.AreEqual ("https://xamarin.com/", c.RequestUri.AbsoluteUri, "#2");
76 c = new HttpRequestMessage (HttpMethod.Get, new Uri ("HTTP://xamarin.com:8080"));
77 Assert.AreEqual ("http://xamarin.com:8080/", c.RequestUri.AbsoluteUri, "#3");
79 var a = Uri.UriSchemeHttps;
80 var b = new Uri ("http://xamarin.com");
83 new HttpRequestMessage (HttpMethod.Get, new Uri ("ftp://xamarin.com"));
85 } catch (ArgumentException) {
90 public void Ctor_RelativeUri ()
92 var client = new HttpClient ();
93 client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
94 var uri = new Uri ("Computer", UriKind.Relative);
95 var req = new HttpRequestMessage (HttpMethod.Get, uri);
96 // HttpRequestMessage does not rewrite it here.
97 Assert.AreEqual (req.RequestUri, uri);
101 public void Ctor_RelativeUriString ()
103 var client = new HttpClient ();
104 client.BaseAddress = new Uri ("http://en.wikipedia.org/wiki/");
105 var req = new HttpRequestMessage (HttpMethod.Get, "Computer");
106 // HttpRequestMessage does not rewrite it here.
107 Assert.IsFalse (req.RequestUri.IsAbsoluteUri);
111 public void Headers ()
113 HttpRequestMessage message = new HttpRequestMessage ();
114 HttpRequestHeaders headers = message.Headers;
116 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
117 headers.AcceptCharset.Add (new StringWithQualityHeaderValue ("str-v", 0.002));
118 headers.AcceptEncoding.Add (new StringWithQualityHeaderValue ("str-enc", 0.44));
119 headers.AcceptLanguage.Add (new StringWithQualityHeaderValue ("str-lang", 0.41));
120 headers.Authorization = new AuthenticationHeaderValue ("sh-aut", "par");
121 headers.CacheControl = new CacheControlHeaderValue () {
122 MaxAge = TimeSpan.MaxValue
124 headers.Connection.Add ("test-value");
125 headers.ConnectionClose = true;
126 headers.Date = new DateTimeOffset (DateTime.Today);
127 headers.Expect.Add (new NameValueWithParametersHeaderValue ("en", "ev"));
128 headers.ExpectContinue = true;
129 headers.From = "webmaster@w3.org";
130 headers.Host = "host";
131 headers.IfMatch.Add (new EntityTagHeaderValue ("\"tag\"", true));
132 headers.IfModifiedSince = new DateTimeOffset (DateTime.Today);
133 headers.IfNoneMatch.Add (new EntityTagHeaderValue ("\"tag2\"", true));
134 headers.IfRange = new RangeConditionHeaderValue (new DateTimeOffset (DateTime.Today));
135 headers.IfUnmodifiedSince = new DateTimeOffset? (DateTimeOffset.Now);
136 headers.MaxForwards = 0x15b3;
137 headers.Pragma.Add (new NameValueHeaderValue ("name", "value"));
138 headers.ProxyAuthorization = new AuthenticationHeaderValue ("s", "p");
139 headers.Range = new RangeHeaderValue (5L, 30L);
140 headers.Referrer = new Uri ("http://xamarin.com");
141 headers.TE.Add (new TransferCodingWithQualityHeaderValue ("TE", 0.3));
142 headers.Trailer.Add ("value");
143 headers.TransferEncoding.Add (new TransferCodingHeaderValue ("tchv"));
144 headers.TransferEncodingChunked = true;
145 headers.Upgrade.Add (new ProductHeaderValue ("prod", "ver"));
146 headers.UserAgent.Add (new ProductInfoHeaderValue ("(comment)"));
147 headers.Via.Add (new ViaHeaderValue ("protocol", "rec-by"));
148 headers.Warning.Add (new WarningHeaderValue (5, "agent", "\"txt\""));
151 headers.Add ("authorization", "");
152 Assert.Fail ("Authorization");
153 } catch (FormatException) {
157 headers.Add ("connection", "extra ß ");
158 Assert.Fail ("Date");
159 } catch (FormatException) {
163 headers.Add ("date", "");
164 Assert.Fail ("Date");
165 } catch (FormatException) {
169 headers.Add ("from", "a@w3.org");
170 Assert.Fail ("From");
171 } catch (FormatException) {
175 headers.Add ("hOst", "host");
176 Assert.Fail ("Host");
177 } catch (FormatException) {
181 headers.Add ("if-modified-since", "");
182 Assert.Fail ("if-modified-since");
183 } catch (FormatException) {
187 headers.Add ("if-range", "");
188 Assert.Fail ("if-range");
189 } catch (FormatException) {
193 headers.Add ("if-unmodified-since", "");
194 Assert.Fail ("if-unmodified-since");
195 } catch (FormatException) {
199 headers.Add ("max-forwards", "");
200 Assert.Fail ("max-forwards");
201 } catch (FormatException) {
205 headers.Add ("proxy-authorization", "");
206 Assert.Fail ("proxy-authorization");
207 } catch (FormatException) {
211 headers.Add ("range", "");
212 } catch (FormatException) {
216 headers.Add ("referer", "");
217 Assert.Fail ("referer");
218 } catch (FormatException) {
221 headers.Add ("accept", "audio/y");
222 headers.Add ("accept-charset", "achs");
223 headers.Add ("accept-encoding", "aenc");
224 headers.Add ("accept-language", "alan");
225 headers.Add ("expect", "exp");
226 headers.Add ("if-match", "\"v\"");
227 headers.Add ("if-none-match", "\"v2\"");
228 headers.Add ("pragma", "p");
229 headers.Add ("TE", "0.8");
230 headers.Add ("trailer", "value2");
231 headers.Add ("transfer-encoding", "ttt");
232 headers.Add ("upgrade", "uuu");
233 headers.Add ("user-agent", "uaua");
234 headers.Add ("via", "prot v");
235 headers.Add ("warning", "4 ww \"t\"");
237 Assert.IsTrue (headers.Accept.SequenceEqual (
239 new MediaTypeWithQualityHeaderValue ("audio/x"),
240 new MediaTypeWithQualityHeaderValue ("audio/y")
244 Assert.IsTrue (headers.AcceptCharset.SequenceEqual (
246 new StringWithQualityHeaderValue ("str-v", 0.002),
247 new StringWithQualityHeaderValue ("achs")
251 Assert.IsTrue (headers.AcceptEncoding.SequenceEqual (
253 new StringWithQualityHeaderValue ("str-enc", 0.44),
254 new StringWithQualityHeaderValue ("aenc")
258 Assert.IsTrue (headers.AcceptLanguage.SequenceEqual (
260 new StringWithQualityHeaderValue ("str-lang", 0.41),
261 new StringWithQualityHeaderValue ("alan")
265 Assert.AreEqual (new AuthenticationHeaderValue ("sh-aut", "par"), headers.Authorization);
267 var cch = new CacheControlHeaderValue () {
268 MaxAge = TimeSpan.MaxValue,
271 Assert.AreEqual (cch, headers.CacheControl);
273 Assert.IsTrue (headers.Connection.SequenceEqual (
274 new string[] { "test-value", "close" }));
276 Assert.AreEqual (headers.Date, new DateTimeOffset (DateTime.Today));
278 Assert.IsTrue (headers.Expect.SequenceEqual (
280 new NameValueWithParametersHeaderValue ("en", "ev"),
281 new NameValueWithParametersHeaderValue ("100-continue"),
282 new NameValueWithParametersHeaderValue ("exp")
285 Assert.AreEqual (headers.From, "webmaster@w3.org");
287 Assert.IsTrue (headers.IfMatch.SequenceEqual (
288 new EntityTagHeaderValue[] {
289 new EntityTagHeaderValue ("\"tag\"", true),
290 new EntityTagHeaderValue ("\"v\"", false)
294 Assert.AreEqual (headers.IfModifiedSince, new DateTimeOffset (DateTime.Today));
295 Assert.IsTrue (headers.IfNoneMatch.SequenceEqual (new EntityTagHeaderValue[] { new EntityTagHeaderValue ("\"tag2\"", true), new EntityTagHeaderValue ("\"v2\"", false) }));
296 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.IfRange.Date);
297 Assert.AreEqual (headers.MaxForwards, 0x15b3);
298 Assert.IsTrue (headers.Pragma.SequenceEqual (new NameValueHeaderValue[] { new NameValueHeaderValue ("name", "value"), new NameValueHeaderValue ("p", null) }));
299 Assert.AreEqual ("p", headers.ProxyAuthorization.Parameter);
300 Assert.AreEqual ("s", headers.ProxyAuthorization.Scheme);
301 Assert.AreEqual (5, headers.Range.Ranges.First ().From);
302 Assert.AreEqual (30, headers.Range.Ranges.First ().To);
303 Assert.AreEqual ("bytes", headers.Range.Unit);
304 Assert.AreEqual (headers.Referrer, new Uri ("http://xamarin.com"));
305 Assert.IsTrue (headers.TE.SequenceEqual (new TransferCodingWithQualityHeaderValue[] { new TransferCodingWithQualityHeaderValue ("TE", 0.3), new TransferCodingWithQualityHeaderValue ("0.8") }), "29");
306 Assert.IsTrue (headers.Trailer.SequenceEqual (
311 Assert.IsTrue (headers.TransferEncoding.SequenceEqual (
313 new TransferCodingHeaderValue ("tchv"),
314 new TransferCodingHeaderValue ("chunked"),
315 new TransferCodingHeaderValue ("ttt")
319 Assert.IsTrue (headers.Upgrade.SequenceEqual (
321 new ProductHeaderValue ("prod", "ver"),
322 new ProductHeaderValue ("uuu")
326 Assert.IsTrue (headers.UserAgent.SequenceEqual (
328 new ProductInfoHeaderValue ("(comment)"),
329 new ProductInfoHeaderValue ("uaua", null)
333 Assert.IsTrue (headers.Via.SequenceEqual (
335 new ViaHeaderValue ("protocol", "rec-by"),
336 new ViaHeaderValue ("prot", "v")
340 Assert.IsTrue (headers.Warning.SequenceEqual (
342 new WarningHeaderValue (5, "agent", "\"txt\""),
343 new WarningHeaderValue (4, "ww", "\"t\"")
350 public void Header_BaseImplementation ()
352 HttpRequestMessage message = new HttpRequestMessage ();
353 HttpRequestHeaders headers = message.Headers;
355 headers.Add ("a", "a-value");
356 headers.Add ("b", new List<string> { "v1", "v2" });
357 headers.Add ("c", null as string);
358 headers.Add ("d", new string[0]);
360 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#0");
362 Assert.IsFalse (headers.Contains ("nn"), "#1a");
363 Assert.IsTrue (headers.Contains ("b"), "#1b");
365 var values = headers.GetValues ("b").ToList ();
366 Assert.AreEqual ("v1", values[0], "#2a");
367 Assert.AreEqual ("v2", values[1], "#2b");
369 Assert.IsFalse (headers.Remove ("p"), "#3a");
370 Assert.IsTrue (headers.Remove ("b"), "#3b");
371 Assert.IsFalse (headers.Contains ("b"), "#3b-c");
373 IEnumerable<string> values2;
374 Assert.IsTrue (headers.TryGetValues ("c", out values2));
375 values = values2.ToList ();
376 Assert.AreEqual ("", values[0], "#4a");
379 foreach (var i in headers) {
383 Assert.AreEqual (3, counter, "#5");
387 headers.Accept.Add (new MediaTypeWithQualityHeaderValue ("audio/x"));
388 Assert.IsTrue (headers.TryAddWithoutValidation ("accept", "audio"), "#55");
390 values = headers.GetValues ("accept").ToList ();
391 Assert.AreEqual (2, values.Count, "#6");
392 Assert.AreEqual ("audio/x", values[0], "#6a");
393 Assert.AreEqual ("audio", values[1], "#6b");
394 Assert.AreEqual (1, headers.Accept.Count, "#6c");
398 Assert.IsTrue (headers.TryAddWithoutValidation ("from", new[] { "a@a.com", "ssss@oo.com" }), "#70");
399 values = headers.GetValues ("from").ToList ();
401 Assert.AreEqual (2, values.Count, "#7");
402 Assert.AreEqual ("a@a.com", values[0], "#7a");
403 Assert.AreEqual ("ssss@oo.com", values[1], "#7b");
404 Assert.AreEqual ("a@a.com", headers.From, "#7c");
408 Assert.IsTrue (headers.TryAddWithoutValidation ("Date", "wrong date"), "#8-0");
409 var value = headers.Date;
410 Assert.IsNull (headers.Date, "#8");
414 public void Headers_Invalid ()
416 HttpRequestMessage message = new HttpRequestMessage ();
417 HttpRequestHeaders headers = message.Headers;
420 headers.Add ("Allow", "");
422 } catch (InvalidOperationException) {
426 headers.Add (null, "");
428 } catch (ArgumentException) {
432 headers.Add ("mm", null as IEnumerable<string>);
434 } catch (ArgumentNullException) {
438 headers.Add ("accept", "audio");
440 } catch (FormatException) {
443 Assert.IsFalse (headers.TryAddWithoutValidation ("Allow", ""), "#3"); ;
445 Assert.IsFalse (headers.TryAddWithoutValidation (null, ""), "#4");
448 headers.Contains (null);
450 } catch (ArgumentException) {
454 headers.GetValues (null);
456 } catch (ArgumentException) {
460 headers.GetValues ("bbbb");
462 } catch (InvalidOperationException) {
466 headers.Add ("from", new[] { "a@a.com", "ssss@oo.com" });
468 } catch (FormatException) {
471 Assert.IsTrue (headers.TryAddWithoutValidation ("from", "a@a.com"), "#7-0");
473 headers.Add ("from", "valid@w3.org");
475 } catch (FormatException) {
480 public void Headers_Response ()
482 HttpRequestMessage message = new HttpRequestMessage ();
483 HttpRequestHeaders headers = message.Headers;
485 headers.Add ("Age", "vv");
486 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#1");
489 headers.TryAddWithoutValidation ("Age", "vv");
490 Assert.AreEqual ("vv", headers.GetValues ("Age").First (), "#2");
492 Assert.AreEqual ("Method: GET, RequestUri: '<null>', Version: 1.1, Content: <null>, Headers:\r\n{\r\nAge: vv\r\n}", message.ToString (), "#3");
496 public void Headers_ExpectContinue ()
498 HttpRequestMessage message = new HttpRequestMessage ();
499 HttpRequestHeaders headers = message.Headers;
500 Assert.IsNull (headers.ExpectContinue, "#1");
502 headers.ExpectContinue = false;
503 Assert.IsFalse (headers.ExpectContinue.Value, "#2");
507 headers.ExpectContinue = true;
508 headers.ExpectContinue = true;
509 headers.ExpectContinue = true;
510 headers.ExpectContinue = true;
511 Assert.IsTrue (headers.ExpectContinue.Value, "#3");
512 Assert.AreEqual (1, headers.GetValues ("expect").ToList ().Count, "#4");
515 headers.Expect.Add (new NameValueWithParametersHeaderValue ("100-conTinuE"));
516 Assert.IsTrue (headers.ExpectContinue.Value, "#5");
520 public void Headers_ConnectionClose ()
522 HttpRequestMessage message = new HttpRequestMessage ();
523 HttpRequestHeaders headers = message.Headers;
524 Assert.IsNull (headers.ConnectionClose, "#1");
526 headers.ConnectionClose = false;
527 Assert.IsFalse (headers.ConnectionClose.Value, "#2");
531 headers.ConnectionClose = true;
532 Assert.IsTrue (headers.ConnectionClose.Value, "#3");
535 headers.Connection.Add ("Close");
536 Assert.IsTrue (headers.ConnectionClose.Value, "#4");
540 public void Headers_From_Invalid ()
542 HttpRequestMessage message = new HttpRequestMessage ();
543 HttpRequestHeaders headers = message.Headers;
549 } catch (FormatException) {
552 headers.From = "test";
554 } catch (FormatException) {
559 public void Headers_TransferEncodingChunked ()
561 HttpRequestMessage message = new HttpRequestMessage ();
562 HttpRequestHeaders headers = message.Headers;
563 Assert.IsNull (headers.TransferEncodingChunked, "#1");
565 headers.TransferEncodingChunked = false;
566 Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
570 headers.TransferEncodingChunked = true;
571 Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
572 Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
576 public void Properties ()
578 var c = new HttpRequestMessage ();
580 c.Method = HttpMethod.Post;
581 c.Properties.Add ("a", "test");
583 c.Version = HttpVersion.Version10;
585 Assert.IsNull (c.Content, "#1");
586 Assert.AreEqual (HttpMethod.Post, c.Method, "#2");
587 Assert.AreEqual ("test", c.Properties["a"], "#3");
588 Assert.IsNull (c.RequestUri, "#4");
589 Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
593 public void Properties_Invalid ()
595 var c = new HttpRequestMessage ();
599 } catch (ArgumentNullException) {
603 c.RequestUri = new Uri ("ftp://xamarin.com");
605 } catch (ArgumentException) {
611 } catch (ArgumentNullException) {