cbb24fab1dc6a1e217b9381881617c3ae537f126
[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 #if NET_4_0
28 using System;
29 using System.Collections.Generic;
30 using System.Diagnostics;
31 using System.Text;
32 using System.Web;
33 using System.Web.Caching;
34 using System.Web.Util;
35
36 using NUnit.Framework;
37 using MonoTests.Common;
38
39 namespace MonoTests.System.Web.Util
40 {       
41         class TestRequestValidator : RequestValidator
42         {
43                 public bool DoIsValidRequestString (HttpContext context, string value, RequestValidationSource requestValidationSource,
44                                                 string collectionKey, out int validationFailureIndex)
45                 {
46                         return IsValidRequestString (context, value, requestValidationSource, collectionKey, out validationFailureIndex);
47                 }
48         }
49
50         [TestFixture]
51         public class RequestValidatorTest
52         {
53                 class FakeHttpWorkerRequest : BaseFakeHttpWorkerRequest
54                 {
55                         Uri uri;
56
57                         public FakeHttpWorkerRequest (string url)
58                         {
59                                 uri = new Uri (url, UriKind.Absolute);
60                         }
61
62                         public override string GetQueryString ()
63                         {
64                                 return uri.Query;
65                         }
66
67                         public override string GetUriPath ()
68                         {
69                                 return uri.AbsolutePath;
70                         }
71                 }
72
73                 [Test]
74                 public void Current ()
75                 {
76                         Assert.IsNotNull (RequestValidator.Current, "#A1");
77                         Assert.AreEqual (typeof (RequestValidator).AssemblyQualifiedName, RequestValidator.Current.GetType ().AssemblyQualifiedName, "#A2");
78                         AssertExtensions.Throws<ArgumentNullException> (() => {
79                                 RequestValidator.Current = null;
80                         }, "#A3");
81
82                         RequestValidator.Current = new TestRequestValidator ();
83                         Assert.IsNotNull (RequestValidator.Current, "#B1");
84                         Assert.AreEqual (typeof (TestRequestValidator).AssemblyQualifiedName, RequestValidator.Current.GetType ().AssemblyQualifiedName, "#B2");
85                 }
86
87                 [Test]
88                 public void IsValidRequestString ()
89                 {
90                         var rv = new TestRequestValidator ();
91                         int validationFailureIndex;
92                         var fr = new FakeHttpWorkerRequest ("http://localhost/default.aspx?key=invalid%value");
93                         var ctx = new HttpContext (fr);
94
95                         Assert.IsFalse (rv.DoIsValidRequestString (null, "<script>invalid%value", RequestValidationSource.QueryString, "key", out validationFailureIndex), "#A1");
96                         Assert.IsFalse (rv.DoIsValidRequestString (ctx, "<script>invalid%value", RequestValidationSource.QueryString, "key", out validationFailureIndex), "#A2");
97                 }
98         }
99 }
100 #endif