Exemplo n.º 1
0
        public async Task Speech_ParseNOk()
        {
            var fixture = new Fixture();

            fixture.Behaviors
            .OfType <ThrowingRecursionBehavior>()
            .ToList()
            .ForEach(b => fixture.Behaviors.Remove(b));
            fixture.Behaviors.Add(new OmitOnRecursionBehavior());

            using (var test = new Flurl.Http.Testing.HttpTest())
            {
                test.RespondWith(@"{""error"":""could not do something""}", 502);

                var loggerMock = new Mock <ILogger <WitAi.WitAiClient> >(MockBehavior.Loose);
                var config     = new Core.Configuration.WitAiConfiguration
                {
                    ApiKey     = "someapikey",
                    ApiVersion = "someapiversion",
                    BaseUrl    = "http://someurl.fqdn.local"
                };
                var options = new Mock <IOptionsSnapshot <Core.Configuration.WitAiConfiguration> >(MockBehavior.Strict);
                options.Setup(o => o.Value)
                .Returns(() => config);

                var sut = new WitAi.WitAiClient(loggerMock.Object, options.Object);

                var task = sut.ParseSpeech(new byte[] { 0x30, 0x60, 0x1f, 0x4f });
                await Assert.ThrowsAsync <Flurl.Http.FlurlHttpException>(() => task);
            }
        }
Exemplo n.º 2
0
        public async Task Test_ParseOk()
        {
            var fixture = new Fixture();

            fixture.Behaviors
            .OfType <ThrowingRecursionBehavior>()
            .ToList()
            .ForEach(b => fixture.Behaviors.Remove(b));
            fixture.Behaviors.Add(new OmitOnRecursionBehavior());

            using (var test = new Flurl.Http.Testing.HttpTest())
            {
                test.RespondWithJson(new ParseWitMessageResponse
                {
                    Text         = "text",
                    IsSuccessful = true,
                    MessageId    = System.Guid.NewGuid().ToString(),
                    Entities     = new Dictionary <string, List <WitParsedEntity> >
                    {
                        { "entity1", new List <WitParsedEntity>(fixture.CreateMany <WitParsedEntity>(5)) },
                        { "entity2", new List <WitParsedEntity>(fixture.CreateMany <WitParsedEntity>(5)) },
                        { "entity3", new List <WitParsedEntity>(fixture.CreateMany <WitParsedEntity>(5)) }
                    }
                });

                var loggerMock = new Mock <ILogger <WitAi.WitAiClient> >(MockBehavior.Loose);
                var config     = new Core.Configuration.WitAiConfiguration
                {
                    ApiKey     = "someapikey",
                    ApiVersion = "someapiversion",
                    BaseUrl    = "http://someurl.fqdn.local"
                };
                var options = new Mock <IOptionsSnapshot <Core.Configuration.WitAiConfiguration> >(MockBehavior.Strict);
                options.Setup(o => o.Value)
                .Returns(() => config);

                var sut = new WitAi.WitAiClient(loggerMock.Object, options.Object);

                var result = await sut.Parse("test string");

                Assert.NotNull(result);
                Assert.Null(result.ErrorMessage);
                Assert.NotNull(result.Entities);
                Assert.NotEmpty(result.Entities);

                foreach (var entity in result.Entities)
                {
                    Assert.StartsWith("entity", entity.Name);
                    Assert.Equal(5, entity.Values.Count);
                }
            }
        }
Exemplo n.º 3
0
        public async Task Should_Return_CrimesFromRaleighOpenData()
        {
            using (var httpTest = new Flurl.Http.Testing.HttpTest())
            {
                // Arrange
                httpTest.RespondWith("Some Error", status: 500);

                // Act
                var response = await _client.GetAsync("/api/raleigh/crime");

                var responseString = await response.Content.ReadAsStringAsync();

                // Assert
                Assert.IsTrue(responseString.Contains("Some Error"));
                Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode);
                httpTest.ShouldHaveCalled("https://data.raleighnc.gov/resource/3bhm-we7a.json");
            }
        }
Exemplo n.º 4
0
        public async Task Should_Return_CrimesFromRaleighOpenData()
        {
            using (var httpTest = new Flurl.Http.Testing.HttpTest())
            {
                // Arrange
                httpTest.RespondWith(_json);

                // Act
                var response = await _client.GetAsync("/api/raleigh/crime");

                response.EnsureSuccessStatusCode();
                var responseString = await response.Content.ReadAsStringAsync();

                // Assert
                Assert.IsTrue(responseString.Contains("Traffic/DWI (Driving While Impaired)"));
                httpTest.ShouldHaveCalled("https://data.raleighnc.gov/resource/3bhm-we7a.json");
            }
        }
Exemplo n.º 5
0
        public async Task Should_Return_CrimesFromRaleighOpenData_When_Filtered()
        {
            using (var httpTest = new Flurl.Http.Testing.HttpTest())
            {
                // Arrange
                httpTest.RespondWith(_json);

                // Act
                var response = await _client.GetAsync("/api/raleigh/crime?query=Drug");

                response.EnsureSuccessStatusCode();
                var responseString = await response.Content.ReadAsStringAsync();

                // Assert
                Assert.IsFalse(responseString.Contains("Traffic/DWI (Driving While Impaired)"), "Filtered list should not have Traffic/DWI");
                Assert.IsTrue(responseString.Contains("Drug Violation/Misdemeanor"), "Fitered list should only have Drug crimes");
                httpTest.ShouldHaveCalled("https://data.raleighnc.gov/resource/3bhm-we7a.json");
            }
        }
Exemplo n.º 6
0
        public async Task Get_Should_Error_When_ApiTimeouts()
        {
            using (var httpTest = new Flurl.Http.Testing.HttpTest())
            {
                // Arrange
                httpTest.SimulateTimeout();
                dataSourceMock.Setup(ma => ma.GetAll())
                .ReturnsAsync(() => _dataSources);

                // Act
                var response = await _client.GetAsync("/api/allData");

                response.EnsureSuccessStatusCode();
                var responseString = await response.Content.ReadAsStringAsync();

                // Assert
                dataSourceMock.VerifyAll();
                Assert.IsTrue(responseString.Contains("GET http://mock-server/api/data timed out."), $"Response string {responseString} should contain the url of mock server");
            }
        }
Exemplo n.º 7
0
        public async Task Get_Should_ReturnAllData_When_ApiReturnsOK()
        {
            using (var httpTest = new Flurl.Http.Testing.HttpTest())
            {
                // Arrange
                httpTest.RespondWithJson(new { msg = "hello from the mock" }, 200);
                dataSourceMock.Setup(ma => ma.GetAll())
                .ReturnsAsync(() => _dataSources);

                // Act
                var response = await _client.GetAsync("/api/allData");

                response.EnsureSuccessStatusCode();
                var responseString = await response.Content.ReadAsStringAsync();

                // Assert
                dataSourceMock.VerifyAll();
                Assert.IsTrue(responseString.Contains("hello from the mock"), $"Response string {responseString} should contain the url of mock server");
            }
        }