Skip to content

Commit c84ad58

Browse files
committed
fix #288 broken UsePrettyReponses()
1 parent 6b92005 commit c84ad58

File tree

2 files changed

+162
-129
lines changed

2 files changed

+162
-129
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
5+
namespace Nest.Tests.Unit.Settings
6+
{
7+
[TestFixture]
8+
public class UsePrettyResponses : BaseJsonTests
9+
{
10+
[Test]
11+
public void UsePrettyResponsesShouldSurviveUrlModififications()
12+
{
13+
var settings = new ConnectionSettings(Test.Default.Uri)
14+
.SetDefaultIndex(Test.Default.DefaultIndex)
15+
.UsePrettyResponses();
16+
var connection = new InMemoryConnection(settings);
17+
var client = new ElasticClient(settings, connection);
18+
19+
var r = client.Health(HealthLevel.Cluster);
20+
var u = new Uri(r.ConnectionStatus.RequestUrl);
21+
u.AbsolutePath.Should().StartWith("/_cluster/health");
22+
u.Query.Should().Contain("level=cluster");
23+
24+
u.Query.Should().Contain("pretty=true");
25+
}
26+
27+
}
28+
}

Diff for: src/Nest/Domain/Connection/ConnectionSettings.cs

+134-129
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,56 @@
44

