Пример #1
0
        public async void Put_Upload_Is_200_Response()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (request, umbCtx, typedContent, serviceContext, searchProvider) =>
            {
                MemberServiceMocks.SetupMocksForPost(serviceContext);
            });

            using (var server = TestServer.Create(builder => startup.Configuration(builder)))
            {
                var mfdc = new MultipartFormDataContent();
                mfdc.Add(
                    new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes("This is from a file")))
                {
                    Headers =
                    {
                        ContentType = new MediaTypeHeaderValue("image/jpg")
                    }
                },
                    name: "Data",
                    fileName: "File1.txt");

                var uri    = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}/123/upload", RouteConstants.MembersSegment));
                var result = await server.HttpClient.PutAsync(uri, mfdc);

                Console.WriteLine(result);

                var json = await((StreamContent)result.Content).ReadAsStringAsync();
                Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented));
                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            }
        }
        public async Task Post_Is_400_Validation_Property_Required()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (testServices) =>
            {
                MemberServiceMocks.SetupMocksForPost(testServices.ServiceContext);

                var mockPropertyEditor = Mock.Get(PropertyEditorResolver.Current);
                mockPropertyEditor.Setup(x => x.GetByAlias("testEditor")).Returns(new ModelMocks.SimplePropertyEditor());
            });

            using (var server = TestServer.Create(builder => startup.UseDefaultTestSetup(builder)))
            {
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}", RouteConstants.MembersSegment)),
                    Method     = HttpMethod.Post,
                };

                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json"));
                //NOTE: it is missing
                request.Content = new StringContent(@"{
   ""contentTypeAlias"": ""testType"",
  ""name"": ""John Johnson"",
  ""email"" : ""*****@*****.**"",
  ""userName"" : ""johnjohnson"",
  ""properties"": {
    ""TestProperty1"": """",
    ""testProperty2"": ""property value2""
  }
}", Encoding.UTF8, "application/json");

                Console.WriteLine(request);
                var result = await server.HttpClient.SendAsync(request);

                Console.WriteLine(result);

                var json = await((StreamContent)result.Content).ReadAsStringAsync();
                Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented));

                Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);

                var djson = JsonConvert.DeserializeObject <JObject>(json);

                Assert.AreEqual(1, djson["totalResults"].Value <int>());
                Assert.AreEqual("content.properties.TestProperty1.value", djson["_embedded"]["errors"][0]["logRef"].Value <string>());
            }
        }
Пример #3
0
        public async void Post_Is_400_Validation_Property_Missing()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (request, umbCtx, typedContent, serviceContext, searchProvider) =>
            {
                MemberServiceMocks.SetupMocksForPost(serviceContext);
            });

            using (var server = TestServer.Create(builder => startup.Configuration(builder)))
            {
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}", RouteConstants.MembersSegment)),
                    Method     = HttpMethod.Post,
                };

                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json"));
                //NOTE: it is missing
                request.Content = new StringContent(@"{
  ""contentTypeAlias"": ""testType"",
  ""name"": ""John Johnson"",
  ""email"" : ""*****@*****.**"",
  ""userName"" : ""johnjohnson"",
  ""parentId"": 456,
  ""templateId"": 9,
  ""properties"": {
    ""thisDoesntExist"": ""property value1"",
    ""testProperty2"": ""property value2""
  }
}", Encoding.UTF8, "application/json");

                Console.WriteLine(request);
                var result = await server.HttpClient.SendAsync(request);

                Console.WriteLine(result);

                var json = await((StreamContent)result.Content).ReadAsStringAsync();
                Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented));

                Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);

                var djson = JsonConvert.DeserializeObject <JObject>(json);

                Assert.AreEqual(1, djson["totalResults"].Value <int>());
                Assert.AreEqual("content.properties.thisDoesntExist", djson["_embedded"]["errors"][0]["logRef"].Value <string>());
            }
        }
        public async Task Post_Is_201_Response()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (testServices) =>
            {
                MemberServiceMocks.SetupMocksForPost(testServices.ServiceContext);
            });

            await base.Post_Is_201_Response(startup.UseDefaultTestSetup, RouteConstants.MembersSegment, new StringContent(@"{
  ""contentTypeAlias"": ""testType"",
  ""name"": ""John Johnson"",
  ""email"" : ""*****@*****.**"",
  ""userName"" : ""johnjohnson"",
  ""properties"": {
    ""TestProperty1"": ""property value1"",
    ""testProperty2"": ""property value2""
  }
}", Encoding.UTF8, "application/json"));
        }
        public async Task Put_Is_200_Response()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (testServices) =>
            {
                MemberServiceMocks.SetupMocksForPost(testServices.ServiceContext);
            });

            using (var server = TestServer.Create(builder => startup.UseDefaultTestSetup(builder)))
            {
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}/123", RouteConstants.MembersSegment)),
                    Method     = HttpMethod.Put,
                };

                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json"));
                request.Content = new StringContent(@"{
  ""contentTypeAlias"": ""testType"",
  ""name"": ""John Johnson"",
  ""email"" : ""*****@*****.**"",
  ""userName"" : ""johnjohnson"",
  ""properties"": {
    ""TestProperty1"": ""property value1"",
    ""testProperty2"": ""property value2""
  }
}", Encoding.UTF8, "application/json");

                Console.WriteLine(request);
                var result = await server.HttpClient.SendAsync(request);

                Console.WriteLine(result);

                var json = await((StreamContent)result.Content).ReadAsStringAsync();
                Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented));

                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            }
        }