Merge pull request #1542 from ninjarobot/UriTemplateMatchException
[mono.git] / mcs / class / System.Net.Http / Test / System.Net.Http.Headers / ViaHeaderValueTest.cs
1 //
2 // ViaHeaderValueTest.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using NUnit.Framework;
33 using System.Net.Http.Headers;
34
35 namespace MonoTests.System.Net.Http.Headers
36 {
37         [TestFixture]
38         public class ViaHeaderValueTest
39         {
40                 [Test]
41                 public void Ctor_InvalidArguments ()
42                 {
43                         try {
44                                 new ViaHeaderValue (null, "rb");
45                                 Assert.Fail ("#1");
46                         } catch (ArgumentException) {
47                         }
48
49                         try {
50                                 new ViaHeaderValue (" ", null);
51                                 Assert.Fail ("#2");
52                         } catch (FormatException) {
53                         }
54
55                         try {
56                                 new ViaHeaderValue ("a", null);
57                                 Assert.Fail ("#3");
58                         } catch (ArgumentException) {
59                         }
60
61                         try {
62                                 new ViaHeaderValue ("a", "b", "::");
63                                 Assert.Fail ("#4");
64                         } catch (FormatException) {
65                         }
66
67                         try {
68                                 new ViaHeaderValue ("a", "b", null, "(aaa");
69                                 Assert.Fail ("#5");
70                         } catch (FormatException) {
71                         }
72                 }
73
74                 [Test]
75                 public void Ctor_ValidArguments ()
76                 {
77                         new ViaHeaderValue ("a", "b", null);
78                         new ViaHeaderValue ("a", "b", null, null);
79                 }
80
81                 [Test]
82                 public void Equals ()
83                 {
84                         var value = new ViaHeaderValue ("ab", "x");
85                         Assert.AreEqual (value, new ViaHeaderValue ("ab", "x"), "#1");
86                         Assert.AreEqual (value, new ViaHeaderValue ("AB", "X"), "#2");
87                         Assert.AreNotEqual (value, new ViaHeaderValue ("AA", "x"), "#3");
88
89                         value = new ViaHeaderValue ("ab", "DD", "cc");
90                         Assert.AreEqual (value, new ViaHeaderValue ("Ab", "DD", "cC"), "#4");
91                         Assert.AreNotEqual (value, new ViaHeaderValue ("AB", "DD"), "#5");
92                         Assert.AreNotEqual (value, new ViaHeaderValue ("Ab", "dd", "cc", "(c)"), "#6");
93                 }
94
95                 [Test]
96                 public void Parse ()
97                 {
98                         var res = ViaHeaderValue.Parse ("1.1 nowhere.com");
99                         Assert.IsNull (res.ProtocolName, "#1");
100                         Assert.AreEqual ("nowhere.com", res.ReceivedBy, "#2");
101                         Assert.AreEqual ("1.1", res.ProtocolVersion, "#3");
102                         Assert.AreEqual ("1.1 nowhere.com", res.ToString (), "#4");
103
104                         res = ViaHeaderValue.Parse ("foo / 1.1 nowhere.com:43   ( lal ( a ) la ) ");
105                         Assert.AreEqual ("foo", res.ProtocolName, "#10");
106                         Assert.AreEqual ("1.1", res.ProtocolVersion, "#11");
107                         Assert.AreEqual ("nowhere.com:43", res.ReceivedBy, "#12");
108                         Assert.AreEqual ("( lal ( a ) la )", res.Comment, "#13");
109                         Assert.AreEqual ("foo/1.1 nowhere.com:43 ( lal ( a ) la )", res.ToString (), "#14");
110                 }
111
112                 [Test]
113                 public void Parse_Invalid ()
114                 {
115                         try {
116                                 ViaHeaderValue.Parse (null);
117                                 Assert.Fail ("#1");
118                         } catch (FormatException) {
119                         }
120
121                         try {
122                                 ViaHeaderValue.Parse ("  ");
123                                 Assert.Fail ("#2");
124                         } catch (FormatException) {
125                         }
126
127                         try {
128                                 ViaHeaderValue.Parse ("a");
129                                 Assert.Fail ("#3");
130                         } catch (FormatException) {
131                         }
132
133                         try {
134                                 ViaHeaderValue.Parse ("1 nowhere.com :43");
135                                 Assert.Fail ("#4");
136                         } catch (FormatException) {
137                         }
138                 }
139
140                 [Test]
141                 public void Properties ()
142                 {
143                         var value = new ViaHeaderValue ("s", "p");
144                         Assert.IsNull (value.ProtocolName, "#1");
145                         Assert.AreEqual ("s", value.ProtocolVersion, "#2");
146                         Assert.AreEqual ("p", value.ReceivedBy, "#3");
147
148                         value = new ViaHeaderValue ("s", "rb", "name");
149                         Assert.AreEqual ("name", value.ProtocolName, "#4");
150                         Assert.AreEqual ("s", value.ProtocolVersion, "#5");
151                         Assert.AreEqual ("rb", value.ReceivedBy, "#6");
152
153                         value = new ViaHeaderValue ("s", "rb", "name", "(cmt)");
154                         Assert.AreEqual ("name", value.ProtocolName, "#7");
155                         Assert.AreEqual ("s", value.ProtocolVersion, "#8");
156                         Assert.AreEqual ("rb", value.ReceivedBy, "#9");
157                         Assert.AreEqual ("(cmt)", value.Comment, "#10");
158                 }
159
160                 [Test]
161                 public void TryParse ()
162                 {
163                         ViaHeaderValue res;
164                         Assert.IsTrue (ViaHeaderValue.TryParse ("a b", out res), "#1");
165                         Assert.AreEqual ("b", res.ReceivedBy, "#2");
166                         Assert.AreEqual ("a", res.ProtocolVersion, "#3");
167                         Assert.IsNull (res.Comment, "#4");
168                         Assert.IsNull (res.ProtocolName, "#5");
169                 }
170
171                 [Test]
172                 public void TryParse_Invalid ()
173                 {
174                         ViaHeaderValue res;
175                         Assert.IsFalse (ViaHeaderValue.TryParse ("", out res), "#1");
176                         Assert.IsNull (res, "#2");
177                 }
178         }
179 }