private void WriteBodyAsJson(Model model)
        {
            string jsonString = SerializeModel(model, false);

            var writer = new StreamWriter(m_context.Request.Body, Encoding.UTF8);
            writer.Write(jsonString);
            writer.Flush();
        }
        public void EmptyResourceShouldCreateValidationErrors()
        {
            IServiceBehavior behavior = new ValidationBehavior();

            var resource = new Model();

            behavior.OnMethodExecuting(m_context, new MethodExecutingContext(m_service, m_method, resource));

            Assert.That(m_context.Request.ResourceState.IsValid, Is.False);
            Assert.That(m_context.Request.ResourceState.Count, Is.GreaterThan(0));
        }
        private static Model CreateModel()
        {
            var model = new Model
            {
                Id = 1,
                Name = "John Doe",
                Items = new[] { "A", "B", "C" }
            };

            return model;
        }
Example #4
0
        public IResult Post(Model model)
        {
            if (model == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest, "No model provided.");
            }

            return Result.ResponseStatus(HttpStatusCode.Created, "Created", new Dictionary<string, string>
                                                                            {
                                                                                { "Location", Context.Request.Url.OperationUrl + "/1" }
                                                                            });
        }
        private static string SerializeModel(Model model, bool jsonP)
        {
            var serializedModel = JsonConvert.SerializeObject(model, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            if (!jsonP)
            {
                return serializedModel;
            }

            return String.Format("{0}({1});", CallbackFunction, serializedModel);
        }
        public void ValidResourceShouldNotCreateValidationErrors()
        {
            IServiceBehavior behavior = new ValidationBehavior();

            var resource = new Model
            {
                Id = 1,
                Name = "Joe Bloe"
            };

            behavior.OnMethodExecuting(m_context, new MethodExecutingContext(m_service, m_method, resource));

            Assert.That(m_context.Request.ResourceState.IsValid, Is.True);
            Assert.That(m_context.Request.ResourceState.Count, Is.EqualTo(0));
        }
        private void WriteBodyAsFormsEncodedString(Model model)
        {
            var nameValueBuilder = new StringBuilder();
            nameValueBuilder.Append("ID=").Append(HttpUtility.UrlEncode(model.Id.ToString()));
            nameValueBuilder.Append("&Name=").Append(HttpUtility.UrlEncode(model.Name));

            foreach (string item in model.Items)
            {
                nameValueBuilder.Append("&Items=").Append(HttpUtility.UrlEncode(item));
            }

            var writer = new StreamWriter(m_context.Request.Body, Encoding.UTF8);
            writer.Write(nameValueBuilder.ToString());
            writer.Flush();
        }
        private static string SerializeModel(Model model)
        {
            var memoryStream = new MemoryStream();

            var xmlWriter = new XmlTextWriter(new StreamWriter(memoryStream, Encoding.UTF8))
            {
                Formatting = Formatting.None
            };

            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add(String.Empty, String.Empty);

            var serializer = new XmlSerializer(model.GetType());
            serializer.Serialize(xmlWriter, model, namespaces);

            memoryStream.Seek(0, SeekOrigin.Begin);

            using (var reader = new StreamReader(memoryStream, Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
        private void WriteBodyAsXml(Model model)
        {
            var xmlWriter = new XmlTextWriter(new StreamWriter(m_context.Request.Body, Encoding.UTF8))
            {
                Formatting = Formatting.None
            };

            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add(String.Empty, String.Empty);

            var serializer = new XmlSerializer(model.GetType());
            serializer.Serialize(xmlWriter, model, namespaces);
        }
        public void ResourceWithNameOver25CharactersShouldCreateValidationErrors()
        {
            IServiceBehavior behavior = new ValidationBehavior();

            var resource = new Model
            {
                Id = 1,
                Name = "Abcdefghijklmnopqrstuvwxyz"
            };

            behavior.OnMethodExecuting(m_context, new MethodExecutingContext(m_service, m_method, resource));

            Assert.That(m_context.Request.ResourceState.IsValid, Is.False);
            Assert.That(m_context.Request.ResourceState.Count, Is.GreaterThan(0));
        }
 private static string SerializeModel(Model model)
 {
     return JsonConvert.SerializeObject(model);
 }