Example #1
0
        public void Should_not_deserialize_with_json_property_if_not_appropriate_type(DeviceItem obj)
        {
            var json   = JsonForCastle.SerializeObject(obj);
            var result = JsonForCastle.DeserializeObject <DeviceItem>(json);

            result.Should().NotBeNull();
        }
Example #2
0
 public static StringContent ToHttpContent(this object obj)
 {
     return(new StringContent(
                JsonForCastle.SerializeObject(obj),
                Encoding.UTF8,
                "application/json"));
 }
Example #3
0
        public void Should_deserialize_with_json_property_if_appropriate_type(IHasJson obj)
        {
            var json   = JsonForCastle.SerializeObject(obj);
            var result = JsonForCastle.DeserializeObject <Device>(json);

            result.Internal.Should().NotBeNull();
        }
Example #4
0
        public void Should_deserialize_from_snakecase()
        {
            const string json = "{\"user_id\":\"123\"}";

            var result = JsonForCastle.DeserializeObject <GenericObject>(json);

            result.UserId.Should().Be("123");
        }
Example #5
0
        public void Should_serialize_client_id_to_false_if_empty(string value, string expected)
        {
            var obj = new RequestContext()
            {
                ClientId = value
            };

            var result = JsonForCastle.SerializeObject(obj);

            result.Should().Contain(expected);
        }
Example #6
0
        public void Should_serialize_header_to_truebool_if_truestring(
            string name,
            string value,
            string expected)
        {
            var obj = new RequestContext()
            {
                Headers = new Dictionary <string, string>()
                {
                    [name] = value
                }
            };

            var result = JsonForCastle.SerializeObject(obj);

            result.Should().Contain(expected);
        }
        private async Task <TResponse> SendRequest <TResponse>(HttpMethod method, string endpoint, object payload = null)
            where TResponse : class, new()
        {
            var jsonContent = payload != null?payload.ToHttpContent() : null;

            var requestMessage = new HttpRequestMessage(method, endpoint)
            {
                Content = jsonContent
            };

            _logger.Info(() => string.Concat(
                             "Request",
                             Environment.NewLine,
                             requestMessage,
                             Environment.NewLine,
                             payload != null ? JsonForCastle.SerializeObject(payload) : ""
                             ));

            try
            {
                var response = await _httpClient.SendAsync(requestMessage);

                var content = response.Content != null ? await response.Content.ReadAsStringAsync() : "";

                _logger.Info(() => string.Concat(
                                 "Response",
                                 Environment.NewLine,
                                 response,
                                 Environment.NewLine,
                                 content
                                 ));

                if (response.IsSuccessStatusCode)
                {
                    return(JsonForCastle.DeserializeObject <TResponse>(content));
                }

                throw await response.ToCastleException(requestMessage.RequestUri.AbsoluteUri);
            }
            catch (OperationCanceledException)
            {
                throw new CastleTimeoutException(
                          requestMessage.RequestUri.AbsoluteUri,
                          (int)_httpClient.Timeout.TotalMilliseconds);
            }
        }
Example #8
0
        public JObject BuildTrackRequest(ActionRequest request)
        {
            var prepared = (request ?? new ActionRequest()).PrepareApiCopy(_configuration.AllowList, _configuration.DenyList);

            return(JsonForCastle.FromObject(prepared));
        }
Example #9
0
        public JObject BuildAuthenticateRequest(ActionRequest request)
        {
            var prepared = (request ?? new ActionRequest()).PrepareApiCopy(_configuration.Whitelist, _configuration.Blacklist);

            return(JsonForCastle.FromObject(prepared));
        }
Example #10
0
        public void Should_only_allow_serialization_for_scrub()
        {
            Action act = () => JsonForCastle.DeserializeObject <NotValidScrubbing>("{ \"generic_property\":\"test\" }");

            act.Should().Throw <NotSupportedException>();
        }
Example #11
0
        public void Should_deserialize_to_default_if_error()
        {
            var result = JsonForCastle.DeserializeObject <VoidResponse>("");

            result.Should().NotBeNull();
        }