2 // HttpResponseMessageTest.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 HttpResponseMessageTest
44 public void Ctor_Invalid ()
47 new HttpResponseMessage ((HttpStatusCode) (-1));
49 } catch (ArgumentOutOfRangeException) {
54 public void Ctor_Default ()
56 var m = new HttpResponseMessage ();
57 Assert.IsNull (m.Content, "#1");
58 Assert.IsNotNull (m.Headers, "#2");
59 Assert.IsTrue (m.IsSuccessStatusCode, "#3");
60 Assert.AreEqual ("OK", m.ReasonPhrase, "#4");
61 Assert.IsNull (m.RequestMessage, "#5");
62 Assert.AreEqual (HttpStatusCode.OK, m.StatusCode, "#6");
63 Assert.AreEqual (new Version (1, 1), m.Version, "#7");
64 Assert.IsNull (m.Headers.CacheControl, "#8");
66 Assert.AreEqual ("StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: <null>, Headers:\r\n{\r\n}", m.ToString (), "#9");
70 public void Headers ()
72 HttpResponseMessage message = new HttpResponseMessage ();
73 HttpResponseHeaders headers = message.Headers;
75 headers.AcceptRanges.Add ("ac-v");
76 headers.Age = TimeSpan.MaxValue;
77 headers.CacheControl = new CacheControlHeaderValue () {
80 headers.Connection.Add ("test-value");
81 headers.ConnectionClose = true;
82 headers.Date = new DateTimeOffset (DateTime.Today);
83 headers.ETag = new EntityTagHeaderValue ("\"tag\"", true);
84 headers.Location = new Uri ("http://xamarin.com");
85 headers.Pragma.Add (new NameValueHeaderValue ("name", "value"));
86 headers.ProxyAuthenticate.Add (new AuthenticationHeaderValue ("proxy", "par"));
87 headers.RetryAfter = new RetryConditionHeaderValue (TimeSpan.MinValue);
88 headers.Server.Add (new ProductInfoHeaderValue ("(comment)"));
89 headers.Trailer.Add ("trailer-vvv");
90 headers.TransferEncoding.Add (new TransferCodingHeaderValue ("tchv"));
91 headers.TransferEncodingChunked = true;
92 headers.Upgrade.Add (new ProductHeaderValue ("prod", "ver"));
93 headers.Vary.Add ("vary");
94 headers.Via.Add (new ViaHeaderValue ("protocol", "rec-by"));
95 headers.Warning.Add (new WarningHeaderValue (5, "agent", "\"txt\""));
96 headers.WwwAuthenticate.Add (new AuthenticationHeaderValue ("www", "par"));
99 headers.Add ("age", "");
101 } catch (FormatException) {
105 headers.Add ("date", "");
106 Assert.Fail ("date");
107 } catch (FormatException) {
111 headers.Add ("etag", "");
112 Assert.Fail ("etag");
113 } catch (FormatException) {
117 headers.Add ("location", "extra");
118 Assert.Fail ("location");
119 } catch (FormatException) {
123 headers.Add ("retry-after", "extra");
124 Assert.Fail ("retry-after");
125 } catch (FormatException) {
128 headers.Add ("accept-ranges", "achs");
129 // TODO: headers.Add ("cache-control", "cache-value");
130 headers.Add ("connection", "ccc");
131 headers.Add ("pragma", "p");
132 headers.Add ("proxy-authenticate", "ttt");
133 headers.Add ("server", "server");
134 headers.Add ("trailer", "tt-r");
135 headers.Add ("transfer-encoding", "ttt");
136 headers.Add ("upgrade", "uuu");
137 headers.Add ("upgrade", "vvvvaa");
138 headers.Add ("vary", "vvaar");
139 headers.Add ("via", "prot v");
140 headers.Add ("warning", "4 ww \"t\"");
141 headers.Add ("www-Authenticate", "ww");
143 Assert.IsTrue (headers.AcceptRanges.SequenceEqual (
150 Assert.AreEqual (TimeSpan.MaxValue, headers.Age);
153 new CacheControlHeaderValue () {
155 // TODO Extensions = { new NameValueHeaderValue ("cache-value") }
156 }, headers.CacheControl);
158 Assert.IsTrue (headers.Connection.SequenceEqual (
159 new string[] { "test-value", "close", "ccc" }));
161 Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Date);
163 Assert.AreEqual (new EntityTagHeaderValue ("\"tag\"", true), headers.ETag);
165 Assert.AreEqual (new Uri ("http://xamarin.com"), headers.Location);
167 Assert.IsTrue (headers.Pragma.SequenceEqual (
169 new NameValueHeaderValue ("name", "value"),
170 new NameValueHeaderValue ("p"),
173 Assert.IsTrue (headers.ProxyAuthenticate.SequenceEqual (
175 new AuthenticationHeaderValue ("proxy", "par"),
176 new AuthenticationHeaderValue ("ttt")
180 Assert.AreEqual (new RetryConditionHeaderValue (TimeSpan.MinValue), headers.RetryAfter);
182 Assert.IsTrue (headers.Server.SequenceEqual (
184 new ProductInfoHeaderValue ("(comment)"),
185 new ProductInfoHeaderValue (new ProductHeaderValue ("server"))
189 Assert.IsTrue (headers.Trailer.SequenceEqual (
195 Assert.IsTrue (headers.TransferEncoding.SequenceEqual (
197 new TransferCodingHeaderValue ("tchv"),
198 new TransferCodingHeaderValue ("chunked"),
199 new TransferCodingHeaderValue ("ttt")
203 Assert.IsTrue (headers.Upgrade.SequenceEqual (
205 new ProductHeaderValue ("prod", "ver"),
206 new ProductHeaderValue ("uuu"),
207 new ProductHeaderValue ("vvvvaa")
211 Assert.IsTrue (headers.Vary.SequenceEqual (
218 Assert.IsTrue (headers.Via.SequenceEqual (
220 new ViaHeaderValue ("protocol", "rec-by"),
221 new ViaHeaderValue ("prot", "v")
225 Assert.IsTrue (headers.Warning.SequenceEqual (
227 new WarningHeaderValue (5, "agent", "\"txt\""),
228 new WarningHeaderValue (4, "ww", "\"t\"")
232 Assert.IsTrue (headers.WwwAuthenticate.SequenceEqual (
234 new AuthenticationHeaderValue ("www", "par"),
235 new AuthenticationHeaderValue ("ww")
241 public void EnsureSuccessStatusCode ()
243 HttpResponseMessage message = new HttpResponseMessage ();
244 Assert.AreSame (message, message.EnsureSuccessStatusCode (), "#1");
246 message = new HttpResponseMessage (HttpStatusCode.BadRequest);
247 message.ReasonPhrase = "test reason";
249 message.EnsureSuccessStatusCode ();
251 } catch (HttpRequestException e) {
252 Assert.IsTrue (e.Message.Contains ("400 (test reason)"), "#3");
257 public void Headers_GetEnumerator ()
259 HttpResponseMessage message = new HttpResponseMessage ();
260 HttpResponseHeaders headers = message.Headers;
262 headers.Add ("a", new[] { "v1", "v2" });
263 headers.Add ("cache-control", "audio");
264 headers.Age = new TimeSpan (4444, 2, 3, 4, 5);
268 foreach (var entry in headers) {
271 Assert.AreEqual ("a", entry.Key);
272 values = entry.Value.ToList ();
273 Assert.AreEqual (2, values.Count);
274 Assert.AreEqual ("v1", values[0]);
277 Assert.AreEqual ("cache-control", entry.Key);
278 values = entry.Value.ToList ();
279 Assert.AreEqual (1, values.Count);
280 Assert.AreEqual ("audio", values[0]);
283 Assert.AreEqual ("Age", entry.Key);
284 values = entry.Value.ToList ();
285 Assert.AreEqual (1, values.Count);
286 Assert.AreEqual ("383968984", values[0]);
293 Assert.AreEqual (3, i, "#10");
297 public void Header_BaseImplementation ()
299 HttpResponseMessage message = new HttpResponseMessage ();
300 HttpResponseHeaders headers = message.Headers;
302 headers.Add ("a", "a-value");
303 headers.Add ("b", new List<string> { "v1", "v2" });
304 headers.Add ("c", null as string);
305 headers.Add ("d", new string[0]);
307 headers.AddWithoutValidation ("cache-control", "audio");
309 Assert.IsFalse (headers.Contains ("nn"), "#1a");
310 Assert.IsTrue (headers.Contains ("b"), "#1b");
312 var values = headers.GetValues ("b").ToList ();
313 Assert.AreEqual ("v1", values[0], "#2a");
314 Assert.AreEqual ("v2", values[1], "#2b");
316 Assert.IsFalse (headers.Remove ("p"), "#3a");
317 Assert.IsTrue (headers.Remove ("b"), "#3b");
318 Assert.IsFalse (headers.Contains ("b"), "#3b-c");
320 IEnumerable<string> values2;
321 Assert.IsTrue (headers.TryGetValues ("c", out values2));
322 values = values2.ToList ();
323 Assert.AreEqual ("", values[0], "#4a");
326 foreach (var i in headers) {
330 Assert.AreEqual (3, counter, "#5");
336 public void Headers_Invalid ()
338 HttpResponseMessage message = new HttpResponseMessage ();
339 HttpResponseHeaders headers = message.Headers;
342 headers.Add ("age", "");
344 } catch (FormatException) {
348 headers.Add (null, "");
350 } catch (ArgumentException) {
354 headers.Add ("mm", null as IEnumerable<string>);
356 } catch (ArgumentNullException) {
360 headers.Add ("Allow", "audio");
362 } catch (InvalidOperationException) {
366 headers.AddWithoutValidation ("Allow", "");
368 } catch (InvalidOperationException) {
372 headers.AddWithoutValidation (null, "");
374 } catch (ArgumentException) {
378 headers.Contains (null);
380 } catch (ArgumentException) {
384 headers.GetValues (null);
386 } catch (ArgumentException) {
390 headers.GetValues ("bbbb");
392 } catch (InvalidOperationException) {
396 headers.Add ("location", new[] { "google.com", "xamarin.com" });
398 } catch (FormatException) {
401 headers.AddWithoutValidation ("location", "a@a.com");
403 headers.Add ("location", "w3.org");
405 } catch (FormatException) {
410 public void Headers_Request ()
412 HttpResponseMessage message = new HttpResponseMessage ();
413 HttpResponseHeaders headers = message.Headers;
415 headers.Add ("accept", "audio");
416 Assert.AreEqual ("audio", headers.GetValues ("Accept").First (), "#1");
419 headers.AddWithoutValidation ("accept", "audio");
420 Assert.AreEqual ("audio", headers.GetValues ("Accept").First (), "#2");
424 public void Headers_ConnectionClose ()
426 HttpResponseMessage message = new HttpResponseMessage ();
427 HttpResponseHeaders headers = message.Headers;
428 Assert.IsNull (headers.ConnectionClose, "#1");
430 headers.ConnectionClose = false;
431 Assert.IsFalse (headers.ConnectionClose.Value, "#2");
435 headers.ConnectionClose = true;
436 Assert.IsTrue (headers.ConnectionClose.Value, "#3");
439 headers.Connection.Add ("Close");
440 Assert.IsTrue (headers.ConnectionClose.Value, "#4");
442 Assert.AreEqual ("StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: <null>, Headers:\r\n{\r\nConnection: Close\r\n}", message.ToString (), "#5");
446 public void Headers_Location ()
448 HttpResponseMessage message = new HttpResponseMessage ();
449 HttpResponseHeaders headers = message.Headers;
450 headers.AddWithoutValidation ("location", "http://w3.org");
451 Assert.AreEqual (new Uri ("http://w3.org"), headers.Location);
455 public void Headers_TransferEncoding ()
457 HttpResponseMessage message = new HttpResponseMessage ();
458 HttpResponseHeaders headers = message.Headers;
459 headers.AddWithoutValidation ("Transfer-Encoding", "mmm");
460 headers.AddWithoutValidation ("Transfer-Encoding", "▀");
461 headers.AddWithoutValidation ("Transfer-Encoding", "zz");
463 var a = headers.TransferEncoding;
464 Assert.AreEqual (2, a.Count, "#1");
466 // Assert.AreEqual ("mmm, zz, ▀", a.ToString (), "#2");
470 public void Headers_TransferEncodingChunked ()
472 HttpResponseMessage message = new HttpResponseMessage ();
473 HttpResponseHeaders headers = message.Headers;
474 Assert.IsNull (headers.TransferEncodingChunked, "#1");
476 headers.TransferEncodingChunked = false;
477 Assert.IsFalse (headers.TransferEncodingChunked.Value, "#2");
481 headers.TransferEncodingChunked = true;
482 Assert.IsTrue (headers.TransferEncodingChunked.Value, "#3");
483 Assert.AreEqual (1, headers.TransferEncoding.Count, "#3b");
486 headers.AddWithoutValidation ("Transfer-Encoding", "value");
487 // Assert.AreEqual (false, headers.TransferEncodingChunked, "#4");
490 headers.AddWithoutValidation ("Transfer-Encoding", "chunked");
491 Assert.AreEqual (true, headers.TransferEncodingChunked, "#5");
493 message = new HttpResponseMessage ();
494 headers = message.Headers;
495 headers.AddWithoutValidation ("Transfer-Encoding", "ß");
496 Assert.IsNull (headers.TransferEncodingChunked, "#6");
500 public void Properties ()
502 var c = new HttpResponseMessage ();
504 c.ReasonPhrase = "rphr";
505 c.RequestMessage = new HttpRequestMessage ();
506 c.RequestMessage.Properties.Add ("a", "test");
507 c.StatusCode = HttpStatusCode.UnsupportedMediaType;
508 c.Version = HttpVersion.Version10;
510 Assert.IsNull (c.Content, "#1");
511 Assert.AreEqual ("rphr", c.ReasonPhrase, "#2");
512 Assert.AreEqual ("test", c.RequestMessage.Properties["a"], "#3");
513 Assert.AreEqual (HttpStatusCode.UnsupportedMediaType, c.StatusCode, "#4");
514 Assert.AreEqual (HttpVersion.Version10, c.Version, "#5");
515 Assert.IsFalse (c.IsSuccessStatusCode, "#6");
519 public void Properties_Invalid ()
521 var c = new HttpResponseMessage ();
524 c.StatusCode = (HttpStatusCode) (-1);
526 } catch (ArgumentException) {
532 } catch (ArgumentNullException) {