55
namespace Nest
66
{
7-
public class ConnectionSettings : IConnectionSettings
8-
{
9-
private string _defaultIndex;
10-
public string DefaultIndex
11-
{
12-
get
13-
{
14-
if (this._defaultIndex.IsNullOrEmpty())
15-
throw new NullReferenceException("No default index set on connection!");
16-
return this._defaultIndex;
17-
}
18-
private set { this._defaultIndex = value; }
19-
}
20-
public Uri Uri { get; private set; }
21-
public string Host { get; private set; }
22-
public int Port { get; private set; }
23-
public int Timeout { get; private set; }
24-
public string ProxyUsername { get; private set; }
25-
public string ProxyPassword { get; private set; }
26-
public string ProxyAddress { get; private set; }
27-
28-
public int MaximumAsyncConnections { get; private set; }
29-
public bool UsesPrettyResponses { get; private set; }
7+
public class ConnectionSettings : IConnectionSettings
8+
{
9+
private string _defaultIndex;
10+
public string DefaultIndex
11+
{
12+
get
13+
{
14+
if (this._defaultIndex.IsNullOrEmpty())
15+
throw new NullReferenceException("No default index set on connection!");
16+
return this._defaultIndex;
17+
}
18+
private set { this._defaultIndex = value; }
19+
}
20+
public Uri Uri { get; private set; }
21+
public string Host { get; private set; }
22+
public int Port { get; private set; }
23+
public int Timeout { get; private set; }
24+
public string ProxyUsername { get; private set; }
25+
public string ProxyPassword { get; private set; }
26+
public string ProxyAddress { get; private set; }
27+
28+
public int MaximumAsyncConnections { get; private set; }
29+
public bool UsesPrettyResponses { get; private set; }
3030
public bool TraceEnabled { get; private set; }
3131
public bool DontDoubleEscapePathDotsAndSlashes { get; private set; }
3232

33-
public Func<Type, string> DefaultTypeNameInferrer { get; private set; }
34-
public Action<ConnectionStatus> ConnectionStatusHandler { get; private set; }
35-
public FluentDictionary<Type, string> DefaultIndices { get; private set; }
36-
public FluentDictionary<Type, string> DefaultTypeNames { get; private set; }
37-
public NameValueCollection QueryStringParameters { get; private set; }
33+
public Func<Type, string> DefaultTypeNameInferrer { get; private set; }
34+
public Action<ConnectionStatus> ConnectionStatusHandler { get; private set; }
35+
public FluentDictionary<Type, string> DefaultIndices { get; private set; }
36+
public FluentDictionary<Type, string> DefaultTypeNames { get; private set; }
37+
public NameValueCollection QueryStringParameters { get; private set; }
3838

39-
public ConnectionSettings(Uri uri)
40-
{
41-
uri.ThrowIfNull("uri");
39+
public ConnectionSettings(Uri uri)
40+
{
41+
uri.ThrowIfNull("uri");
4242

43-
this.Timeout = 60 * 1000;
43+
this.Timeout = 60 * 1000;
4444

45-
this.Uri = uri;
46-
if (!uri.ToString().EndsWith("/"))
47-
this.Uri = new Uri(uri.ToString() + "/");
48-
this.Host = uri.Host;
49-
this.Port = uri.Port;
45+
this.Uri = uri;
46+
if (!uri.ToString().EndsWith("/"))
47+
this.Uri = new Uri(uri.ToString() + "/");
48+
this.Host = uri.Host;
49+
this.Port = uri.Port;
5050

51-
this.MaximumAsyncConnections = 20;
52-
this.DefaultTypeNameInferrer = this.LowerCaseAndPluralizeTypeNameInferrer;
53-
this.DefaultIndices = new FluentDictionary<Type, string>();
54-
this.DefaultTypeNames = new FluentDictionary<Type, string>();
55-
this.ConnectionStatusHandler = this.ConnectionStatusDefaultHandler;
56-
}
51+
this.MaximumAsyncConnections = 20;
52+
this.DefaultTypeNameInferrer = this.LowerCaseAndPluralizeTypeNameInferrer;
53+
this.DefaultIndices = new FluentDictionary<Type, string>();
54+
this.DefaultTypeNames = new FluentDictionary<Type, string>();
55+
this.ConnectionStatusHandler = this.ConnectionStatusDefaultHandler;
56+
}
5757

5858
/// <summary>
5959
/// Enable Trace signals to the IConnection that it should put debug information on the Trace.
@@ -72,6 +72,10 @@ public ConnectionSettings EnableTrace(bool enabled = true)
7272
/// <returns></returns>
7373
public ConnectionSettings SetGlobalQueryStringParameters(NameValueCollection queryStringParameters)
7474
{
75+
if (this.QueryStringParameters != null)
76+
{
77+
this.QueryStringParameters.Add(queryStringParameters);
78+
}
7579
this.QueryStringParameters = queryStringParameters;
7680
return this;
7781
}
@@ -86,53 +90,54 @@ public ConnectionSettings SetTimeout(int timeout)
8690
this.Timeout = timeout;
8791
return this;
8892
}
89-
/// <summary>
90-
/// Index to default to when no index is specified.
91-
/// </summary>
92-
/// <param name="defaultIndex">When null/empty/not set might throw NRE later on
93-
/// when not specifying index explicitly while indexing.
94-
/// </param>
95-
/// <returns></returns>
96-
public ConnectionSettings SetDefaultIndex(string defaultIndex)
97-
{
98-
this.DefaultIndex = defaultIndex;
99-
return this;
100-
}
101-
/// <summary>
102-
/// Semaphore asynchronous connections automatically by giving
103-
/// it a maximum concurrent connections. Great to prevent
104-
/// out of memory exceptions
105-
/// </summary>
106-
/// <param name="maximum">defaults to 20</param>
107-
/// <returns></returns>
108-
public ConnectionSettings SetMaximumAsyncConnections(int maximum)
109-
{
110-
this.MaximumAsyncConnections = maximum;
111-
return this;
112-
}
113-
114-
/// <summary>
115-
/// If your connection has to go through proxy use this method to specify the proxy url
116-
/// </summary>
117-
/// <returns></returns>
118-
public ConnectionSettings SetProxy(Uri proxyAdress, string username, string password)
119-
{
120-
proxyAdress.ThrowIfNull("proxyAdress");
121-
this.ProxyAddress = proxyAdress.ToString();
122-
this.ProxyUsername = username;
123-
this.ProxyPassword = password;
124-
return this;
125-
}
126-
127-
/// <summary>
128-
/// Append ?pretty=true to requests, this helps to debug send and received json.
129-
/// </summary>
130-
/// <returns></returns>
131-
public ConnectionSettings UsePrettyResponses(bool b = true)
132-
{
133-
this.UsesPrettyResponses = b;
134-
return this;
135-
}
93+
/// <summary>
94+
/// Index to default to when no index is specified.
95+
/// </summary>
96+
/// <param name="defaultIndex">When null/empty/not set might throw NRE later on
97+
/// when not specifying index explicitly while indexing.
98+
/// </param>
99+
/// <returns></returns>
100+
public ConnectionSettings SetDefaultIndex(string defaultIndex)
101+
{
102+
this.DefaultIndex = defaultIndex;
103+
return this;
104+
}
105+
/// <summary>
106+
/// Semaphore asynchronous connections automatically by giving
107+
/// it a maximum concurrent connections. Great to prevent
108+
/// out of memory exceptions
109+
/// </summary>
110+
/// <param name="maximum">defaults to 20</param>
111+
/// <returns></returns>
112+
public ConnectionSettings SetMaximumAsyncConnections(int maximum)
113+
{
114+
this.MaximumAsyncConnections = maximum;
115+
return this;
116+
}
117+
118+
/// <summary>
119+
/// If your connection has to go through proxy use this method to specify the proxy url
120+
/// </summary>
121+
/// <returns></returns>
122+
public ConnectionSettings SetProxy(Uri proxyAdress, string username, string password)
123+
{
124+
proxyAdress.ThrowIfNull("proxyAdress");
125+
this.ProxyAddress = proxyAdress.ToString();
126+
this.ProxyUsername = username;
127+
this.ProxyPassword = password;
128+
return this;
129+
}
130+
131+
/// <summary>
132+
/// Append ?pretty=true to requests, this helps to debug send and received json.
133+
/// </summary>
134+
/// <returns></returns>
135+
public ConnectionSettings UsePrettyResponses(bool b = true)
136+
{
137+
this.UsesPrettyResponses = b;
138+
this.SetGlobalQueryStringParameters(new NameValueCollection { { "pretty", b.ToString().ToLowerInvariant() } });
139+
return this;
140+
}
136141

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

147-
private string LowerCaseAndPluralizeTypeNameInferrer(Type type)
148-
{
149-
type.ThrowIfNull("type");
150-
return Inflector.MakePlural(type.Name).ToLower();
151-
}
152-
153-
private void ConnectionStatusDefaultHandler(ConnectionStatus status)
154-
{
155-
return;
156-
}
157-
158-
public ConnectionSettings SetDefaultTypeNameInferrer(Func<Type, string> defaultTypeNameInferrer)
159-
{
160-
defaultTypeNameInferrer.ThrowIfNull("defaultTypeNameInferrer");
161-
this.DefaultTypeNameInferrer = defaultTypeNameInferrer;
162-
return this;
163-
}
164-
165-
public ConnectionSettings SetConnectionStatusHandler(Action<ConnectionStatus> handler)
166-
{
167-
handler.ThrowIfNull("handler");
168-
this.ConnectionStatusHandler = handler;
169-
return this;
170-
}
171-
172-
public ConnectionSettings MapDefaultTypeIndices(Action<FluentDictionary<Type, string>> mappingSelector)
173-
{
174-
mappingSelector.ThrowIfNull("mappingSelector");
175-
mappingSelector(this.DefaultIndices);
176-
return this;
177-
}
178-
public ConnectionSettings MapDefaultTypeNames(Action<FluentDictionary<Type, string>> mappingSelector)
179-
{
180-
mappingSelector.ThrowIfNull("mappingSelector");
181-
mappingSelector(this.DefaultTypeNames);
182-
return this;
183-
}
184-
185-
}
152+
private string LowerCaseAndPluralizeTypeNameInferrer(Type type)
153+
{
154+
type.ThrowIfNull("type");
155+
return Inflector.MakePlural(type.Name).ToLower();
156+
}
157+
158+
private void ConnectionStatusDefaultHandler(ConnectionStatus status)
159+
{
160+
return;
161+
}
162+
163+
public ConnectionSettings SetDefaultTypeNameInferrer(Func<Type, string> defaultTypeNameInferrer)
164+
{
165+
defaultTypeNameInferrer.ThrowIfNull("defaultTypeNameInferrer");
166+
this.DefaultTypeNameInferrer = defaultTypeNameInferrer;
167+
return this;
168+
}
169+
170+
public ConnectionSettings SetConnectionStatusHandler(Action<ConnectionStatus> handler)
171+
{
172+
handler.ThrowIfNull("handler");
173+
this.ConnectionStatusHandler = handler;
174+
return this;
175+
}
176+
177+
public ConnectionSettings MapDefaultTypeIndices(Action<FluentDictionary<Type, string>> mappingSelector)
178+
{
179+
mappingSelector.ThrowIfNull("mappingSelector");
180+
mappingSelector(this.DefaultIndices);
181+
return this;
182+
}
183+
public ConnectionSettings MapDefaultTypeNames(Action<FluentDictionary<Type, string>> mappingSelector)
184+
{
185+
mappingSelector.ThrowIfNull("mappingSelector");
186+
mappingSelector(this.DefaultTypeNames);
187+
return this;
188+
}
189+
190+
}
186191
}

0 commit comments

Comments
 (0)