public async Task When_creating_with_given_encoding()
        {
            var content = new JSONContent("Foo", Encoding.UTF32);

            content.Headers.ShouldNotBeNull();
            content.Headers.ShouldHaveSingleItem();

            content.Headers.ContentType
            .ShouldBe(MediaTypeHeaderValue.Parse("application/json; charset=utf-32"));

            var json = await content.ReadAsStringAsync();

            json.ShouldBe("Foo");
        }
        public async Task When_creating_with_default_constructor()
        {
            var content = new JSONContent("Foo");

            content.Headers.ShouldNotBeNull();
            content.Headers.ShouldHaveSingleItem();

            content.Headers.ContentType
            .ShouldBe(MediaTypeHeaderValue.Parse("application/json; charset=utf-8"));

            var json = await content.ReadAsStringAsync();

            json.ShouldBe("Foo");
        }
Exemplo n.º 3
0
        public static async Task <HttpResponseMessage> Post(string url, string Data)
        {
            if (string.IsNullOrEmpty(Data))
            {
                throw new ArgumentException(MessageCodes.ArgumentExceptionData);
            }
            var jsonContent = new JSONContent(Data, Encoding.UTF8);

            try
            {
                double TimeOut = double.Parse(configuration["AppSettings:ApiTimeOut"]);
                using (var client = new RestClient(timeout: TimeSpan.FromSeconds(TimeOut)))
                {
                    var result = await client.PostAsync(url, jsonContent);

                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            throw new NotImplementedException(MessageCodes.NotSupportedContentType);
        }
        public async Task AttemptToAuthenticateUserAsync_WhenInvokedWithWrongLoginData_ShouldReturnSuccessSetToFalse()
        {
            // Setup
            //_mockMonstersRepository.SetupGet(r => r.Monsters).Returns(new[]
            //{
            //	new Monster(),
            //	new Monster()
            //}.ToAsyncEnumerable());
            var client        = TestServer.CreateClient();
            var postLoginData = new JSONContent(new LoginData {
                Password = "******", Username = "******"
            });

            // Execute
            var response = await client.PostAsync(new Uri($"{BaseUrl}/api/auth"), postLoginData);

            // Verify
            var responseData = JsonConvert.DeserializeObject <JObject>(await response.Content.ReadAsStringAsync());

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            response.Content.Headers.ContentType.MediaType.Should().Be("application/json");
            responseData.Should().ContainKey("success");
            responseData.GetValue("success", StringComparison.Ordinal).Value <bool>().Should().BeFalse();
        }
Exemplo n.º 5
0
        public static async Task <HttpResponseMessage> Post(string url, RequestMessage req, char currentLanguage)
        {
            if (string.IsNullOrEmpty(req.Data))
            {
                throw new ArgumentException(MessageCodes.ArgumentExceptionData);
            }
            //Xu lu TH call API SSL/TLS: https://
            if (url != null && url.Length > 8 && url.Substring(0, 8) == ("https://"))
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                if (bool.Parse(configuration["AppSettings:IGNORE_CERT_HTTPS"]))
                {
                    ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
                }
            }
            if (req.ContentType == MediaTypeHeader.Json)
            {
                var jsonContent = new JSONContent(req.Data, Encoding.UTF8);
                var headers     = new Dictionary <string, IEnumerable <string> >
                {
                    { HttpRequestHeader.Accept.ToString(), new [] { req.AcceptType } }
                };
                if (req.headerKeys.Count > 0)
                {
                    foreach (var item in req.headerKeys)
                    {
                        headers.Add(item.Key, new[] { item.Value });
                    }
                }
                if (null != req.FacilityId)
                {
                    headers.Add(MediaTypeHeader.Facility, new[] { req.FacilityId.ToString() });
                }
                if (null != req.UserId)
                {
                    headers.Add(MediaTypeHeader.UserId, new[] { req.UserId.ToString() });
                }
                if (!string.IsNullOrEmpty(req.UserName))
                {
                    headers.Add(MediaTypeHeader.UserName, new[] { req.UserName.ToString() });
                }
                if (!string.IsNullOrEmpty(currentLanguage.ToString()) && currentLanguage != ' ')
                {
                    headers.Add(MediaTypeHeader.CurrentLanguage, new[] { currentLanguage.ToString() });
                }
                try
                {
                    double TimeOut = double.Parse(configuration["AppSettings:ApiTimeOut"]);
                    TimeOut = req.TimeOut > 0 ? req.TimeOut : TimeOut;
                    using (var client = new RestClient(headers, timeout: TimeSpan.FromSeconds(TimeOut)))
                    {
                        var result = await client.PostAsync(url, jsonContent);

                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            throw new NotImplementedException(MessageCodes.NotSupportedContentType);
        }