[System.Net.Htpp] Add test for large content range headers
[mono-project.git] / mcs / class / System.Net.Http / Test / System.Net.Http.Headers / ContentRangeHeaderValueTest.cs
blob7522def2d7b2999527d732a93cc3006bac2b0d24
1 //
2 // ContentRangeHeaderValueTest.cs
3 //
4 // Authors:
5 // Marek Safar <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
8 //
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.
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using NUnit.Framework;
33 using System.Net.Http.Headers;
35 namespace MonoTests.System.Net.Http.Headers
37 [TestFixture]
38 public class ContentRangeHeaderValueTest
40 [Test]
41 public void Ctor_InvalidArguments ()
43 try {
44 new ContentRangeHeaderValue (-1);
45 Assert.Fail ("#1");
46 } catch (ArgumentOutOfRangeException) {
49 try {
50 new ContentRangeHeaderValue (10, 5);
51 Assert.Fail ("#2");
52 } catch (ArgumentOutOfRangeException) {
55 try {
56 new ContentRangeHeaderValue (0, 1, 0);
57 Assert.Fail ("#3");
58 } catch (ArgumentOutOfRangeException) {
61 try {
62 new ContentRangeHeaderValue (-1, 15);
63 Assert.Fail ("#4");
64 } catch (ArgumentOutOfRangeException) {
68 [Test]
69 public void Equals ()
71 var value = new ContentRangeHeaderValue (8);
72 Assert.AreEqual (value, new ContentRangeHeaderValue (8), "#1");
73 Assert.AreNotEqual (value, new ContentRangeHeaderValue (8, 30), "#2");
75 value = new ContentRangeHeaderValue (5, 30, 100);
76 Assert.AreEqual (value, new ContentRangeHeaderValue (5, 30, 100), "#3");
77 Assert.AreNotEqual (value, new ContentRangeHeaderValue (5, 30, 101), "#4");
78 Assert.AreNotEqual (value, new ContentRangeHeaderValue (5, 30), "#5");
79 Assert.AreNotEqual (value, new ContentRangeHeaderValue (5, 30, 100) {
80 Unit = "g"
81 }, "#6");
84 [Test]
85 public void Parse ()
87 var res = ContentRangeHeaderValue.Parse ("bytes 0 - 499/ 9223372036854775807");
88 Assert.AreEqual (0, res.From, "#1");
89 Assert.AreEqual (499, res.To, "#2");
90 Assert.AreEqual (9223372036854775807, res.Length, "#3");
91 Assert.AreEqual ("bytes 0-499/9223372036854775807", res.ToString (), "#4");
93 res = ContentRangeHeaderValue.Parse ("bytes */ 8");
94 Assert.IsNull (res.From, "#11");
95 Assert.IsNull (res.To, "#12");
96 Assert.AreEqual (8, res.Length, "#13");
97 Assert.AreEqual ("bytes */8", res.ToString (), "#14");
99 res = ContentRangeHeaderValue.Parse ("by */*");
100 Assert.IsNull (res.From, "#21");
101 Assert.IsNull (res.To, "#22");
102 Assert.IsNull (res.Length, "#23");
103 Assert.AreEqual ("by */*", res.ToString (), "#24");
105 res = ContentRangeHeaderValue.Parse("bytes 199999999999999999 - 999999999999999999/ 9223372036854775807");
106 Assert.AreEqual (199999999999999999, res.From, "#31");
107 Assert.AreEqual (999999999999999999, res.To, "#32");
108 Assert.AreEqual (9223372036854775807, res.Length, "#33");
109 Assert.AreEqual ("bytes 199999999999999999-999999999999999999/9223372036854775807", res.ToString (), "#34");
112 [Test]
113 public void Parse_Invalid ()
115 try {
116 ContentRangeHeaderValue.Parse (null);
117 Assert.Fail ("#1");
118 } catch (FormatException) {
121 try {
122 ContentRangeHeaderValue.Parse (" ");
123 Assert.Fail ("#2");
124 } catch (FormatException) {
127 try {
128 ContentRangeHeaderValue.Parse ("a b");
129 Assert.Fail ("#3");
130 } catch (FormatException) {
133 try {
134 ContentRangeHeaderValue.Parse ("bytes 10/");
135 Assert.Fail ("#4");
136 } catch (FormatException) {
140 [Test]
141 public void Properties ()
143 var value = new ContentRangeHeaderValue (4);
144 Assert.IsNull (value.From, "#1");
145 Assert.IsTrue (value.HasLength, "#2");
146 Assert.IsFalse (value.HasRange, "#3");
147 Assert.AreEqual (4, value.Length, "#4");
148 Assert.IsNull (value.To, "#5");
149 Assert.AreEqual ("bytes", value.Unit, "#6");
151 value = new ContentRangeHeaderValue (1, 10, 20);
152 value.Unit = "mu";
153 Assert.AreEqual (1, value.From, "#11");
154 Assert.IsTrue (value.HasLength, "#12");
155 Assert.IsTrue (value.HasRange, "#13");
156 Assert.AreEqual (20, value.Length, "#14");
157 Assert.AreEqual (10, value.To, "#15");
158 Assert.AreEqual ("mu", value.Unit, "#16");
161 [Test]
162 public void TryParse ()
164 ContentRangeHeaderValue res;
165 Assert.IsTrue (ContentRangeHeaderValue.TryParse ("b 1-10/*", out res), "#1");
166 Assert.AreEqual (1, res.From, "#2");
167 Assert.AreEqual (10, res.To, "#3");
170 [Test]
171 public void TryParse_Invalid ()
173 ContentRangeHeaderValue res;
174 Assert.IsFalse (ContentRangeHeaderValue.TryParse ("", out res), "#1");
175 Assert.IsNull (res, "#2");