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.
29 using System.Collections.Generic;
30 using System.Diagnostics;
33 using System.Web.Caching;
34 using System.Web.Util;
36 using NUnit.Framework;
37 using MonoTests.Common;
39 namespace MonoTests.System.Web.Util
41 class TestRequestValidator : RequestValidator
43 public bool DoIsValidRequestString (HttpContext context, string value, RequestValidationSource requestValidationSource,
44 string collectionKey, out int validationFailureIndex)
46 return IsValidRequestString (context, value, requestValidationSource, collectionKey, out validationFailureIndex);
51 public class RequestValidatorTest
53 class FakeHttpWorkerRequest : BaseFakeHttpWorkerRequest
57 public FakeHttpWorkerRequest (string url)
59 uri = new Uri (url, UriKind.Absolute);
62 public override string GetQueryString ()
67 public override string GetUriPath ()
69 return uri.AbsolutePath;
74 public void Current ()
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;
82 RequestValidator.Current = new TestRequestValidator ();
83 Assert.IsNotNull (RequestValidator.Current, "#B1");
84 Assert.AreEqual (typeof (TestRequestValidator).AssemblyQualifiedName, RequestValidator.Current.GetType ().AssemblyQualifiedName, "#B2");
88 public void IsValidRequestString ()
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);
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");