|
14 | 14 |
|
15 | 15 | namespace Tests.ClientConcepts.Connection |
16 | 16 | { |
17 | | - /**[[modifying-default-connection]] |
18 | | - * === Modifying the default connection |
19 | | - * |
20 | | - * The client abstracts sending the request and creating a response behind `IConnection` and the default |
21 | | - * implementation uses |
22 | | - * |
23 | | - * - https://msdn.microsoft.com/en-us/library/system.net.webrequest(v=vs.110).aspx[`System.Net.WebRequest`] for Desktop CLR |
24 | | - * - https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx[`System.Net.Http.HttpClient`] for Core CLR |
25 | | - * |
26 | | - * The reason for different implementations is that `WebRequest` and `ServicePoint` are not directly available |
27 | | - * on netstandard 1.3. |
28 | | - * |
29 | | - * The Desktop CLR implementation using `WebRequest` is the most mature implementation, having been tried and trusted |
30 | | - * in production since the beginning of NEST. For this reason, we aren't quite ready to it give up in favour of |
31 | | - * a `HttpClient` implementation across all CLR versions. |
32 | | - * |
33 | | - * In addition to production usage, there are also a couple of important toggles that are easy to set against a |
34 | | - * `ServicePoint` that are not possible to set as yet on `HttpClient`. |
35 | | - * |
36 | | - * Finally, another limitation is that `HttpClient` has no synchronous code paths, so supporting these means |
37 | | - * doing hacky async patches which definitely need time to bake. |
38 | | - * |
39 | | - * So why would you ever want to pass your own `IConnection`? Let's look at a couple of examples |
40 | | - * |
41 | | - */ |
42 | | - public class ModifyingTheDefaultConnection |
43 | | - { |
44 | | - /**==== Using InMemoryConnection |
45 | | - * |
46 | | - * `InMemoryConnection` is an in-built `IConnection` that makes it easy to write unit tests against. It can be |
47 | | - * configured to respond with default response bytes, HTTP status code and an exception when a call is made. |
48 | | - * |
49 | | - * `InMemoryConnection` **doesn't actually send any requests or receive any responses from Elasticsearch**; |
50 | | - * requests are still serialized and the request bytes can be obtained on the response if `.DisableDirectStreaming` is |
51 | | - * set to `true` on the request or globally |
52 | | - */ |
53 | | - public void InMemoryConnectionDefaultCtor() |
54 | | - { |
55 | | - var connection = new InMemoryConnection(); |
56 | | - var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); |
57 | | - var settings = new ConnectionSettings(connectionPool, connection); |
58 | | - var client = new ElasticClient(settings); |
59 | | - } |
| 17 | + /**[[modifying-default-connection]] |
| 18 | + * === Modifying the default connection |
| 19 | + * |
| 20 | + * The client abstracts sending the request and creating a response behind `IConnection` and the default |
| 21 | + * implementation uses |
| 22 | + * |
| 23 | + * - https://msdn.microsoft.com/en-us/library/system.net.webrequest(v=vs.110).aspx[`System.Net.WebRequest`] for Desktop CLR |
| 24 | + * - https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx[`System.Net.Http.HttpClient`] for Core CLR |
| 25 | + * |
| 26 | + * The reason for different implementations is that `WebRequest` and `ServicePoint` are not directly available |
| 27 | + * on netstandard 1.3. |
| 28 | + * |
| 29 | + * The Desktop CLR implementation using `WebRequest` is the most mature implementation, having been tried and trusted |
| 30 | + * in production since the beginning of NEST. For this reason, we aren't quite ready to it give up in favour of |
| 31 | + * a `HttpClient` implementation across all CLR versions. |
| 32 | + * |
| 33 | + * In addition to production usage, there are also a couple of important toggles that are easy to set against a |
| 34 | + * `ServicePoint` that are not possible to set as yet on `HttpClient`. |
| 35 | + * |
| 36 | + * Finally, another limitation is that `HttpClient` has no synchronous code paths, so supporting these means |
| 37 | + * doing hacky async patches which definitely need time to bake. |
| 38 | + * |
| 39 | + * So why would you ever want to pass your own `IConnection`? Let's look at a couple of examples |
| 40 | + * |
| 41 | + */ |
| 42 | + public class ModifyingTheDefaultConnection |
| 43 | + { |
| 44 | + /**==== Using InMemoryConnection |
| 45 | + * |
| 46 | + * `InMemoryConnection` is an in-built `IConnection` that makes it easy to write unit tests against. It can be |
| 47 | + * configured to respond with default response bytes, HTTP status code and an exception when a call is made. |
| 48 | + * |
| 49 | + * `InMemoryConnection` **doesn't actually send any requests or receive any responses from Elasticsearch**; |
| 50 | + * requests are still serialized and the request bytes can be obtained on the response if `.DisableDirectStreaming` is |
| 51 | + * set to `true` on the request or globally |
| 52 | + */ |
| 53 | + public void InMemoryConnectionDefaultCtor() |
| 54 | + { |
| 55 | + var connection = new InMemoryConnection(); |
| 56 | + var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); |
| 57 | + var settings = new ConnectionSettings(connectionPool, connection); |
| 58 | + var client = new ElasticClient(settings); |
| 59 | + } |
60 | 60 |
|
61 | | - /** |
| 61 | + /** |
62 | 62 | * Here we create a new `ConnectionSettings` by using the overload that takes a `IConnectionPool` and an `IConnection`. |
63 | 63 | * We pass it an `InMemoryConnection` which, using the default parameterless constructor, |
64 | | - * will return 200 for everything and never actually perform any IO. |
65 | | - * |
66 | | - * Let's see a more complex example |
| 64 | + * will return 200 for everything and never actually perform any IO. |
| 65 | + * |
| 66 | + * Let's see a more complex example |
67 | 67 | */ |
68 | | - [U] |
69 | | - public void InMemoryConnectionOverloadedCtor() |
70 | | - { |
71 | | - var response = new |
72 | | - { |
73 | | - took = 1, |
74 | | - timed_out = false, |
75 | | - _shards = new |
76 | | - { |
77 | | - total = 2, |
78 | | - successful = 2, |
79 | | - failed = 0 |
80 | | - }, |
81 | | - hits = new |
82 | | - { |
83 | | - total = 25, |
84 | | - max_score = 1.0, |
85 | | - hits = Enumerable.Range(1, 25).Select(i => (object)new |
86 | | - { |
87 | | - _index = "project", |
88 | | - _type = "project", |
89 | | - _id = $"Project {i}", |
90 | | - _score = 1.0, |
91 | | - _source = new { name = $"Project {i}" } |
92 | | - }).ToArray() |
93 | | - } |
94 | | - }; |
| 68 | + [U] |
| 69 | + public void InMemoryConnectionOverloadedCtor() |
| 70 | + { |
| 71 | + var response = new |
| 72 | + { |
| 73 | + took = 1, |
| 74 | + timed_out = false, |
| 75 | + _shards = new |
| 76 | + { |
| 77 | + total = 2, |
| 78 | + successful = 2, |
| 79 | + failed = 0 |
| 80 | + }, |
| 81 | + hits = new |
| 82 | + { |
| 83 | + total = 25, |
| 84 | + max_score = 1.0, |
| 85 | + hits = Enumerable.Range(1, 25).Select(i => (object)new |
| 86 | + { |
| 87 | + _index = "project", |
| 88 | + _type = "project", |
| 89 | + _id = $"Project {i}", |
| 90 | + _score = 1.0, |
| 91 | + _source = new { name = $"Project {i}" } |
| 92 | + }).ToArray() |
| 93 | + } |
| 94 | + }; |
95 | 95 |
|
96 | | - var responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)); |
97 | | - var connection = new InMemoryConnection(responseBytes, 200); // <1> `InMemoryConnection` is configured to **always** return `responseBytes` along with a 200 HTTP status code |
98 | | - var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); |
99 | | - var settings = new ConnectionSettings(connectionPool, connection).DefaultIndex("project"); |
100 | | - var client = new ElasticClient(settings); |
| 96 | + var responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)); |
| 97 | + var connection = new InMemoryConnection(responseBytes, 200); // <1> `InMemoryConnection` is configured to **always** return `responseBytes` along with a 200 HTTP status code |
| 98 | + var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); |
| 99 | + var settings = new ConnectionSettings(connectionPool, connection).DefaultIndex("project"); |
| 100 | + var client = new ElasticClient(settings); |
101 | 101 |
|
102 | | - var searchResponse = client.Search<Project>(s => s.MatchAll()); |
| 102 | + var searchResponse = client.Search<Project>(s => s.MatchAll()); |
103 | 103 |
|
104 | | - /** |
105 | | - * We can now assert that the `searchResponse` is valid and contains documents deserialized |
106 | | - * from our fixed `InMemoryConnection` response |
107 | | - */ |
108 | | - searchResponse.ShouldBeValid(); |
109 | | - searchResponse.Documents.Count.Should().Be(25); |
110 | | - } |
111 | | - } |
| 104 | + /** |
| 105 | + * We can now assert that the `searchResponse` is valid and contains documents deserialized |
| 106 | + * from our fixed `InMemoryConnection` response |
| 107 | + */ |
| 108 | + searchResponse.ShouldBeValid(); |
| 109 | + searchResponse.Documents.Count.Should().Be(25); |
| 110 | + } |
| 111 | + } |
112 | 112 | } |
0 commit comments