Skip to content

Commit

Permalink
cleaned up code; removed duplicate code; cleaned up references;
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hallett committed Aug 16, 2015
1 parent 6d75367 commit 2a239b0
Show file tree
Hide file tree
Showing 99 changed files with 3,208 additions and 2,910 deletions.
Binary file modified .nuget/RestSharp.Build.dll
Binary file not shown.
Binary file modified .nuget/Signed/RestSharp.Build.dll
Binary file not shown.
42 changes: 21 additions & 21 deletions RestSharp.Build/NuSpecUpdateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace RestSharp.Build
{
public class NuSpecUpdateTask : Task
{
private Assembly _assembly;
private Assembly assembly;

public string Id { get; private set; }

Expand All @@ -27,7 +27,7 @@ public NuSpecUpdateTask() : this(null) { }

public NuSpecUpdateTask(Assembly assembly)
{
this._assembly = assembly;
this.assembly = assembly;
}

public override bool Execute()
Expand All @@ -36,18 +36,18 @@ public override bool Execute()
return false;

var path = Path.GetFullPath(this.SourceAssemblyFile);
this._assembly = this._assembly ?? Assembly.LoadFile(path);
this.assembly = this.assembly ?? Assembly.LoadFile(path);

var name = this._assembly.GetName();
var name = this.assembly.GetName();

#if SIGNED
this.Id = name.Name + "Signed";
#else
this.Id = name.Name;
#endif
this.Authors = this.GetAuthors(this._assembly);
this.Description = this.GetDescription(this._assembly);
this.Version = this.GetVersion(this._assembly);
this.Authors = GetAuthors(this.assembly);
this.Description = GetDescription(this.assembly);
this.Version = GetVersion(this.assembly);

this.GenerateComputedSpecFile();

Expand All @@ -59,16 +59,16 @@ private void GenerateComputedSpecFile()
var doc = XDocument.Load(this.SpecFile);
var metaNode = doc.Descendants("metadata").First();

this.ReplaceToken(metaNode, "id", this.Id);
this.ReplaceToken(metaNode, "authors", this.Authors);
this.ReplaceToken(metaNode, "owners", this.Authors);
this.ReplaceToken(metaNode, "description", this.Description);
this.ReplaceToken(metaNode, "version", this.Version);
ReplaceToken(metaNode, "id", this.Id);
ReplaceToken(metaNode, "authors", this.Authors);
ReplaceToken(metaNode, "owners", this.Authors);
ReplaceToken(metaNode, "description", this.Description);
ReplaceToken(metaNode, "version", this.Version);

doc.Save(this.SpecFile.Replace(".nuspec", "-computed.nuspec"));
}

private void ReplaceToken(XElement metaNode, XName name, string value)
private static void ReplaceToken(XContainer metaNode, XName name, string value)
{
var node = metaNode.Element(name);
var token = string.Format("${0}$", name.ToString().TrimEnd('s'));
Expand All @@ -78,26 +78,26 @@ private void ReplaceToken(XElement metaNode, XName name, string value)
token = "$author$";
}

if (node.Value.Equals(token, StringComparison.OrdinalIgnoreCase))
if (node != null && node.Value.Equals(token, StringComparison.OrdinalIgnoreCase))
{
node.SetValue(value);
}
}

private string GetDescription(Assembly asm)
private static string GetDescription(ICustomAttributeProvider asm)
{
return this.GetAttribute<AssemblyDescriptionAttribute>(asm).Description;
return GetAttribute<AssemblyDescriptionAttribute>(asm).Description;
}

private string GetAuthors(Assembly asm)
private static string GetAuthors(ICustomAttributeProvider asm)
{
return this.GetAttribute<AssemblyCompanyAttribute>(asm).Company;
return GetAttribute<AssemblyCompanyAttribute>(asm).Company;
}

