Exemplo n.º 1
0
        public async Task JsonParserError403()
        {
            var settings        = CreateServiceAgentSettings();
            var serviceProvider = CreateServiceProvider(settings);
            var agent           = new TestAgent(serviceProvider, Options.Create(settings));

            agent.HttpClient = CreateClient();
            var message = new HttpResponseMessage();

            message.Content    = new StringContent("");
            message.StatusCode = System.Net.HttpStatusCode.Forbidden;

            var result = await Assert.ThrowsAsync <ForbiddenException>(async() => await agent.ParseJsonWithError(message));

            Assert.NotNull(result);
        }
Exemplo n.º 2
0
        public async Task JsonParserError400()
        {
            var settings        = CreateServiceAgentSettings();
            var serviceProvider = CreateServiceProvider(settings);
            var agent           = new TestAgent(serviceProvider, Options.Create(settings));

            agent.HttpClient = CreateClient();
            var message = new HttpResponseMessage();

            message.StatusCode = System.Net.HttpStatusCode.BadRequest;
            message.Content    = new StringContent(@"
            {""identifier"": ""dbcd3004-3af0-4862-bad1-2c4013dec85f"",
                ""code"": ""test123"",
               ""title"": ""Client validation failed."",
               ""status"": 401,
               ""extraParameters"": {
                                ""naam"": [""Naam moet uniek zijn"",""Test""],
                                    }
                        }");

            var result = await Assert.ThrowsAsync <ValidationException>(async() => await agent.ParseJsonWithError(message));

            Assert.Equal("Client validation failed.", result.Message);
            Assert.Equal("test123", result.Code);

            Assert.True(result.Messages.Count() == 1);
            var extraParam = result.Messages.FirstOrDefault();

            Assert.NotNull(extraParam);
            Assert.Equal("naam", extraParam.Key);
            Assert.True(extraParam.Value.Count() == 2);
            var errorMessage = extraParam.Value.FirstOrDefault();

            Assert.NotNull(errorMessage);
            Assert.Equal("Naam moet uniek zijn", errorMessage);
            errorMessage = extraParam.Value.LastOrDefault();
            Assert.NotNull(errorMessage);
            Assert.Equal("Test", errorMessage);
        }
        public async Task JsonParserErrorOtherStatus()
        {
            var client          = CreateClient();
            var settings        = CreateServiceAgentSettings();
            var serviceProvider = CreateServiceProvider(settings);

            var agent = new TestAgent(client, serviceProvider, Options.Create(settings));

            var message = new HttpResponseMessage();

            message.Content    = new StringContent(@"<HTML><h1>STATUS 500</h1></HTML>");
            message.StatusCode = HttpStatusCode.InternalServerError;

            var result = await Assert.ThrowsAsync <ServiceAgentException>(async() => await agent.ParseJsonWithError(message));

            Assert.NotNull(result);
            Assert.Equal(await message.Content.ReadAsStringAsync(), result.Messages.FirstOrDefault().Value.FirstOrDefault());
            Assert.Equal(result.Code, $"Status: {HttpStatusCode.InternalServerError.ToString()}");
        }
Exemplo n.º 4
0
        public async Task JsonParserErrorWithMissingTitleAndStatusCode()
        {
            var settings        = CreateServiceAgentSettings();
            var serviceProvider = CreateServiceProvider(settings);
            var agent           = new TestAgent(serviceProvider, Options.Create(settings));

            agent.HttpClient = CreateClient();
            var message = new HttpResponseMessage();
            var body    = JsonConvert.SerializeObject(new
            {
                identifier      = "dbcd3004-3af0-4862-bad1-2c4013dec85f",
                extraParameters = (string)null
            });

            message.Content = new StringContent(body);

            var result = await Assert.ThrowsAsync <ServiceAgentException>(async() => await agent.ParseJsonWithError(message));

            Assert.True(result.Messages.Count() == 1);
            var extraParam = result.Messages.FirstOrDefault();

            Assert.NotNull(extraParam);
            Assert.Equal("json", extraParam.Key);
            Assert.True(extraParam.Value.Count() == 1);
            var errorMessage = extraParam.Value.FirstOrDefault();

            Assert.NotNull(errorMessage);
            Assert.Equal(body, errorMessage);
        }
        public async Task JsonParserErrorWith2Param()
        {
            var client          = CreateClient();
            var settings        = CreateServiceAgentSettings();
            var serviceProvider = CreateServiceProvider(settings);

            var agent = new TestAgent(client, serviceProvider, Options.Create(settings));

            var message = new HttpResponseMessage();

            message.Content = new StringContent(@"
            {""identifier"": ""dbcd3004-3af0-4862-bad1-2c4013dec85f"",
               ""title"": ""Client validation failed."",
               ""status"": 400,
               ""extraParameters"": {
                                ""naam"": [""Naam moet uniek zijn"",""Test""],
                                ""test"": [""Naam moet uniek zijn 2"",""Test2""]
                                    }
                        }");

            var result = await Assert.ThrowsAsync <ServiceAgentException>(async() => await agent.ParseJsonWithError(message));

            Assert.True(result.Messages.Count() == 2);

            var(key, value) = result.Messages.FirstOrDefault();
            Assert.NotNull(key);
            Assert.Equal("naam", key);
            Assert.True(value.Count() == 2);

            var errorMessage = value.FirstOrDefault();

            Assert.NotNull(errorMessage);
            Assert.Equal("Naam moet uniek zijn", errorMessage);

            (key, value) = result.Messages.LastOrDefault();
            Assert.NotNull(key);
            Assert.Equal("test", key);
            Assert.True(value.Count() == 2);

            errorMessage = value.LastOrDefault();
            Assert.NotNull(errorMessage);
            Assert.Equal("Test2", errorMessage);
        }