Merge pull request #3522 from henricm/fix-csharp-compiler-path-windows
[mono.git] / mcs / class / System.Net.Http / Test / System.Net.Http.Headers / HttpHeaderValueCollection.cs
1 //
2 // HttpHeaderValueCollectionTest.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 using System.Net.Http;
35 using System.Linq;
36
37 namespace MonoTests.System.Net.Http.Headers
38 {
39         [TestFixture]
40         public class HttpHeaderValueCollectionTest
41         {
42                 [Test]
43                 public void ParseAdd ()
44                 {
45                         HttpRequestMessage message = new HttpRequestMessage ();
46                         HttpRequestHeaders headers = message.Headers;
47
48                         headers.TE.ParseAdd ("pp");
49                         Assert.AreEqual ("pp", headers.TE.ToArray ()[0].Value, "#1");
50                         Assert.AreEqual (1, headers.TE.Count);
51                 }
52
53                 [Test]
54                 public void ParseAdd_Invalid ()
55                 {
56                         HttpRequestMessage message = new HttpRequestMessage ();
57                         HttpRequestHeaders headers = message.Headers;
58
59                         try {
60                                 headers.Via.ParseAdd ("pp");
61                         } catch (FormatException) {
62                         }
63                 }
64
65                 [Test]
66                 public void TryParseAdd ()
67                 {
68                         HttpRequestMessage message = new HttpRequestMessage ();
69                         HttpRequestHeaders headers = message.Headers;
70
71                         Assert.IsTrue (headers.TE.TryParseAdd ("pp"), "#1");
72                         Assert.AreEqual ("pp", headers.TE.ToArray ()[0].Value, "#2");
73                         Assert.AreEqual (1, headers.TE.Count, "#3");
74
75                         Assert.IsFalse (headers.Via.TryParseAdd ("wrong"), "#4");
76                 }
77
78                 [Test]
79                 public void ToStringTest ()
80                 {
81                         HttpRequestMessage message = new HttpRequestMessage ();
82                         HttpRequestHeaders headers = message.Headers;
83
84                         Assert.AreEqual ("", headers.Connection.ToString (), "#1");
85
86                         headers.Connection.Add ("kk");
87
88                         Assert.AreEqual ("kk", headers.Connection.ToString (), "#2");
89
90                         headers.Connection.Add ("ttt");
91
92                         Assert.AreEqual ("kk, ttt", headers.Connection.ToString (), "#3");
93                 }
94
95                 [Test]
96                 public void AddInvalid ()
97                 {
98                         HttpRequestMessage message = new HttpRequestMessage ();
99                         HttpRequestHeaders headers = message.Headers;
100
101                         headers.TryAddWithoutValidation ("User-Agent", "user,agent/1.0");
102
103                         Assert.AreEqual (0, headers.UserAgent.Count, "#1");
104                         Assert.AreEqual ("user,agent/1.0", headers.UserAgent.ToString (), "#2");
105
106                         Assert.AreEqual ("User-Agent: user,agent/1.0\r\n", headers.ToString (), "#3");
107
108                         headers.UserAgent.Clear ();
109                         Assert.AreEqual ("", headers.UserAgent.ToString (), "#4");
110                         Assert.AreEqual ("", headers.ToString (), "#5");
111                 }
112
113                 [Test]
114                 public void AddInvalidAndValid ()
115                 {
116                         HttpRequestMessage message = new HttpRequestMessage ();
117                         HttpRequestHeaders headers = message.Headers;
118
119                         headers.TryAddWithoutValidation ("User-Agent", "user,agent/1.0");
120                         headers.TryAddWithoutValidation("User-Agent", "agent2/2.0");
121
122                         Assert.AreEqual (1, headers.UserAgent.Count, "#1");
123                         Assert.AreEqual ("agent2/2.0user,agent/1.0", headers.UserAgent.ToString (), "#2");
124
125                         Assert.AreEqual ("User-Agent: agent2/2.0 user,agent/1.0\r\n", headers.ToString (), "#3");
126
127                         headers.UserAgent.Clear ();
128                         Assert.AreEqual ("", headers.UserAgent.ToString (), "#4");
129                         Assert.AreEqual ("", headers.ToString (), "#5");
130                 }
131         }
132 }