Ejemplo n.º 1
0
        public void TestIssue490()
        {
            var sg = new CouchDB("http", GetReplicationServer());
            using (var remoteDb = sg.CreateDatabase("issue490")) {

                var push = database.CreatePushReplication(remoteDb.RemoteUri);
                CreateFilteredDocuments(database, 30);
                CreateNonFilteredDocuments (database, 10);
                RunReplication(push);
                Assert.IsTrue(push.ChangesCount==40);
                Assert.IsTrue(push.CompletedChangesCount==40);

                Assert.IsNull(push.LastError);
                Assert.AreEqual(40, database.DocumentCount);

                for (int i = 0; i <= 5; i++) {
                    pull = database.CreatePullReplication(remoteDb.RemoteUri);
                    pull.Continuous = true;
                    pull.Start ();
                    Task.Delay (1000).Wait();
                    CallToView ();
                    Task.Delay (2000).Wait();
                    RecreateDatabase ();
                }
            }

        }
        public void TestUpdateToServerSavesAttachment()
        {
            var couchDb = new CouchDB(GetReplicationProtocol(), GetReplicationServer());
            using (var remoteDb = couchDb.CreateDatabase(TempDbName())) {
                var pull = database.CreatePullReplication(remoteDb.RemoteUri);
                pull.Continuous = true;
                pull.Start();

                var docName = "doc" + Convert.ToString((ulong)DateTime.UtcNow.TimeSinceEpoch().TotalMilliseconds);
                var endpoint = remoteDb.RemoteUri.AppendPath(docName);
                var docContent = Encoding.UTF8.GetBytes("{\"foo\":false}");
                var putRequest = new HttpRequestMessage(HttpMethod.Put, endpoint);
                putRequest.Content = new StringContent("{\"foo\":false}");
                putRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var response = _httpClient.SendAsync(putRequest).Result;
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);


                var attachmentStream = GetAsset("attachment.png");
                var baos = new MemoryStream();
                attachmentStream.CopyTo(baos);
                attachmentStream.Dispose();
                endpoint = endpoint.AppendPath("attachment?rev=1-1153b140e4c8674e2e6425c94de860a0");
                docContent = baos.ToArray();
                baos.Dispose();

                putRequest = new HttpRequestMessage(HttpMethod.Put, endpoint);
                putRequest.Content = new ByteArrayContent(docContent);
                putRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                response = _httpClient.SendAsync(putRequest).Result;
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);

                endpoint = remoteDb.RemoteUri.AppendPath(docName + "?rev=2-bb71ce0da1de19f848177525c4ae5a8b");
                const string docContentStr = "{\"foo\":true,\"_attachments\":{\"attachment\":{\"content_type\":\"image/png\",\"revpos\":2,\"digest\":\"md5-ks1IBwCXbuY7VWAO9CkEjA==\",\"length\":519173,\"stub\":true}}}";
                putRequest = new HttpRequestMessage(HttpMethod.Put, endpoint);
                putRequest.Content = new StringContent(docContentStr);
                putRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = _httpClient.SendAsync(putRequest).Result;
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);

                Sleep(1000);
                while (pull.Status == ReplicationStatus.Active) {
                    Sleep(500);
                }

                Assert.IsNull(pull.LastError);
                Assert.AreEqual(ReplicationStatus.Idle, pull.Status);
                var doc = database.GetExistingDocument(docName);
                Assert.IsNotNull(doc);
                Assert.IsNotNull(doc.CurrentRevision.Attachments);

                StopReplication(pull);
            }
        }
        [Test] // Issue #449
        public void TestPushAttachmentToCouchDB()
        {
            var couchDb = new CouchDB(GetReplicationProtocol(), GetReplicationServer());
            using (var remoteDb = couchDb.CreateDatabase(TempDbName())) {

                var push = database.CreatePushReplication(remoteDb.RemoteUri);
                CreateDocuments(database, 2);
                var attachDoc = database.CreateDocument();
                var newRev = attachDoc.CreateRevision();
                var newProps = newRev.UserProperties;
                newProps["foo"] = "bar";
                newRev.SetUserProperties(newProps);
                var attachmentStream = GetAsset("attachment.png");
                newRev.SetAttachment("attachment.png", "image/png", attachmentStream);
                newRev.Save();
                
                RunReplication(push);
                Assert.AreEqual(3, push.ChangesCount);
                Assert.AreEqual(3, push.CompletedChangesCount);
                attachDoc = database.GetExistingDocument(attachDoc.Id);
                attachDoc.Update(rev =>
                {
                    var props = rev.UserProperties;
                    props["extraminutes"] = "5";
                    rev.SetUserProperties(props);
                    return true;
                });

                push = database.CreatePushReplication(push.RemoteUrl);
                RunReplication(push);

                database.Close();
                database = EnsureEmptyDatabase(database.Name);
                var pull = database.CreatePullReplication(push.RemoteUrl);
                RunReplication(pull);
                Assert.AreEqual(3, database.GetDocumentCount());
                attachDoc = database.GetExistingDocument(attachDoc.Id);
                Assert.IsNotNull(attachDoc, "Failed to retrieve doc with attachment");
                Assert.IsNotNull(attachDoc.CurrentRevision.Attachments, "Failed to retrieve attachments on attachment doc");
                attachDoc.Update(rev =>
                {
                    var props = rev.UserProperties;
                    props["extraminutes"] = "10";
                    rev.SetUserProperties(props);
                    return true;
                });

                push = database.CreatePushReplication(pull.RemoteUrl);
                RunReplication(push);
                Assert.IsNull(push.LastError);
                Assert.AreEqual(1, push.ChangesCount);
                Assert.AreEqual(1, push.CompletedChangesCount);
                Assert.AreEqual(3, database.GetDocumentCount());
            }
        }
