Skip to content

Commit

Permalink
fix #288 broken UsePrettyReponses()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Jul 26, 2013
1 parent 6b92005 commit c84ad58
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 129 deletions.
28 changes: 28 additions & 0 deletions src/Nest.Tests.Unit/Settings/UsePrettyResponseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using FluentAssertions;
using NUnit.Framework;

namespace Nest.Tests.Unit.Settings
{
[TestFixture]
public class UsePrettyResponses : BaseJsonTests
{
[Test]
public void UsePrettyResponsesShouldSurviveUrlModififications()
{
var settings = new ConnectionSettings(Test.Default.Uri)
.SetDefaultIndex(Test.Default.DefaultIndex)
.UsePrettyResponses();
var connection = new InMemoryConnection(settings);
var client = new ElasticClient(settings, connection);

var r = client.Health(HealthLevel.Cluster);
var u = new Uri(r.ConnectionStatus.RequestUrl);
u.AbsolutePath.Should().StartWith("/_cluster/health");
u.Query.Should().Contain("level=cluster");

u.Query.Should().Contain("pretty=true");
}

}
}
263 changes: 134 additions & 129 deletions src/Nest/Domain/Connection/ConnectionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,56 @@

namespace Nest
{
public class ConnectionSettings : IConnectionSettings
{
private string _defaultIndex;
public string DefaultIndex
{
get
{
if (this._defaultIndex.IsNullOrEmpty())
throw new NullReferenceException("No default index set on connection!");
return this._defaultIndex;
}
private set { this._defaultIndex = value; }
}
public Uri Uri { get; private set; }
public string Host { get; private set; }
public int Port { get; private set; }
public int Timeout { get; private set; }
public string ProxyUsername { get; private set; }
public string ProxyPassword { get; private set; }
public string ProxyAddress { get; private set; }

public int MaximumAsyncConnections { get; private set; }
public bool UsesPrettyResponses { get; private set; }
public class ConnectionSettings : IConnectionSettings
{
private string _defaultIndex;
public string DefaultIndex
{
get
{
if (this._defaultIndex.IsNullOrEmpty())
throw new NullReferenceException("No default index set on connection!");
return this._defaultIndex;
}
private set { this._defaultIndex = value; }
}
public Uri Uri { get; private set; }
public string Host { get; private set; }
public int Port { get; private set; }
public int Timeout { get; private set; }
public string ProxyUsername { get; private set; }
public string ProxyPassword { get; private set; }
public string ProxyAddress { get; private set; }

public int MaximumAsyncConnections { get; private set; }
public bool UsesPrettyResponses { get; private set; }
public bool TraceEnabled { get; private set; }
public bool DontDoubleEscapePathDotsAndSlashes { get; private set; }

public Func<Type, string> DefaultTypeNameInferrer { get; private set; }
public Action<ConnectionStatus> ConnectionStatusHandler { get; private set; }
public FluentDictionary<Type, string> DefaultIndices { get; private set; }
public FluentDictionary<Type, string> DefaultTypeNames { get; private set; }
public NameValueCollection QueryStringParameters { get; private set; }
public Func<Type, string> DefaultTypeNameInferrer { get; private set; }
public Action<ConnectionStatus> ConnectionStatusHandler { get; private set; }
public FluentDictionary<Type, string> DefaultIndices { get; private set; }
public FluentDictionary<Type, string> DefaultTypeNames { get; private set; }
public NameValueCollection QueryStringParameters { get; private set; }

public ConnectionSettings(Uri uri)
{
uri.ThrowIfNull("uri");
public ConnectionSettings(Uri uri)
{
uri.ThrowIfNull("uri");

this.Timeout = 60 * 1000;
this.Timeout = 60 * 1000;

this.Uri = uri;
if (!uri.ToString().EndsWith("/"))
this.Uri = new Uri(uri.ToString() + "/");
this.Host = uri.Host;
this.Port = uri.Port;
this.Uri = uri;
if (!uri.ToString().EndsWith("/"))
this.Uri = new Uri(uri.ToString() + "/");
this.Host = uri.Host;
this.Port = uri.Port;

this.MaximumAsyncConnections = 20;
this.DefaultTypeNameInferrer = this.LowerCaseAndPluralizeTypeNameInferrer;
this.DefaultIndices = new FluentDictionary<Type, string>();
this.DefaultTypeNames = new FluentDictionary<Type, string>();
this.ConnectionStatusHandler = this.ConnectionStatusDefaultHandler;
}
this.MaximumAsyncConnections = 20;
this.DefaultTypeNameInferrer = this.LowerCaseAndPluralizeTypeNameInferrer;
this.DefaultIndices = new FluentDictionary<Type, string>();
this.DefaultTypeNames = new FluentDictionary<Type, string>();
this.ConnectionStatusHandler = this.ConnectionStatusDefaultHandler;
}

/// <summary>
/// Enable Trace signals to the IConnection that it should put debug information on the Trace.
Expand All @@ -72,6 +72,10 @@ public ConnectionSettings EnableTrace(bool enabled = true)
/// <returns></returns>
public ConnectionSettings SetGlobalQueryStringParameters(NameValueCollection queryStringParameters)
{
if (this.QueryStringParameters != null)
{
this.QueryStringParameters.Add(queryStringParameters);
}
this.QueryStringParameters = queryStringParameters;
return this;
}
Expand All @@ -86,53 +90,54 @@ public ConnectionSettings SetTimeout(int timeout)
this.Timeout = timeout;
return this;
}
/// <summary>
/// Index to default to when no index is specified.
/// </summary>
/// <param name="defaultIndex">When null/empty/not set might throw NRE later on
/// when not specifying index explicitly while indexing.
/// </param>
/// <returns></returns>
public ConnectionSettings SetDefaultIndex(string defaultIndex)
{
this.DefaultIndex = defaultIndex;
return this;
}
/// <summary>
/// Semaphore asynchronous connections automatically by giving
/// it a maximum concurrent connections. Great to prevent
/// out of memory exceptions
/// </summary>
/// <param name="maximum">defaults to 20</param>
/// <returns></returns>
public ConnectionSettings SetMaximumAsyncConnections(int maximum)
{
this.MaximumAsyncConnections = maximum;
return this;
}

/// <summary>
/// If your connection has to go through proxy use this method to specify the proxy url
/// </summary>
/// <returns></returns>
public ConnectionSettings SetProxy(Uri proxyAdress, string username, string password)
{
proxyAdress.ThrowIfNull("proxyAdress");
this.ProxyAddress = proxyAdress.ToString();
this.ProxyUsername = username;
this.ProxyPassword = password;
return this;
}

/// <summary>
/// Append ?pretty=true to requests, this helps to debug send and received json.
/// </summary>
/// <returns></returns>
public ConnectionSettings UsePrettyResponses(bool b = true)
{
this.UsesPrettyResponses = b;
return this;
}
/// <summary>
/// Index to default to when no index is specified.
/// </summary>
/// <param name="defaultIndex">When null/empty/not set might throw NRE later on
/// when not specifying index explicitly while indexing.
/// </param>
/// <returns></returns>
public ConnectionSettings SetDefaultIndex(string defaultIndex)
{
this.DefaultIndex = defaultIndex;
return this;
}
/// <summary>
/// Semaphore asynchronous connections automatically by giving
/// it a maximum concurrent connections. Great to prevent
/// out of memory exceptions
/// </summary>
/// <param name="maximum">defaults to 20</param>
/// <returns></returns>
public ConnectionSettings SetMaximumAsyncConnections(int maximum)
{
this.MaximumAsyncConnections = maximum;
return this;
}

/// <summary>
/// If your connection has to go through proxy use this method to specify the proxy url
/// </summary>
/// <returns></returns>
public ConnectionSettings SetProxy(Uri proxyAdress, string username, string password)
{
proxyAdress.ThrowIfNull("proxyAdress");
this.ProxyAddress = proxyAdress.ToString();
this.ProxyUsername = username;
this.ProxyPassword = password;
return this;
}

/// <summary>
/// Append ?pretty=true to requests, this helps to debug send and received json.
/// </summary>
/// <returns></returns>
public ConnectionSettings UsePrettyResponses(bool b = true)
{
this.UsesPrettyResponses = b;
this.SetGlobalQueryStringParameters(new NameValueCollection { { "pretty", b.ToString().ToLowerInvariant() } });
return this;
}

/// <summary>
/// Append ?pretty=true to requests, this helps to debug send and received json.
Expand All @@ -144,43 +149,43 @@ public ConnectionSettings SetDontDoubleEscapePathDotsAndSlashes(bool b = true)
return this;
}

private string LowerCaseAndPluralizeTypeNameInferrer(Type type)
{
type.ThrowIfNull("type");
return Inflector.MakePlural(type.Name).ToLower();
}

private void ConnectionStatusDefaultHandler(ConnectionStatus status)
{
return;
}

public ConnectionSettings SetDefaultTypeNameInferrer(Func<Type, string> defaultTypeNameInferrer)
{
defaultTypeNameInferrer.ThrowIfNull("defaultTypeNameInferrer");
this.DefaultTypeNameInferrer = defaultTypeNameInferrer;
return this;
}

public ConnectionSettings SetConnectionStatusHandler(Action<ConnectionStatus> handler)
{
handler.ThrowIfNull("handler");
this.ConnectionStatusHandler = handler;
return this;
}

public ConnectionSettings MapDefaultTypeIndices(Action<FluentDictionary<Type, string>> mappingSelector)
{
mappingSelector.ThrowIfNull("mappingSelector");
mappingSelector(this.DefaultIndices);
return this;
}
public ConnectionSettings MapDefaultTypeNames(Action<FluentDictionary<Type, string>> mappingSelector)
{
mappingSelector.ThrowIfNull("mappingSelector");
mappingSelector(this.DefaultTypeNames);
return this;
}

}
private string LowerCaseAndPluralizeTypeNameInferrer(Type type)
{
type.ThrowIfNull("type");
return Inflector.MakePlural(type.Name).ToLower();
}

private void ConnectionStatusDefaultHandler(ConnectionStatus status)
{
return;
}

public ConnectionSettings SetDefaultTypeNameInferrer(Func<Type, string> defaultTypeNameInferrer)
{
defaultTypeNameInferrer.ThrowIfNull("defaultTypeNameInferrer");
this.DefaultTypeNameInferrer = defaultTypeNameInferrer;
return this;
}

public ConnectionSettings SetConnectionStatusHandler(Action<ConnectionStatus> handler)
{
handler.ThrowIfNull("handler");
this.ConnectionStatusHandler = handler;
return this;
}

public ConnectionSettings MapDefaultTypeIndices(Action<FluentDictionary<Type, string>> mappingSelector)
{
mappingSelector.ThrowIfNull("mappingSelector");
mappingSelector(this.DefaultIndices);
return this;
}
public ConnectionSettings MapDefaultTypeNames(Action<FluentDictionary<Type, string>> mappingSelector)
{
mappingSelector.ThrowIfNull("mappingSelector");
mappingSelector(this.DefaultTypeNames);
return this;
}

}
}

0 comments on commit c84ad58

Please sign in to comment.