Ejemplo n.º 1
0
        public void SingleHtmlDownloadGetStream()
        {
            IDocument document = Substitute.For<IDocument>();
            Stream stream = null;
            IEnumerable<MetadataItem> metadata = null;
            string source = null;
            document
                .When(x => x.Clone(Arg.Any<string>(), Arg.Any<Stream>(), Arg.Any<IEnumerable<MetadataItem>>(), Arg.Any<bool>()))
                .Do(x =>
                {
                    source = x.Arg<string>();
                    stream = x.Arg<Stream>();
                    metadata = x.Arg<IEnumerable<MetadataItem>>();
                });

            IModule download = new Download().WithUris("http://www.siwawi.com/");
            IExecutionContext context = Substitute.For<IExecutionContext>();

            // When
            download.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

            // Then

            Assert.IsNotEmpty(source, "Source cannot be empty");
            Console.WriteLine("Source " + source);

            var headers = metadata.FirstOrDefault(x => x.Key == Keys.SourceHeaders).Value as Dictionary<string, string>;

            Assert.IsNotNull(headers, "Header cannot be null");
            Assert.IsTrue(headers.Count > 0, "Headers must contain contents");

            foreach (var h in headers)
            {
                Assert.IsNotEmpty(h.Key, "Header key cannot be empty");
                Assert.IsNotEmpty(h.Value, "Header value cannot be empty");
                Console.WriteLine($"{h.Key} - {h.Value}");
            }

            stream.Seek(0, SeekOrigin.Begin);
            var content = new StreamReader(stream).ReadToEnd();
            stream.Dispose();

            Assert.IsNotEmpty(content, "Download cannot be empty");
            Console.WriteLine("Content " + content);
        }
Ejemplo n.º 2
0
            public void MultipleHtmlDownload()
            {
                IDocument document = Substitute.For<IDocument>();

                var output = new List<Tuple<Stream, IEnumerable<KeyValuePair<string, object>>>>();

                IExecutionContext context = Substitute.For<IExecutionContext>();
                context
                    .When(x => x.GetDocument(Arg.Any<Stream>(), Arg.Any<IEnumerable<KeyValuePair<string, object>>>(), Arg.Any<bool>()))
                    .Do(x =>
                    {
                        output.Add(Tuple.Create(x.Arg<Stream>(), x.Arg<IEnumerable<KeyValuePair<string, object>>>()));
                    });

                IModule download = new Download().WithUris("http://www.siwawi.com/", "http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream");

                // When
                download.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                foreach(var o in output)
                {
                    var headers = o.Item2.FirstOrDefault(x => x.Key == Keys.SourceHeaders).Value as Dictionary<string, string>;

                    Assert.IsNotNull(headers, "Header cannot be null");
                    Assert.IsTrue(headers.Count > 0, "Headers must contain contents");

                    foreach (var h in headers)
                    {
                        Assert.IsNotEmpty(h.Key, "Header key cannot be empty");
                        Assert.IsNotEmpty(h.Value, "Header value cannot be empty");
                    }

                    o.Item1.Seek(0, SeekOrigin.Begin);
                    var content = new StreamReader(o.Item1).ReadToEnd();
                    o.Item1.Dispose();

                    Assert.IsNotEmpty(content, "Download cannot be empty");
                }
            }
Ejemplo n.º 3
0
            public void SingleImageDownloadWithRequestHeader()
            {
                IDocument document = Substitute.For<IDocument>();
                Stream stream = null;
                IEnumerable<KeyValuePair<string, object>> metadata = null;

                IExecutionContext context = Substitute.For<IExecutionContext>();
                context
                    .When(x => x.GetDocument(Arg.Any<string>(), Arg.Any<Stream>(), Arg.Any<IEnumerable<KeyValuePair<string, object>>>()))
                    .Do(x =>
                    {
                        stream = x.Arg<Stream>();
                        metadata = x.Arg<IEnumerable<KeyValuePair<string, object>>>();
                    });

                var header = new RequestHeader();
                header.Accept.Add("image/jpeg");

                IModule download = new Download().WithUri("http://siwawi.com/images/cover/617215_113386155490459_1547184305_o-cover.jpg", header);
                context.OutputFolder.Returns(x => AssemblyDirectory);

                // When
                download.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                stream.Seek(0, SeekOrigin.Begin);

                var path = Path.Combine(context.OutputFolder, "test-with-request-header.jpg");
                File.WriteAllBytes(path, ReadToByte(stream));
                stream.Dispose();

                Assert.IsTrue(File.Exists(path), "Download cannot be empty");
            }
Ejemplo n.º 4
0
            public void SingleImageDownloadWithRequestHeader()
            {
                IDocument document = Substitute.For<IDocument>();
                Stream stream = null;
                IEnumerable<KeyValuePair<string, object>> metadata = null;

                IExecutionContext context = Substitute.For<IExecutionContext>();
                context
                    .When(x => x.GetDocument(Arg.Any<FilePath>(), Arg.Any<Stream>(), Arg.Any<IEnumerable<KeyValuePair<string, object>>>()))
                    .Do(x =>
                    {
                        stream = x.Arg<Stream>();
                        metadata = x.Arg<IEnumerable<KeyValuePair<string, object>>>();
                    });

                var header = new RequestHeader();
                header.Accept.Add("image/jpeg");

                IModule download = new Download().WithUri("http://wyam.io/Content/images/nav-logo.png", header);

                // When
                download.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                stream.Seek(0, SeekOrigin.Begin);
                Assert.AreNotEqual(-1, stream.ReadByte());
                stream.Dispose();
            }