Ejemplo n.º 4
0
        public void TestUpdateToServerSavesAttachment()
        {
            var couchDb = new CouchDB(GetReplicationProtocol(), GetReplicationServer());
            using (var remoteDb = couchDb.CreateDatabase(TempDbName())) {
                var pull = database.CreatePullReplication(remoteDb.RemoteUri);
                pull.Continuous = true;
                pull.Start();

                var docName = "doc" + Convert.ToString(DateTime.UtcNow.ToMillisecondsSinceEpoch());
                var endpoint = remoteDb.RemoteUri.AppendPath(docName);
                var docContent = Encoding.UTF8.GetBytes("{\"foo\":false}");
                var putRequest = HttpWebRequest.Create(endpoint);
                putRequest.Method = "PUT";
                putRequest.ContentType = "application/json";
                putRequest.GetRequestStream().Write(docContent, 0, docContent.Length);
                var response = (HttpWebResponse)putRequest.GetResponse();
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);


                var attachmentStream = (InputStream)GetAsset("attachment.png");
                var baos = new MemoryStream();
                attachmentStream.Wrapped.CopyTo(baos);
                attachmentStream.Dispose();
                endpoint = endpoint.AppendPath("attachment?rev=1-1153b140e4c8674e2e6425c94de860a0");
                docContent = baos.ToArray();

                putRequest = HttpWebRequest.Create(endpoint);
                putRequest.Method = "PUT";
                putRequest.ContentType = "image/png";
                putRequest.GetRequestStream().Write(docContent, 0, docContent.Length);
                response = (HttpWebResponse)putRequest.GetResponse();
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);

                endpoint = remoteDb.RemoteUri.AppendPath(docName + "?rev=2-bb71ce0da1de19f848177525c4ae5a8b");
                docContent = Encoding.UTF8.GetBytes("{\"foo\":true,\"_attachments\":{\"attachment\":{\"content_type\":\"image/png\",\"revpos\":2,\"digest\":\"md5-ks1IBwCXbuY7VWAO9CkEjA==\",\"length\":519173,\"stub\":true}}}");
                putRequest = HttpWebRequest.Create(endpoint);
                putRequest.Method = "PUT";
                putRequest.ContentType = "application/json";
                putRequest.GetRequestStream().Write(docContent, 0, docContent.Length);
                response = (HttpWebResponse)putRequest.GetResponse();
                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);

                Thread.Sleep(1000);
                while (pull.Status == ReplicationStatus.Active) {
                    Thread.Sleep(500);
                }

                Assert.IsNull(pull.LastError);
                Assert.AreEqual(ReplicationStatus.Idle, pull.Status);
                var doc = database.GetExistingDocument(docName);
                Assert.IsNotNull(doc);
                Assert.IsNotNull(doc.CurrentRevision.Attachments);
            }
        }