Merge pull request #1870 from saper/langinfo_h
[mono.git] / mcs / class / System.Web / Test / System.Web.Util / RequestValidatorTest.cs
1 // Authors:
2 //      Marek Habersack <mhabersack@novell.com>
3 //
4 // Copyright (C) 2010 Novell Inc. http://novell.com
5 //
6
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 using System;
28 using System.Collections.Generic;
29 using System.Diagnostics;
30 using System.Text;
31 using System.Web;
32 using System.Web.Caching;
33 using System.Web.Util;
34
35 using NUnit.Framework;
36 using MonoTests.Common;
37
38 namespace MonoTests.System.Web.Util
39 {       
40         class TestRequestValidator : RequestValidator
41         {
42                 public bool DoIsValidRequestString (HttpContext context, string value, RequestValidationSource requestValidationSource,
43                                                 string collectionKey, out int validationFailureIndex)
44                 {
45                         return IsValidRequestString (context, value, requestValidationSource, collectionKey, out validationFailureIndex);
46                 }
47         }
48
49         [TestFixture]
50         public class RequestValidatorTest
51         {
52                 class FakeHttpWorkerRequest : BaseFakeHttpWorkerRequest
53                 {
54                         Uri uri;
55
56                         public FakeHttpWorkerRequest (string url)
57                         {
58                                 uri = new Uri (url, UriKind.Absolute);
59                         }
60
61                         public override string GetQueryString ()
62                         {
63                                 return uri.Query;
64                         }
65
66                         public override string GetUriPath ()
67                         {
68                                 return uri.AbsolutePath;
69                         }
70                 }
71
72                 [Test]
73                 public void Current ()
74                 {
75                         Assert.IsNotNull (RequestValidator.Current, "#A1");
76                         Assert.AreEqual (typeof (RequestValidator).AssemblyQualifiedName, RequestValidator.Current.GetType ().AssemblyQualifiedName, "#A2");
77                         AssertExtensions.Throws<ArgumentNullException> (() => {
78                                 RequestValidator.Current = null;
79                         }, "#A3");
80
81                         RequestValidator.Current = new TestRequestValidator ();
82                         Assert.IsNotNull (RequestValidator.Current, "#B1");
83                         Assert.AreEqual (typeof (TestRequestValidator).AssemblyQualifiedName, RequestValidator.Current.GetType ().AssemblyQualifiedName, "#B2");
84                 }
85
86                 [Test]
87                 public void IsValidRequestString ()
88                 {
89                         var rv = new TestRequestValidator ();
90                         int validationFailureIndex;
91                         var fr = new FakeHttpWorkerRequest ("http://localhost/default.aspx?key=invalid%value");
92                         var ctx = new HttpContext (fr);
93
94                         Assert.IsFalse (rv.DoIsValidRequestString (null, "<script>invalid%value", RequestValidationSource.QueryString, "key", out validationFailureIndex), "#A1");
95                         Assert.IsFalse (rv.DoIsValidRequestString (ctx, "<script>invalid%value", RequestValidationSource.QueryString, "key", out validationFailureIndex), "#A2");
96                 }
97         }
98 }