2 // Marek Habersack <mhabersack@novell.com>
4 // Copyright (C) 2010 Novell Inc. http://novell.com
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
28 using System.Collections.Generic;
29 using System.Diagnostics;
32 using System.Web.Caching;
33 using System.Web.Util;
35 using NUnit.Framework;
36 using MonoTests.Common;
38 namespace MonoTests.System.Web.Util
40 class TestRequestValidator : RequestValidator
42 public bool DoIsValidRequestString (HttpContext context, string value, RequestValidationSource requestValidationSource,
43 string collectionKey, out int validationFailureIndex)
45 return IsValidRequestString (context, value, requestValidationSource, collectionKey, out validationFailureIndex);
50 public class RequestValidatorTest
52 class FakeHttpWorkerRequest : BaseFakeHttpWorkerRequest
56 public FakeHttpWorkerRequest (string url)
58 uri = new Uri (url, UriKind.Absolute);
61 public override string GetQueryString ()
66 public override string GetUriPath ()
68 return uri.AbsolutePath;
73 public void Current ()
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;
81 RequestValidator.Current = new TestRequestValidator ();
82 Assert.IsNotNull (RequestValidator.Current, "#B1");
83 Assert.AreEqual (typeof (TestRequestValidator).AssemblyQualifiedName, RequestValidator.Current.GetType ().AssemblyQualifiedName, "#B2");
87 public void IsValidRequestString ()
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);
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");