Skip to content

Commit 0d6718e

Browse files
committed
Some refactoring
1 parent d0556a5 commit 0d6718e

22 files changed

+267
-294
lines changed

Patronum.Actions/ApplicationAction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ protected ApplicationActions(IApplicationUnderTest app)
1111
ApplicationUnderTest = app;
1212
}
1313
}
14-
}
14+
}

Patronum.Actions/ApplicationActor.cs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ protected ApplicationActor(IApplicationUnderTest app)
1414
ApplicationUnderTest = app;
1515
}
1616

17-
1817
public abstract bool SignIn();
1918

2019
public abstract bool SignOut();

Patronum.Actions/Patronum.Actions.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<Compile Include="ApplicationAction.cs" />
4040
</ItemGroup>
4141
<ItemGroup>
42-
<ProjectReference Include="..\2Gis.TestingTools.Patronum\Patronum.WebService.Testing.csproj">
42+
<ProjectReference Include="..\Patronum.WebService.Testing\Patronum.WebService.Testing.csproj">
4343
<Project>{6fbd0dc3-5d17-4568-b1f5-88613536c646}</Project>
4444
<Name>Patronum.WebService.Testing</Name>
4545
</ProjectReference>

Patronum.Actions/Settings.StyleCop

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@
1717
<BooleanProperty Name="Enabled">False</BooleanProperty>
1818
</RuleSettings>
1919
</Rule>
20+
<Rule Name="UsingDirectivesMustBePlacedWithinNamespace">
21+
<RuleSettings>
22+
<BooleanProperty Name="Enabled">False</BooleanProperty>
23+
</RuleSettings>
24+
</Rule>
25+
</Rules>
26+
<AnalyzerSettings />
27+
</Analyzer>
28+
<Analyzer AnalyzerId="StyleCop.CSharp.ReadabilityRules">
29+
<Rules>
30+
<Rule Name="DoNotPrefixCallsWithBaseUnlessLocalImplementationExists">
31+
<RuleSettings>
32+
<BooleanProperty Name="Enabled">False</BooleanProperty>
33+
</RuleSettings>
34+
</Rule>
35+
<Rule Name="PrefixLocalCallsWithThis">
36+
<RuleSettings>
37+
<BooleanProperty Name="Enabled">False</BooleanProperty>
38+
</RuleSettings>
39+
</Rule>
40+
<Rule Name="PrefixCallsCorrectly">
41+
<RuleSettings>
42+
<BooleanProperty Name="Enabled">False</BooleanProperty>
43+
</RuleSettings>
44+
</Rule>
2045
</Rules>
2146
<AnalyzerSettings />
2247
</Analyzer>

Patronum.WebService.Testing/Exceptions/LogException.cs

-28
This file was deleted.

Patronum.WebService.Testing/Helpers/JsonSourceHelper.cs

-63
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
5+
namespace Patronum.WebService.Testing.HttpRequest
6+
{
7+
public enum RequestMethod
8+
{
9+
Post,
10+
Get
11+
}
12+
13+
public class HttpRequest
14+
{
15+
protected readonly NetworkCredential Credential;
16+
17+
protected readonly Uri ServiceUri;
18+
19+
20+
public HttpRequest(Uri uri, NetworkCredential credential)
21+
{
22+
ServiceUri = uri;
23+
Credential = credential;
24+
}
25+
26+
protected HttpResponse Request(HttpWebRequest request)
27+
{
28+
var response = new HttpResponse();
29+
30+
try
31+
{
32+
var result = (HttpWebResponse)request.GetResponse();
33+
response.Code = (int)result.StatusCode;
34+
35+
var responseStream = result.GetResponseStream();
36+
if (responseStream != null)
37+
{
38+
using (var reader = new StreamReader(responseStream))
39+
{
40+
response.Text = reader.ReadToEnd();
41+
}
42+
}
43+
}
44+
catch (Exception e)
45+
{
46+
try
47+
{
48+
response.Code = (int)((HttpWebResponse)((WebException)e).Response).StatusCode;
49+
}
50+
catch (Exception)
51+
{
52+
response.Code = 500;
53+
}
54+
}
55+
56+
return response;
57+
}
58+
}
59+
}

