forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestValidationTest.cs
More file actions
99 lines (83 loc) · 4.13 KB
/
RequestValidationTest.cs
File metadata and controls
99 lines (83 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Schema;
using System.Xml;
using System.Collections.Specialized;
using NUnit.Framework;
using SharpMap.Utilities.Wfs;
namespace UnitTests.WFS
{
[TestFixture]
[Ignore("Tests disabled because they download external schemas from the internet")]
public class RequestValidationTest
{
[OneTimeSetUp]
public void OneTimeSetUp()
{
GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();
}
[Test]
public void TestGETFilter1_1_0()
{
var featureTypeInfo = new WfsFeatureTypeInfo("http://localhost/", "nsPrefix", "featureTypeNamespace", "featureType", "geometryName", GeometryTypeEnum.PointPropertyType);
WFS_1_1_0_TextResources wfs = new WFS_1_1_0_TextResources();
string querystring = wfs.GetFeatureGETRequest(featureTypeInfo, new GeoAPI.Geometries.Envelope(1, 2, 3, 4), null, true);
NameValueCollection qscoll = ParseQueryString(querystring);
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas.Add("http://www.opengis.net/ogc", "http://schemas.opengis.net/filter/1.1.0/filter.xsd");
readerSettings.Schemas.Add("http://www.opengis.net/ogc", "http://schemas.opengis.net/filter/1.1.0/expr.xsd");
readerSettings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(qscoll["FILTER"]));
XmlTextReader xmlReader = new XmlTextReader(ms);
XmlReader objXmlReader = XmlReader.Create(xmlReader, readerSettings);
while (objXmlReader.Read()) { }
}
[Test]
public void TestPOSTFilter1_1_0()
{
var featureTypeInfo = new WfsFeatureTypeInfo("http://localhost/", "nsPrefix", "featureTypeNamespace", "featureType", "geometryName", GeometryTypeEnum.PointPropertyType);
WFS_1_1_0_TextResources wfs = new WFS_1_1_0_TextResources();
byte[] request = wfs.GetFeaturePOSTRequest(featureTypeInfo, "", new GeoAPI.Geometries.Envelope(1, 2, 3, 4), null, true);
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas.Add("http://www.opengis.net/wfs", "http://schemas.opengis.net/wfs/1.1.0/wfs.xsd");
readerSettings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
MemoryStream ms = new MemoryStream(request);
XmlTextReader xmlReader = new XmlTextReader(ms);
XmlReader objXmlReader = XmlReader.Create(xmlReader, readerSettings);
while (objXmlReader.Read()) { }
}
private void ValidationEventHandler(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Error)
{
Assert.Fail("Validation error: " + args.Message);
}
else if (args.Severity == XmlSeverityType.Warning)
{
Assert.Fail("Validation warning: " + args.Message);
}
}
private static NameValueCollection ParseQueryString(string s)
{
NameValueCollection queryParameters = new NameValueCollection();
s = s.TrimStart('?');
string[] querySegments = s.Split('&');
foreach (string segment in querySegments)
{
int equalIndex = segment.IndexOf('=');
if (equalIndex > 0)
{
string key = segment.Substring(0, equalIndex);
string val = Uri.UnescapeDataString(segment.Substring(equalIndex + 1));
queryParameters.Add(key, val);
}
}
return queryParameters;
}
}
}