public static async void NonSuccessfulHttpRequestThrows()
        {
            var client = Substitute.For<IElasticsearchClient>();

            client.SearchAsync<string>(
                    "_all",
                    "docType",
                    @"{""timeout"":""10s""}",
                    Arg.Any<Func<SearchRequestParameters, SearchRequestParameters>>())
                .Returns(Task.FromResult(ElasticsearchResponse<string>.Create(
                    new ConnectionConfiguration(),
                    404,
                    "_search",
                    "_all",
                    new byte[0])));

            var localConnection = new ElasticNetConnection(client);
            var request = new SearchRequest { DocumentType = "docType" };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                formatter.Body,
                request,
                CancellationToken.None,
                log));

            Assert.IsType<HttpRequestException>(ex);
            Assert.Equal("Response status code does not indicate success: 404 (Not Found).", ex.Message);
        }
Ejemplo n.º 2
0
        public static async void NonSuccessfulHttpRequestThrows()
        {
            var client = Substitute.For <IElasticsearchClient>();

            client.SearchAsync <string>(
                "_all",
                "docType",
                @"{""timeout"":""10s""}",
                Arg.Any <Func <SearchRequestParameters, SearchRequestParameters> >())
            .Returns(Task.FromResult(ElasticsearchResponse <string> .Create(
                                         new ConnectionConfiguration(),
                                         404,
                                         "_search",
                                         "_all",
                                         new byte[0])));

            var localConnection = new ElasticNetConnection(client);
            var request         = new SearchRequest {
                DocumentType = "docType"
            };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                                                     formatter.Body,
                                                     request,
                                                     CancellationToken.None,
                                                     log));

            Assert.IsType <HttpRequestException>(ex);
            Assert.Equal("Response status code does not indicate success: 404 (Not Found).", ex.Message);
        }
Ejemplo n.º 3
0
        public static async void SearchAsyncThrowsTaskCancelledExceptionWithAlreadyCancelledCancellationToken()
        {
            var client = Substitute.For <IElasticsearchClient>();

            var spyLog          = new SpyLog();
            var localConnection = new ElasticNetConnection(client, "SearchIndex");
            var request         = new SearchRequest {
                DocumentType = "docType"
            };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                                                     formatter.Body,
                                                     request,
                                                     new CancellationToken(true),
                                                     spyLog));

            Assert.IsType <TaskCanceledException>(ex);
        }
Ejemplo n.º 4
0
        public static async Task LogsDebugMessagesDuringExecution()
        {
            var client = Substitute.For <IElasticsearchClient>();

            var responseString = BuildResponseString(2, 1, 1, 0.3141, "testIndex", "testType", "testId");
            var spyLog         = new SpyLog();

            client.SearchAsync <string>(
                "SearchIndex",
                "abc123",
                @"{""size"":2112,""timeout"":""10s""}",
                Arg.Any <Func <SearchRequestParameters, SearchRequestParameters> >())
            .Returns(Task.FromResult(ElasticsearchResponse <string> .Create(
                                         new ConnectionConfiguration(),
                                         200,
                                         "_search",
                                         "http://localhost/SearchIndex/abc123/_search",
                                         new byte[0],
                                         responseString)));

            var localConnection = new ElasticNetConnection(client, index: "SearchIndex");
            var request         = new SearchRequest {
                DocumentType = "abc123", Size = 2112
            };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            await localConnection.SearchAsync(
                formatter.Body,
                request,
                CancellationToken.None,
                spyLog);

            Assert.Equal(4, spyLog.Entries.Count);
            Assert.Equal(@"Request: POST http://localhost/SearchIndex/abc123/_search", spyLog.Entries[0].Message);
            Assert.Equal(@"Body:" + '\n' + @"{""size"":2112,""timeout"":""10s""}", spyLog.Entries[1].Message);
            Assert.True(new Regex(@"Response: 200 OK \(in \d+ms\)").Match(spyLog.Entries[2].Message).Success);
            Assert.True(new Regex(@"Deserialized \d+ characters into 1 hits in \d+ms").Match(spyLog.Entries[3].Message).Success);
        }
        public static async Task LogsDebugMessagesDuringExecution()
        {
            var client = Substitute.For<IElasticsearchClient>();

            var responseString = BuildResponseString(2, 1, 1, 0.3141, "testIndex", "testType", "testId");
            var spyLog = new SpyLog();

            client.SearchAsync<string>(
                    "SearchIndex",
                    "abc123",
                    @"{""size"":2112,""timeout"":""10s""}",
                    Arg.Any<Func<SearchRequestParameters, SearchRequestParameters>>())
                .Returns(Task.FromResult(ElasticsearchResponse<string>.Create(
                    new ConnectionConfiguration(),
                    200,
                    "_search",
                    "http://localhost/SearchIndex/abc123/_search",
                    new byte[0],
                    responseString)));

            var localConnection = new ElasticNetConnection(client, index: "SearchIndex");
            var request = new SearchRequest { DocumentType = "abc123", Size = 2112 };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            await localConnection.SearchAsync(
                formatter.Body,
                request,
                CancellationToken.None,
                spyLog);

            Assert.Equal(4, spyLog.Entries.Count);
            Assert.Equal(@"Request: POST http://localhost/SearchIndex/abc123/_search", spyLog.Entries[0].Message);
            Assert.Equal(@"Body:" + '\n' + @"{""size"":2112,""timeout"":""10s""}", spyLog.Entries[1].Message);
            Assert.True(new Regex(@"Response: 200 OK \(in \d+ms\)").Match(spyLog.Entries[2].Message).Success);
            Assert.True(new Regex(@"Deserialized \d+ characters into 1 hits in \d+ms").Match(spyLog.Entries[3].Message).Success);
        }
        public static async void SearchAsyncThrowsTaskCancelledExceptionWithAlreadyCancelledCancellationToken()
        {
            var client = Substitute.For<IElasticsearchClient>();

            var spyLog = new SpyLog();
            var localConnection = new ElasticNetConnection(client, "SearchIndex");
            var request = new SearchRequest { DocumentType = "docType" };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                formatter.Body,
                request,
                new CancellationToken(true),
                spyLog));

            Assert.IsType<TaskCanceledException>(ex);
        }