public void ValidateParts_ListContainsInvalidPart()
        {
            // Arrange
            Application app         = TestDataUtil.GetApplication("ttd", "events");
            string      partName    = "hundebilde";
            string      dataTypeId  = "hundebilde";
            string      contentType = "application/pdf";

            RequestPart validPart = new RequestPart
            {
                Name        = partName,
                ContentType = contentType,
                FileSize    = 1337
            };

            RequestPart invalidPart = new RequestPart
            {
                Name        = partName,
                ContentType = contentType,
                FileSize    = 1337 * 1024 * 1024
            };

            List <RequestPart> parts = new List <RequestPart> {
                validPart, invalidPart
            };

            string expectedErrorMessage = $"The multipart section named {partName} exceeds the size limit of element type '{dataTypeId}'";
            RequestPartValidator sut    = new RequestPartValidator(app);

            // Act
            string actual = sut.ValidateParts(parts);

            // Assert
            Assert.Equal(expectedErrorMessage, actual);
        }
        public void ValidatePart_InvalidContentSize()
        {
            // Arrange
            Application app         = TestDataUtil.GetApplication("ttd", "events");
            string      partName    = "hundebilde";
            string      contentType = "application/pdf";

            using Stream stream = new MemoryStream();

            RequestPart part = new RequestPart
            {
                Name        = partName,
                ContentType = contentType,
                Stream      = stream
            };

            string expectedErrorMessage = $"The multipart section named {partName} has no data. Cannot process empty part.";
            RequestPartValidator sut    = new RequestPartValidator(app);

            // Act
            string actual = sut.ValidatePart(part);

            // Assert
            Assert.Equal(expectedErrorMessage, actual);
        }
        public void ValidateParts_ExceedsAllowedNuberOfDataElements()
        {
            // Arrange
            Application app         = TestDataUtil.GetApplication("ttd", "events");
            string      partName    = "hundebilde";
            string      dataTypeId  = "hundebilde";
            string      contentType = "application/pdf";

            RequestPart part = new RequestPart
            {
                Name        = partName,
                ContentType = contentType,
                FileSize    = 1337
            };

            List <RequestPart> parts = new List <RequestPart> {
                part, part
            };

            string expectedErrorMessage = $"The list of parts contains more elements of type '{dataTypeId}' than the element type allows.";
            RequestPartValidator sut    = new RequestPartValidator(app);

            // Act
            string actual = sut.ValidateParts(parts);

            // Assert
            Assert.Equal(expectedErrorMessage, actual);
        }
Пример #4
0
        public void ValidatePartTest_ValidElement_ReturnsNull()
        {
            // Arrange
            Application application = new Application
            {
                ElementTypes = new List <ElementType>
                {
                    new ElementType {
                        Id = "default", AllowedContentType = new List <string> {
                            "application/xml"
                        }, MaxCount = 1
                    }
                }
            };

            RequestPartValidator target = new RequestPartValidator(application);

            RequestPart part = new RequestPart
            {
                Name        = "default",
                ContentType = "application/xml",
                Stream      = new MemoryStream(Encoding.UTF8.GetBytes("viktig melding"))
            };

            // Act
            string error = target.ValidatePart(part);

            // Assert
            Assert.Null(error);
        }
Пример #5
0
        /// <summary>The built-in Grace "print" method</summary>
        public GraceObject Print(EvaluationContext ctx, GraceObject arg)
        {
            Object obj = arg;
            var    gop = arg as GraceObjectProxy;

            if (gop != null)
            {
                obj = gop.Object;
            }
            var go = obj as GraceObject;

            if (go != null)
            {
                var req  = new MethodRequest();
                var part = RequestPart.Nullary("asString");
                req.AddPart(part);
                if (go.RespondsTo(req))
                {
                    obj = go.Request(ctx, req);
                }
            }
            GraceString gs = null;

            go = obj as GraceObject;
            if (go != null)
            {
                gs = go.FindNativeParent <GraceString>();
            }
            if (gs != null)
            {
                obj = gs.Value.Replace("\u2028", Environment.NewLine);
            }
            sink.WriteLine("" + obj);
            return(GraceObject.Done);
        }
Пример #6
0
        public static void MakeRequest(Dispatcher dispatcher, RequestPart url, bool isSearch)
        {
            if (dispatcher != null)
            {
                Dispatcher = dispatcher;
            }
            HttpWebRequest request;

            try {
                request = (HttpWebRequest)WebRequest.Create(url.ToString());
            } catch {
                //TODO: Actually do something here
                //Raise error or notify what happened
                return;
            }
            request.Method      = "POST";
            request.ContentType = "application/json; charset=utf-8";

            if (isSearch)
            {
                request.BeginGetResponse(new AsyncCallback(ResponseHandler.SearchRequestCallback), request);
            }
            else if (url is AllRequestPart)
            {
                request.BeginGetResponse(new AsyncCallback(ResponseHandler.AllRequestCallback), request);
            }
            else if (url is TopRequestPart)
            {
                request.BeginGetResponse(new AsyncCallback(ResponseHandler.TopRequestCallback), request);
            }
            else
            {
                request.BeginGetResponse(new AsyncCallback(ResponseHandler.RequestCallback), request);
            }
        }