private string GetVersion(Assembly asm)
private static string GetVersion(Assembly asm)
{
var version = asm.GetName().Version.ToString();
var attr = this.GetAttribute<AssemblyInformationalVersionAttribute>(asm);
var attr = GetAttribute<AssemblyInformationalVersionAttribute>(asm);

if (attr != null)
{
Expand All @@ -107,7 +107,7 @@ private string GetVersion(Assembly asm)
return version;
}

private TAttr GetAttribute<TAttr>(Assembly asm) where TAttr : Attribute
private static TAttr GetAttribute<TAttr>(ICustomAttributeProvider asm) where TAttr : Attribute
{
var attrs = asm.GetCustomAttributes(typeof(TAttr), false);

Expand Down
1 change: 0 additions & 1 deletion RestSharp.IntegrationTests/AsyncRequestBodyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public void Can_Have_No_Body_Added_To_POST_Request()
{
var client = new RestClient(BASE_URL);
var request = new RestRequest(RequestBodyCapturer.RESOURCE, httpMethod);

var resetEvent = new ManualResetEvent(false);

client.ExecuteAsync(request, response => resetEvent.Set());
Expand Down
10 changes: 5 additions & 5 deletions RestSharp.IntegrationTests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ public void Can_Perform_GET_TaskAsync()
public void Can_Handle_Exception_Thrown_By_OnBeforeDeserialization_Handler()
{
const string baseUrl = "http://localhost:8888/";
const string ExceptionMessage = "Thrown from OnBeforeDeserialization";
const string exceptionMessage = "Thrown from OnBeforeDeserialization";

using (SimpleServer.Create(baseUrl, Handlers.Generic<ResponseHandler>()))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("success");

request.OnBeforeDeserialization += r => { throw new Exception(ExceptionMessage); };
request.OnBeforeDeserialization += r => { throw new Exception(exceptionMessage); };

var task = client.ExecuteTaskAsync<Response>(request);

task.Wait();

var response = task.Result;

Assert.AreEqual(ExceptionMessage, response.ErrorMessage);
Assert.AreEqual(exceptionMessage, response.ErrorMessage);
Assert.AreEqual(ResponseStatus.Error, response.ResponseStatus);
}
}
Expand Down Expand Up @@ -217,7 +217,7 @@ public void Can_Timeout_GET_TaskAsync()
var client = new RestClient(baseUrl);
var request = new RestRequest("timeout", Method.GET).AddBody("Body_Content");

//Half the value of ResponseHandler.Timeout
// Half the value of ResponseHandler.Timeout
request.Timeout = 500;

var task = client.ExecuteTaskAsync(request);
Expand All @@ -240,7 +240,7 @@ public void Can_Timeout_PUT_TaskAsync()
var client = new RestClient(baseUrl);
var request = new RestRequest("timeout", Method.PUT).AddBody("Body_Content");

//Half the value of ResponseHandler.Timeout
// Half the value of ResponseHandler.Timeout
request.Timeout = 500;

var task = client.ExecuteTaskAsync(request);
Expand Down
4 changes: 2 additions & 2 deletions RestSharp.IntegrationTests/AuthenticationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using NUnit.Framework;
using RestSharp.Authenticators;
using RestSharp.Contrib;
using RestSharp.Extensions.MonoHttp;
using RestSharp.IntegrationTests.Helpers;

namespace RestSharp.IntegrationTests
Expand All @@ -17,7 +17,7 @@ public void Can_Authenticate_With_Basic_Http_Auth()
{
Uri baseUrl = new Uri("http://localhost:8888/");

using(SimpleServer.Create(baseUrl.AbsoluteUri, UsernamePasswordEchoHandler))
using (SimpleServer.Create(baseUrl.AbsoluteUri, UsernamePasswordEchoHandler))
{
var client = new RestClient(baseUrl)
{
Expand Down
34 changes: 17 additions & 17 deletions RestSharp.IntegrationTests/CompressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Can_Handle_Gzip_Compressed_Content()
{
Uri baseUrl = new Uri("http://localhost:8888/");

using(SimpleServer.Create(baseUrl.AbsoluteUri, GzipEchoValue("This is some gzipped content")))
using (SimpleServer.Create(baseUrl.AbsoluteUri, GzipEchoValue("This is some gzipped content")))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("");
Expand All @@ -29,7 +29,7 @@ public void Can_Handle_Deflate_Compressed_Content()
{
Uri baseUrl = new Uri("http://localhost:8888/");

using(SimpleServer.Create(baseUrl.AbsoluteUri, DeflateEchoValue("This is some deflated content")))
using (SimpleServer.Create(baseUrl.AbsoluteUri, DeflateEchoValue("This is some deflated content")))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("");
Expand All @@ -44,7 +44,7 @@ public void Can_Handle_Uncompressed_Content()
{
Uri baseUrl = new Uri("http://localhost:8888/");

using(SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.EchoValue("This is some sample content")))
using (SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.EchoValue("This is some sample content")))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("");
Expand All @@ -57,27 +57,27 @@ public void Can_Handle_Uncompressed_Content()
static Action<HttpListenerContext> GzipEchoValue(string value)
{
return context =>
{
context.Response.Headers.Add("Content-encoding", "gzip");
{
context.Response.Headers.Add("Content-encoding", "gzip");
using (var gzip = new GZipStream(context.Response.OutputStream, CompressionMode.Compress, true))
{
gzip.WriteStringUtf8(value);
}
};
using (var gzip = new GZipStream(context.Response.OutputStream, CompressionMode.Compress, true))
{
gzip.WriteStringUtf8(value);
}
};
}

static Action<HttpListenerContext> DeflateEchoValue(string value)
{
return context =>
{
context.Response.Headers.Add("Content-encoding", "deflate");
{
context.Response.Headers.Add("Content-encoding", "deflate");
using (var gzip = new DeflateStream(context.Response.OutputStream, CompressionMode.Compress, true))
{
gzip.WriteStringUtf8(value);
}
};
using (var gzip = new DeflateStream(context.Response.OutputStream, CompressionMode.Compress, true))
{
gzip.WriteStringUtf8(value);
}
};
}
}
}
2 changes: 1 addition & 1 deletion RestSharp.IntegrationTests/FileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void Handles_Binary_File_Download()
{
Uri baseUrl = new Uri("http://localhost:8888/");

using(SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.FileHandler))
using (SimpleServer.Create(baseUrl.AbsoluteUri, Handlers.FileHandler))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("Assets/Koala.jpg");
Expand Down
26 changes: 13 additions & 13 deletions RestSharp.IntegrationTests/Helpers/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ public static void FileHandler(HttpListenerContext context)
public static Action<HttpListenerContext> Generic<T>() where T : new()
{
return ctx =>
{
var methodName = ctx.Request.Url.Segments.Last();
var method = typeof(T).GetMethod(methodName,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
{
var methodName = ctx.Request.Url.Segments.Last();
var method = typeof(T).GetMethod(methodName,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
if (method.IsStatic)
{
method.Invoke(null, new object[] { ctx });
}
else
{
method.Invoke(new T(), new object[] { ctx });
}
};
if (method.IsStatic)
{
method.Invoke(null, new object[] { ctx });
}
else
{
method.Invoke(new T(), new object[] { ctx });
}
};
}
}
}
31 changes: 23 additions & 8 deletions RestSharp.IntegrationTests/MultipartFormDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ public void MultipartFormData_WithParameterAndFile_Async()
{
var client = new RestClient(baseUrl);
var request = new RestRequest("/", Method.POST) { AlwaysMultipartFormData = true };
string path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName, "Assets\\TestFile.txt");
var directoryInfo = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;

if (directoryInfo != null)
{
string path = Path.Combine(directoryInfo.FullName,
"Assets\\TestFile.txt");

request.AddFile("fileName", path);
}

request.AddFile("fileName", path);
request.AddParameter("controlName", "test", "application/json", ParameterType.RequestBody);

var task = client.ExecuteTaskAsync(request).ContinueWith(x =>
{
Assert.AreEqual(this.expectedFileAndBodyRequestContent, x.Result.Content);
});
var task = client.ExecuteTaskAsync(request)
.ContinueWith(x =>
{
Assert.AreEqual(this.expectedFileAndBodyRequestContent, x.Result.Content);
});

task.Wait();
}
Expand All @@ -62,9 +70,16 @@ public void MultipartFormData_WithParameterAndFile()
{
var client = new RestClient(baseUrl);
var request = new RestRequest("/", Method.POST) { AlwaysMultipartFormData = true };
string path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName, "Assets\\TestFile.txt");
var directoryInfo = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;

if (directoryInfo != null)
{
string path = Path.Combine(directoryInfo.FullName,
"Assets\\TestFile.txt");

request.AddFile("fileName", path);
}

request.AddFile("fileName", path);
request.AddParameter("controlName", "test", "application/json", ParameterType.RequestBody);

var response = client.Execute(request);
Expand Down
13 changes: 7 additions & 6 deletions RestSharp.IntegrationTests/NonProtocolExceptionHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public void Task_Handles_Non_Existent_Domain()
{
var client = new RestClient("http://192.168.1.200:8001");
var request = new RestRequest("/")
{
RequestFormat = DataFormat.Json,
Method = Method.GET
};

{
RequestFormat = DataFormat.Json,
Method = Method.GET
};
var task = client.ExecuteTaskAsync<StupidClass>(request);

task.Wait();

var response = task.Result;
Expand Down Expand Up @@ -73,6 +73,7 @@ public void Handles_Server_Timeout_Error()
public void Handles_Server_Timeout_Error_Async()
{
const string baseUrl = "http://localhost:8888/";

var resetEvent = new ManualResetEvent(false);

using (SimpleServer.Create(baseUrl, TimeoutHandler))
Expand Down Expand Up @@ -106,7 +107,7 @@ public void Handles_Server_Timeout_Error_AsyncTask()
{
var client = new RestClient(baseUrl);
var request = new RestRequest("404") { Timeout = 500 };
var task = client.ExecuteTaskAsync(request);
var task = client.ExecuteTaskAsync(request);

task.Wait();

Expand Down
1 change: 1 addition & 0 deletions RestSharp.IntegrationTests/RequestBodyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class RequestBodyTests
public void Can_Not_Be_Added_To_GET_Request()
{
const Method httpMethod = Method.GET;

using (SimpleServer.Create(BASE_URL, Handlers.Generic<RequestBodyCapturer>()))
{
var client = new RestClient(BASE_URL);
Expand Down
Loading

0 comments on commit 2a239b0

Please sign in to comment.