Exemplo n.º 1
0
            public HttpClient GetHttpClient()
            {
                CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();

                CustomizableMockHttpClient.Responder         sentinal   = this.DefaultChangesResponder();
                Queue <CustomizableMockHttpClient.Responder> responders = new List <CustomizableMockHttpClient.Responder
                                                                                    >();

                responders.AddItem(this.DefaultChangesResponder());
                responders.AddItem(CustomizableMockHttpClient.TransientErrorResponder(errorCode,
                                                                                      statusMessage));
                ResponderChain responderChain = new ResponderChain(responders, sentinal);

                mockHttpClient.SetResponder("_changes", responderChain);
                return(mockHttpClient);
            }
 /// <summary>https://github.com/couchbase/couchbase-lite-android/issues/247</summary>
 /// <exception cref="System.Exception"></exception>
 public virtual void RunPushReplicationWithTransientError(int statusCode, string statusMsg
     , bool expectReplicatorError)
 {
     IDictionary<string, object> properties1 = new Dictionary<string, object>();
     properties1.Put("doc1", "testPushReplicationTransientError");
     CreateDocWithProperties(properties1);
     CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
     mockHttpClient.AddResponderFakeLocalDocumentUpdate404();
     CustomizableMockHttpClient.Responder sentinal = CustomizableMockHttpClient.FakeBulkDocsResponder
         ();
     Queue<CustomizableMockHttpClient.Responder> responders = new List<CustomizableMockHttpClient.Responder
         >();
     responders.AddItem(CustomizableMockHttpClient.TransientErrorResponder(statusCode, 
         statusMsg));
     ResponderChain responderChain = new ResponderChain(responders, sentinal);
     mockHttpClient.SetResponder("_bulk_docs", responderChain);
     // create a replication observer to wait until replication finishes
     CountDownLatch replicationDoneSignal = new CountDownLatch(1);
     LiteTestCase.ReplicationFinishedObserver replicationFinishedObserver = new LiteTestCase.ReplicationFinishedObserver
         (replicationDoneSignal);
     // create replication and add observer
     manager.SetDefaultHttpClientFactory(MockFactoryFactory(mockHttpClient));
     Replication pusher = database.CreatePushReplication(GetReplicationURL());
     pusher.AddChangeListener(replicationFinishedObserver);
     // save the checkpoint id for later usage
     string checkpointId = pusher.RemoteCheckpointDocID();
     // kick off the replication
     pusher.Start();
     // wait for it to finish
     bool success = replicationDoneSignal.Await(60, TimeUnit.Seconds);
     NUnit.Framework.Assert.IsTrue(success);
     Log.D(Tag, "replicationDoneSignal finished");
     if (expectReplicatorError == true)
     {
         NUnit.Framework.Assert.IsNotNull(pusher.GetLastError());
     }
     else
     {
         NUnit.Framework.Assert.IsNull(pusher.GetLastError());
     }
     // workaround for the fact that the replicationDoneSignal.wait() call will unblock before all
     // the statements in Replication.stopped() have even had a chance to execute.
     // (specifically the ones that come after the call to notifyChangeListeners())
     Sharpen.Thread.Sleep(500);
     string localLastSequence = database.LastSequenceWithCheckpointId(checkpointId);
     if (expectReplicatorError == true)
     {
         NUnit.Framework.Assert.IsNull(localLastSequence);
     }
     else
     {
         NUnit.Framework.Assert.IsNotNull(localLastSequence);
     }
 }
 /// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/188</summary>
 /// <exception cref="System.Exception"></exception>
 public virtual void TestServerDoesNotSupportMultipart()
 {
     NUnit.Framework.Assert.AreEqual(0, database.GetLastSequenceNumber());
     IDictionary<string, object> properties1 = new Dictionary<string, object>();
     properties1.Put("dynamic", 1);
     Document doc = CreateDocWithProperties(properties1);
     SavedRevision doc1Rev = doc.GetCurrentRevision();
     // Add attachment to document
     UnsavedRevision doc2UnsavedRev = doc.CreateRevision();
     InputStream attachmentStream = GetAsset("attachment.png");
     doc2UnsavedRev.SetAttachment("attachment.png", "image/png", attachmentStream);
     SavedRevision doc2Rev = doc2UnsavedRev.Save();
     NUnit.Framework.Assert.IsNotNull(doc2Rev);
     CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
     mockHttpClient.AddResponderFakeLocalDocumentUpdate404();
     Queue<CustomizableMockHttpClient.Responder> responders = new List<CustomizableMockHttpClient.Responder
         >();
     //first http://url/db/foo (foo==docid)
     //Reject multipart PUT with response code 415
     responders.AddItem(new _Responder_1691());
     // second http://url/db/foo (foo==docid)
     // second call should be plain json, return good response
     responders.AddItem(new _Responder_1701(doc));
     ResponderChain responderChain = new ResponderChain(responders);
     mockHttpClient.SetResponder(doc.GetId(), responderChain);
     // create replication and add observer
     manager.SetDefaultHttpClientFactory(MockFactoryFactory(mockHttpClient));
     Replication pusher = database.CreatePushReplication(GetReplicationURL());
     RunReplication(pusher);
     IList<HttpWebRequest> captured = mockHttpClient.GetCapturedRequests();
     int entityIndex = 0;
     foreach (HttpWebRequest httpRequest in captured)
     {
         // verify that there are no PUT requests with attachments
         if (httpRequest is HttpPut)
         {
             HttpPut httpPut = (HttpPut)httpRequest;
             HttpEntity entity = httpPut.GetEntity();
             if (entityIndex++ == 0)
             {
                 NUnit.Framework.Assert.IsTrue("PUT request with attachment is not multipart", entity
                      is MultipartEntity);
             }
             else
             {
                 NUnit.Framework.Assert.IsFalse("PUT request with attachment is multipart", entity
                      is MultipartEntity);
             }
         }
     }
 }
 public HttpClient GetHttpClient()
 {
     CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
     CustomizableMockHttpClient.Responder sentinal = this.DefaultChangesResponder();
     Queue<CustomizableMockHttpClient.Responder> responders = new List<CustomizableMockHttpClient.Responder
         >();
     responders.AddItem(this.DefaultChangesResponder());
     responders.AddItem(CustomizableMockHttpClient.TransientErrorResponder(errorCode, 
         statusMessage));
     ResponderChain responderChain = new ResponderChain(responders, sentinal);
     mockHttpClient.SetResponder("_changes", responderChain);
     return mockHttpClient;
 }