2 // ContentRangeHeaderValueTest.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2012 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
.Headers
;
35 namespace MonoTests
.System
.Net
.Http
.Headers
38 public class ContentRangeHeaderValueTest
41 public void Ctor_InvalidArguments ()
44 new ContentRangeHeaderValue (-1);
46 } catch (ArgumentOutOfRangeException
) {
50 new ContentRangeHeaderValue (10, 5);
52 } catch (ArgumentOutOfRangeException
) {
56 new ContentRangeHeaderValue (0, 1, 0);
58 } catch (ArgumentOutOfRangeException
) {
62 new ContentRangeHeaderValue (-1, 15);
64 } catch (ArgumentOutOfRangeException
) {
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) {
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");
113 public void Parse_Invalid ()
116 ContentRangeHeaderValue
.Parse (null);
118 } catch (FormatException
) {
122 ContentRangeHeaderValue
.Parse (" ");
124 } catch (FormatException
) {
128 ContentRangeHeaderValue
.Parse ("a b");
130 } catch (FormatException
) {
134 ContentRangeHeaderValue
.Parse ("bytes 10/");
136 } catch (FormatException
) {
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);
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");
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");
171 public void TryParse_Invalid ()
173 ContentRangeHeaderValue res
;
174 Assert
.IsFalse (ContentRangeHeaderValue
.TryParse ("", out res
), "#1");
175 Assert
.IsNull (res
, "#2");