Пример #7
0
 public RequestNode(Type type, RequestPart request)
 {
     Type     = type;
     Name     = type.Name;
     Request  = request;
     Children = new ObservableCollection <RequestNode>();
     CreatePropertyTree();
 }
        public void ValidatePart_MissingContentType()
        {
            // Arrange
            Application app      = TestDataUtil.GetApplication("ttd", "events");
            string      partName = "hundebilde";

            RequestPart part = new RequestPart
            {
                Name = partName
            };

            string expectedErrorMessage = $"The multipart section named {partName} is missing Content-Type.";
            RequestPartValidator sut    = new RequestPartValidator(app);

            // Act
            string actual = sut.ValidatePart(part);

            // Assert
            Assert.Equal(expectedErrorMessage, actual);
        }
        public void ValidatePart_InvalidDataType()
        {
            // Arrange
            Application app      = TestDataUtil.GetApplication("ttd", "events");
            string      partName = "kattebilde";

            RequestPart part = new RequestPart
            {
                Name = partName
            };

            string expectedErrorMessage = $"Multipart section named, '{partName}' does not correspond to an element type in application metadata";
            RequestPartValidator sut    = new RequestPartValidator(app);

            // Act
            string actual = sut.ValidatePart(part);

            // Assert
            Assert.Equal(expectedErrorMessage, actual);
        }
Пример #10
0
        public void ValidatePartTest_InstanceElementWithWrongContentType_ReturnsError()
        {
            // Arrange
            Application application = new Application();

            RequestPartValidator target = new RequestPartValidator(application);

            RequestPart part = new RequestPart
            {
                Name        = "instance",
                ContentType = "application/xml",
                Stream      = new MemoryStream(Encoding.UTF8.GetBytes("viktig melding")) // Content isn't validated atm.
            };

            // Act
            string error = target.ValidatePart(part);

            // Assert
            Assert.Contains("Expecting 'application/json'", error);
        }
Пример #11
0
        public void ValidatePartTest_ValidInstanceElement_ReturnsNull()
        {
            // Arrange
            Application application = new Application();

            RequestPartValidator target = new RequestPartValidator(application);

            RequestPart part = new RequestPart
            {
                Name        = "instance",
                ContentType = "application/json",
                Stream      = new MemoryStream(Encoding.UTF8.GetBytes("viktig melding")) // Content isn't validated atm.
            };

            // Act
            string error = target.ValidatePart(part);

            // Assert
            Assert.Null(error);
        }
        public void ValidatePart_ValidateInstance_ValidInstance()
        {
            // Arrange
            Application app         = TestDataUtil.GetApplication("ttd", "events");
            string      partName    = "instance";
            string      contentType = "application/json";

            RequestPart part = new RequestPart
            {
                Name        = partName,
                ContentType = contentType,
            };

            RequestPartValidator sut = new RequestPartValidator(app);

            // Act
            var actual = sut.ValidatePart(part);

            // Assert
            Assert.Null(actual);
        }
        public void ValidatePart_ValidateInstance_InvalidContentType()
        {
            // Arrange
            Application app         = TestDataUtil.GetApplication("ttd", "events");
            string      partName    = "instance";
            string      contentType = "application/pdf";

            RequestPart part = new RequestPart
            {
                Name        = partName,
                ContentType = contentType,
            };

            string expectedErrorMessage = $"Unexpected Content-Type '{contentType}' of embedded instance template. Expecting 'application/json'";
            RequestPartValidator sut    = new RequestPartValidator(app);

            // Act
            string actual = sut.ValidatePart(part);

            // Assert
            Assert.Equal(expectedErrorMessage, actual);
        }
        public void ValidatePart_InvalidContentType()
        {
            // Arrange
            Application app         = TestDataUtil.GetApplication("ttd", "events");
            string      partName    = "hundebilde";
            string      dataTypeId  = "hundebilde";
            string      contentType = "application/xml";

            RequestPart part = new RequestPart
            {
                Name        = partName,
                ContentType = contentType
            };

            string expectedErrorMessage = $"The multipart section named {partName} has a Content-Type '{contentType}' which is invalid for element type '{dataTypeId}'";
            RequestPartValidator sut    = new RequestPartValidator(app);

            // Act
            string actual = sut.ValidatePart(part);

            // Assert
            Assert.Equal(expectedErrorMessage, actual);
        }
        /// <summary>
        /// Extracts the instance template from a multipart reader, which contains a number of parts. If the reader contains
        /// only one part and it has no name and contentType application/json it is assumed to be an instance template.
        ///
        /// If found the method removes the part corresponding to the instance template form the parts list.
        /// </summary>
        /// <param name="reader">multipart reader object</param>
        /// <returns>the instance template or null if none is found</returns>
        private async Task <Instance> ExtractInstanceTemplate(MultipartRequestReader reader)
        {
            Instance instanceTemplate = null;

            RequestPart instancePart = reader.Parts.Find(part => part.Name == "instance");

            // assume that first part with no name is an instanceTemplate
            if (instancePart == null && reader.Parts.Count == 1 && reader.Parts[0].ContentType.Contains("application/json") && reader.Parts[0].Name == null)
            {
                instancePart = reader.Parts[0];
            }

            if (instancePart != null)
            {
                reader.Parts.Remove(instancePart);

                using StreamReader streamReader = new StreamReader(instancePart.Stream, Encoding.UTF8);
                string content = await streamReader.ReadToEndAsync();

                instanceTemplate = JsonConvert.DeserializeObject <Instance>(content);
            }

            return(instanceTemplate);
        }
Пример #16
0
 public static void MakeRequest(Dispatcher dispatcher, RequestPart url)
 {
     Connection.MakeRequest(dispatcher, url, false);
 }