Example #1
0
        public void UploadFileWithIncorrectHashTest()
        {
            MemoryStream fileContents = new MemoryStream(Encoding.UTF8.GetBytes("Hello World"));
            var request = new FileUploadRequest
            {
                FileContents = fileContents,
                ContentSHA1 = "0a4d55a8d778e5022fab701977c5d840bbc486d1", //correct hash is 0a4d55a8d778e5022fab701977c5d840bbc486d0
                FileName = "hello.txt",
                ContentType = "text/plain"
            };

            try
            {
                var response = client.UploadFile(request);
            }

            catch (InvalidFileHashException ex)
            {
                //todo: ask about consistency of error messages
                Assert.AreEqual("bz_sha1 did not match data received", ex.Message);
            }
        }
Example #2
0
        public void TestListFileNames()
        {
            var client = new ServiceStackB2Api();
            List<UploadFileResponse> uploadedList = new List<UploadFileResponse>();

            var bucket = client.CreateBucket("ListFileNamesTestBucket");

            MemoryStream fileContents = new MemoryStream(Encoding.UTF8.GetBytes("Hello World"));

            var request = new FileUploadRequest
            {
                FileContents = fileContents,
                ContentSHA1 = null,
                FileName = "one.txt",
                ContentType = "text/plain"
            };

            uploadedList.Add(client.UploadFile(request));
            request.FileContents.Seek(0, SeekOrigin.Begin);

            request.FileName = "two.txt";
            uploadedList.Add(client.UploadFile(request));
            request.FileContents.Seek(0, SeekOrigin.Begin);

            request.FileName = "three.txt";
            uploadedList.Add(client.UploadFile(request));
            request.FileContents.Seek(0, SeekOrigin.Begin);

            request.FileName = "four.txt";
            uploadedList.Add(client.UploadFile(request));
            request.FileContents.Seek(0, SeekOrigin.Begin);

            var response = client.ListFileNames(new ListFileNamesRequest() {BucketId = bucket.BucketId});
            foreach (var responseFile in response.Files)
            {
                Assert.IsTrue(uploadedList.Contains(responseFile));
            }
        }
Example #3
0
        private HttpWebRequest CreateUploadHttpWebRequest(FileUploadRequest fileUploadRequest, UploadUrlResponse uploadUrlResponse)
        {
            var request = (HttpWebRequest)WebRequest.Create(uploadUrlResponse.UploadUrl);
            request.Method = "POST";
            request.Headers.Add("Authorization", uploadUrlResponse.AuthorizationToken);

            if (fileUploadRequest.LastModified != new DateTime()) //todo: how do I actually check if a datetime is set?
            {
                request.Headers.Add("X-Bz-Info-src_last_modified_millis",
                    fileUploadRequest.LastModified.ToUnixTime().ToString());
            }

            if (string.IsNullOrEmpty(fileUploadRequest.ContentSHA1))
            {
                fileUploadRequest.ContentSHA1 = ComputeSha1(fileUploadRequest.FileContents);
                fileUploadRequest.FileContents.Position = 0; //todo: what about non-seekable streams?
            }

            request.Headers.Add("X-Bz-Content-Sha1", fileUploadRequest.ContentSHA1);
            request.Headers.Add("X-Bz-File-Name", GetSafeEncodedFilename(fileUploadRequest.FileName));

            if (string.IsNullOrEmpty(fileUploadRequest.ContentType))
            {
                fileUploadRequest.ContentType = "b2/x-auto";
            }

            request.ContentType = fileUploadRequest.ContentType;
            using (var requestStream = request.GetRequestStream())
            {
                fileUploadRequest.FileContents.CopyTo(requestStream);
            }

            return request;
        }
Example #4
0
        public UploadFileResponse UploadFile(FileUploadRequest fileUploadRequest)
        {
            //http://stackoverflow.com/questions/15999769/how-do-i-set-the-contenttype-in-a-servicestack-client
            var uploadUrlResponse = GetUploadUrl(fileUploadRequest.BucketId);
            var request = CreateUploadHttpWebRequest(fileUploadRequest, uploadUrlResponse);

            try
            {
                using (var reader = new StreamReader(request.GetResponse().GetResponseStream()))
                {
                    return new JsonSerializer<UploadFileResponse>().DeserializeFromReader(reader);
                }
            }

            catch (WebException we)
            {
                var error = we.ToErrorResponse();
                if (error.Message.Contains("sha1")) //todo: always lowercase?
                {
                    throw new InvalidFileHashException(error); //todo: naming?
                }

                throw new BadRequestException(error);
            }
        }
Example #5
0
        public void UploadFileWithShortHashTest()
        {
            MemoryStream fileContents = new MemoryStream(Encoding.UTF8.GetBytes("Hello World"));
            var request = new FileUploadRequest
            {
                FileContents = fileContents,
                ContentSHA1 = "FFFF",
                FileName = "hello.txt",
                ContentType = "text/plain"
            };

            try
            {
                var response = client.UploadFile(request);
            }

            catch (InvalidFileHashException ex)
            {
                Assert.AreEqual("sha1 is wrong length", ex.Message);
            }
        }
Example #6
0
        public void UploadTextFileTest()
        {
            MemoryStream fileContents = new MemoryStream(Encoding.UTF8.GetBytes("Hello World"));
            var request = new FileUploadRequest
            {
                FileContents = fileContents,
                ContentSHA1 = null,
                FileName = "hello.txt",
                ContentType = "text/plain"
            };

            var response = client.UploadFile(request);

            Assert.AreEqual(request.ContentSHA1, response.ContentSha1);
            Assert.AreEqual(request.ContentType, response.ContentType);
            Assert.AreEqual(request.FileName.UrlDecode(), response.FileName.UrlDecode());
        }