Patronum.WebService.Testing/WebServiceClients/Request/HttpResponse.cs Patronum.WebService.Testing/HttpRequest/HttpResponse.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
namespace Patronum.WebService.Testing.WebServiceClients.Request
2+
namespace Patronum.WebService.Testing.HttpRequest
33
{
44
public struct HttpResponse
55
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Web;
7+
8+
namespace Patronum.WebService.Testing.HttpRequest.Request
9+
{
10+
public class RestRequest : HttpRequest
11+
{
12+
public RestRequest(Uri uri, NetworkCredential credential) : base(uri, credential)
13+
{
14+
}
15+
16+
public HttpResponse Request(Dictionary<string, string> parameters, RequestMethod method = RequestMethod.Get)
17+
{
18+
string data = this.PrepareRequestData(parameters);
19+
20+
var request = method.Equals(RequestMethod.Get) ? PrepareGetRequest(data) : PreparePostRequest(data);
21+
request.Credentials = new CredentialCache { { ServiceUri, "Negotiate", Credential } };
22+
23+
return base.Request(request);
24+
}
25+
26+
protected HttpWebRequest PreparePostRequest(string data)
27+
{
28+
var request = (HttpWebRequest)WebRequest.Create(ServiceUri);
29+
30+
request.Method = "POST";
31+
32+
request.ContentType = "application/x-www-form-urlencoded";
33+
using (var dataStream = new StreamWriter(request.GetRequestStream()))
34+
{
35+
dataStream.Write(data);
36+
dataStream.Flush();
37+
}
38+
39+
return request;
40+
}
41+
42+
protected HttpWebRequest PrepareGetRequest(string data)
43+
{
44+
var request = (HttpWebRequest)WebRequest.Create(new Uri(ServiceUri, "?" + data));
45+
46+
request.Method = "GET";
47+
48+
return request;
49+
}
50+
51+
protected string PrepareRequestData(Dictionary<string, string> parameters)
52+
{
53+
var result = string.Empty;
54+
55+
if (parameters == null)
56+
{
57+
return result;
58+
}
59+
60+
result = parameters.Aggregate(result, (current, parameter) => current + (parameter.Key + '=' + HttpUtility.UrlEncode(parameter.Value) + '&'));
61+
return result.Trim(new[] { '&' });
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Net;
3+
using System.Xml.Linq;
4+
5+
namespace Patronum.WebService.Testing.HttpRequest.Request
6+
{
7+
public class SoapRequest : HttpRequest
8+
{
9+
protected readonly string Action;
10+
11+
public SoapRequest(Uri uri, string action, NetworkCredential credential) : base(uri, credential)
12+
{
13+
Action = action;
14+
}
15+
16+
17+
public HttpResponse Request(XDocument soapEnvelop)
18+
{
19+
HttpWebRequest webRequest = PrepareSoapRequest(ServiceUri, Action);
20+
InsertSoapEnvelopeIntoWebRequest(soapEnvelop, webRequest);
21+
22+
return base.Request(webRequest);
23+
}
24+
25+
private HttpWebRequest PrepareSoapRequest(Uri uri, string action)
26+
{
27+
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
28+
webRequest.Headers.Add("SOAPAction", action);
29+
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
30+
webRequest.Accept = "text/xml";
31+
webRequest.Method = "POST";
32+
33+
webRequest.Credentials = Credential;
34+
35+
return webRequest;
36+
}
37+
38+
private void InsertSoapEnvelopeIntoWebRequest(XDocument soapEnvelopeXml, HttpWebRequest webRequest)
39+
{
40+
using (var stream = webRequest.GetRequestStream())
41+
{
42+
soapEnvelopeXml.Save(stream);
43+
}
44+
}
45+
}
46+
}

Patronum.WebService.Testing/Patronum.WebService.Testing.csproj

+7-8
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,21 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45-
<Compile Include="Exceptions\LogException.cs" />
45+
<Compile Include="HttpRequest\Request\RestRequest.cs" />
4646
<Compile Include="IApplicationUnderTest.cs" />
47-
<Compile Include="WebServiceClients\Request\SoapRequest.cs" />
47+
<Compile Include="HttpRequest\Request\SoapRequest.cs" />
4848
<Compile Include="WebServiceClients\WebServiceMethod.cs" />
4949
<Compile Include="WebServiceUnderTest.cs" />
5050
<Compile Include="WebServiceTestCase.cs" />
5151
<Compile Include="Exceptions\NotFoundTestDataException.cs" />
5252
<Compile Include="Helpers\HtmlSourceHelper.cs" />
53-
<Compile Include="Helpers\JsonSourceHelper.cs" />
5453
<Compile Include="Properties\AssemblyInfo.cs" />
5554
<Compile Include="WebServiceClients\ClientCreator.cs" />
56-
<Compile Include="WebServiceClients\Clients\Controller\ControllerClient.cs" />
57-
<Compile Include="WebServiceClients\Request\HttpResponse.cs" />
58-
<Compile Include="WebServiceClients\Request\HttpRequest.cs" />
59-
<Compile Include="WebServiceClients\Clients\Rest\RestClient.cs" />
60-
<Compile Include="WebServiceClients\Clients\Soap\SoapClient.cs" />
55+
<Compile Include="WebServiceClients\Controller\ControllerClient.cs" />
56+
<Compile Include="HttpRequest\HttpResponse.cs" />
57+
<Compile Include="HttpRequest\HttpRequest.cs" />
58+
<Compile Include="WebServiceClients\Rest\RestClient.cs" />
59+
<Compile Include="WebServiceClients\Soap\SoapClient.cs" />
6160
<Compile Include="TestUser.cs" />
6261
<Compile Include="WebServiceClients\WebServiceClient.cs" />
6362
</ItemGroup>

Patronum.WebService.Testing/Settings.StyleCop

+10
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@
222222
<BooleanProperty Name="Enabled">False</BooleanProperty>
223223
</RuleSettings>
224224
</Rule>
225+
<Rule Name="DoNotPrefixCallsWithBaseUnlessLocalImplementationExists">
226+
<RuleSettings>
227+
<BooleanProperty Name="Enabled">False</BooleanProperty>
228+
</RuleSettings>
229+
</Rule>
230+
<Rule Name="PrefixCallsCorrectly">
231+
<RuleSettings>
232+
<BooleanProperty Name="Enabled">False</BooleanProperty>
233+
</RuleSettings>
234+
</Rule>
225235
</Rules>
226236
<AnalyzerSettings />
227237
</Analyzer>

Patronum.WebService.Testing/TestUser.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ public TestUser(long id, string name, string passwd, string username = "")
2121
public string Password { get; set; }
2222

2323

24-
public NetworkCredential GetNetworkCredential()
24+
public NetworkCredential NetworkCredential
2525
{
26-
return new NetworkCredential("2gis\\" + Name, Password);
26+
get
27+
{
28+
return string.IsNullOrEmpty(Name)
29+
? CredentialCache.DefaultNetworkCredentials
30+
: new NetworkCredential(Name, Password);
31+
}
2732
}
2833
}
2934
}

0 commit comments

Comments
 (0)