Esempio n. 1
0
        public void Plug_timeout_is_not_used_for_message_memorization()
        {
            var blockingStream = new MockBlockingStream();

            MockPlug.Register(new XUri("mock://mock"), (plug, verb, uri, request, response) => {
                _log.Debug("returning blocking stream");
                response.Return(new DreamMessage(DreamStatus.Ok, null, MimeType.TEXT, -1, blockingStream));
            });
            var stopwatch = Stopwatch.StartNew();
            var msg       = Plug.New(MockPlug.DefaultUri)
                            .WithTimeout(TimeSpan.FromSeconds(1))
                            .InvokeEx(Verb.GET, DreamMessage.Ok(), new Result <DreamMessage>()).Wait();

            stopwatch.Stop();
            _log.Debug("completed request");
            Assert.AreEqual(DreamStatus.Ok, msg.Status);
            Assert.LessOrEqual(stopwatch.Elapsed.Seconds, 1);
            stopwatch = Stopwatch.StartNew();
            _log.Debug("memorizing request");
            var r = msg.Memorize(new Result(1.Seconds())).Block();

            stopwatch.Stop();
            blockingStream.Unblock();
            _log.Debug("completed request memorization");
            Assert.LessOrEqual(stopwatch.Elapsed.Seconds, 2);
            Assert.IsTrue(r.HasTimedOut);
        }
Esempio n. 2
0
        public void Can_use_Plug_extension_to_return_document_result()
        {
            var autoMockPlug = MockPlug.Register(new XUri("mock://mock"));

            autoMockPlug.Expect().Verb("GET").Response(DreamMessage.Ok(new XDoc("works")));
            Assert.AreEqual("works", Plug.New("mock://mock").Get(new Result <XDoc>()).Wait().Name);
        }
Esempio n. 3
0
        public void Plug_timeout_on_request_returns_RequestConnectionTimeout_not_ResponseDataTransferTimeout()
        {
            var blockingStream = new MockBlockingStream();

            MockPlug.Register(new XUri("mock://mock"), (plug, verb, uri, request, response) => {
                _log.Debug("blocking request");
                Thread.Sleep(5.Seconds());
                _log.Debug("returning blocking stream");
                response.Return(new DreamMessage(DreamStatus.Ok, null, MimeType.TEXT, -1, blockingStream));
            });
            var stopwatch = Stopwatch.StartNew();

            _log.Debug("calling plug");
            var r = Plug.New(MockPlug.DefaultUri)
                    .WithTimeout(1.Seconds())
                    .Get(new Result <DreamMessage>(5.Seconds())).Block();

            _log.Debug("plug done");
            stopwatch.Stop();
            blockingStream.Unblock();
            Assert.GreaterOrEqual(stopwatch.Elapsed.Seconds, 1);
            Assert.LessOrEqual(stopwatch.Elapsed.Seconds, 3);
            Assert.IsFalse(r.HasTimedOut);
            Assert.IsFalse(r.HasException);
            Assert.AreEqual(DreamStatus.RequestConnectionTimeout, r.Value.Status);
        }
Esempio n. 4
0
        public void Mock_sends_back_proper_response_body()
        {
            int                   called        = 0;
            Plug                  calledPlug    = null;
            string                calledVerb    = null;
            XUri                  calledUri     = null;
            DreamMessage          calledRequest = null;
            Result <DreamMessage> calledResponse;
            XDoc                  responseDoc = new XDoc("message").Elem("foo");

            MockPlug.Register(new XUri("http://www.mindtouch.com/foo"), delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                calledPlug     = p;
                calledVerb     = v;
                calledUri      = u;
                calledRequest  = r;
                calledResponse = r2;
                called++;
                calledResponse.Return(DreamMessage.Ok(responseDoc));
            });
            XDoc         doc      = new XDoc("message").Elem("foo");
            DreamMessage response = Plug.New("http://www.mindtouch.com").At("foo").GetAsync().Wait();

            Assert.IsTrue(response.IsSuccessful, response.ToText());
            Assert.AreEqual(1, called);
            Assert.AreEqual("GET", calledVerb);
            Assert.AreEqual(responseDoc, response.ToDocument());
            Assert.AreEqual(calledUri, new XUri("http://www.mindtouch.com/foo"));
        }
Esempio n. 5
0
        public void Mock_intercepts_child_path()
        {
            int                   called     = 0;
            Plug                  calledPlug = null;
            string                calledVerb = null;
            XUri                  calledUri  = null;
            DreamMessage          calledRequest;
            Result <DreamMessage> calledResponse;

            MockPlug.Register(new XUri("http://www.mindtouch.com"), delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                calledPlug     = p;
                calledVerb     = v;
                calledUri      = u;
                calledRequest  = r;
                calledResponse = r2;
                called++;
                calledResponse.Return(DreamMessage.Ok());
            });

            Plug         plug     = Plug.New("http://www.mindtouch.com").At("foo");
            DreamMessage response = plug.GetAsync().Wait();

            Assert.IsTrue(response.IsSuccessful, response.ToText());
            Assert.AreEqual(1, called);
            Assert.AreEqual("GET", calledVerb);
            Assert.AreEqual(new XUri("http://www.mindtouch.com").At("foo"), calledUri);
            Assert.AreEqual(plug, calledPlug);
        }
Esempio n. 6
0
        public void DeregisterAll_clears_all_mocks()
        {
            int  firstCalled = 0;
            XUri uri         = new XUri("http://www.mindtouch.com/foo");

            MockPlug.Register(uri, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                firstCalled++;
                r2.Return(DreamMessage.Ok());
            });
            MockPlug.Register(new XUri("http://www.mindtouch.com/bar"), delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                r2.Return(DreamMessage.Ok());
            });
            Assert.IsTrue(Plug.New(uri).GetAsync().Wait().IsSuccessful);
            Assert.AreEqual(1, firstCalled);
            MockPlug.DeregisterAll();
            int secondCalled = 0;

            MockPlug.Register(uri, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                secondCalled++;
                r2.Return(DreamMessage.Ok());
            });
            MockPlug.Register(new XUri("http://www.mindtouch.com/bar"), delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                r2.Return(DreamMessage.Ok());
            });
            Assert.IsTrue(Plug.New(uri).GetAsync().Wait().IsSuccessful);
            Assert.AreEqual(1, firstCalled);
            Assert.AreEqual(1, secondCalled);
        }
Esempio n. 7
0
        public void Can_match_request_DreamMessages_for_expectation()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect().Verb("POST").Uri(new XUri("http://auto/plug/a")).Request(DreamMessage.Ok(MimeType.TEXT_UTF8, "blah"));
            Async.Fork(() => Plug.New("http://auto/plug/a").PostAsync(DreamMessage.Ok(MimeType.TEXT_UTF8, "blah")), new Result());
            Assert.IsTrue(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
        }
Esempio n. 8
0
        public void Matches_request_doc_to_expectations()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect().Verb("POST").Uri(new XUri("http://auto/plug/a")).RequestDocument(new XDoc("foo"));
            Async.Fork(() => Plug.New("http://auto/plug/a").PostAsync(new XDoc("foo")), new Result());
            Assert.IsTrue(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
        }
Esempio n. 9
0
        public void Ignores_excess_headers()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect().Verb("POST").Uri(new XUri("http://auto/plug/a"));
            Async.Fork(() => Plug.New("http://auto/plug/a").WithHeader("Foo", "123").PostAsync(), new Result());
            Assert.IsTrue(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
        }
Esempio n. 10
0
        public void Excess_expectations_are_BadRequest()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));
            DreamMessage message  = Plug.New("http://auto/plug/a").GetAsync().Wait();

            Assert.IsFalse(message.IsSuccessful);
            Assert.AreEqual(DreamStatus.BadRequest, message.Status);
            Assert.IsFalse(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
            Assert.AreEqual(1, autoPlug.ExcessInterceptions.Length);
        }
Esempio n. 11
0
        public void Catches_mismatched_request_headers()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect().Verb("GET").Uri(new XUri("http://auto/plug/a"))
            .RequestHeader("Foo", "123")
            .RequestHeader("Bar", "right");
            AsyncUtil.Fork(() => Plug.New("http://auto/plug/a").WithHeader("Foo", "123").WithHeader("Bar", "wrong").Get(new Result <DreamMessage>()), new Result());
            Assert.IsFalse(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
            Assert.AreEqual("Expectations were unmet:\r\nExpectation #1: Expected header 'Bar:\r\nExpected: right\r\nGot:      wrong\r\n", autoPlug.VerificationFailure);
        }
Esempio n. 12
0
        public void Catches_missing_headers()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect().Verb("GET").Uri(new XUri("http://auto/plug/a"))
            .RequestHeader("Foo", "123")
            .RequestHeader("Bar", "required");
            Async.Fork(() => Plug.New("http://auto/plug/a").WithHeader("Foo", "123").GetAsync(), new Result());
            Assert.IsFalse(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
            Assert.AreEqual("Expectations were unmet:\r\nExpectation #1: Expected header 'Bar', got none\r\n", autoPlug.VerificationFailure);
        }
Esempio n. 13
0
        public void Autoplug_without_expectations_should_still_fail_on_excess()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            Async.Fork(() => {
                Plug.New("http://auto/plug/b").PutAsync(new XDoc("foo"));
                Plug.New("http://auto/plug/a").PostAsync();
            }, new Result());
            Assert.IsFalse(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
            Assert.AreEqual(2, autoPlug.ExcessInterceptions.Length);
        }
Esempio n. 14
0
        public void Plug_extension_to_return_document_sets_exception_on_non_OK_response()
        {
            var autoMockPlug = MockPlug.Register(new XUri("mock://mock"));

            autoMockPlug.Expect().Verb("GET").Response(DreamMessage.BadRequest("bad puppy"));
            var r = Plug.New("mock://mock").Get(new Result <XDoc>()).Block();

            Assert.IsTrue(r.HasException);
            Assert.AreEqual(typeof(DreamResponseException), r.Exception.GetType());
            Assert.AreEqual(DreamStatus.BadRequest, ((DreamResponseException)r.Exception).Response.Status);
        }
Esempio n. 15
0
        public void Considers_missordered_expectations_as_unmet()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plugx"));

            autoPlug.Expect("POST", new XUri("http://auto/plugx/a"));
            autoPlug.Expect("PUT", new XUri("http://auto/plugx/b"), new XDoc("foo"));
            Async.Fork(() => {
                Plug.New("http://auto/plugx/b").Put(new XDoc("foo"));
                Plug.New("http://auto/plugx/a").Post();
            }, new Result());
            Assert.IsFalse(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
            Assert.AreEqual(0, autoPlug.MetExpectationCount);
        }
Esempio n. 16
0
        public void Should_be_able_to_call_same_url_with_different_headers()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect().Verb("GET").Uri(new XUri("http://auto/plug/a")).RequestHeader("Foo", "");
            autoPlug.Expect().Verb("GET").Uri(new XUri("http://auto/plug/a")).RequestHeader("Foo", "baz");
            Plug p = Plug.New("http://auto/plug/a");

            Async.Fork(() => {
                p.WithHeader("Foo", "").GetAsync().Block();
                p.WithHeader("Foo", "baz").GetAsync().Block();
            }, new Result());
            Assert.IsTrue(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
        }
Esempio n. 17
0
        public void Complains_about_unmet_expectations_after_timeout()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect("POST", new XUri("http://auto/plug/a"));
            autoPlug.Expect("PUT", new XUri("http://auto/plug/b"), new XDoc("foo"));
            Assert.IsFalse(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);

            autoPlug.Reset();
            autoPlug.Expect("POST", new XUri("http://auto/plug/a"));
            autoPlug.Expect("PUT", new XUri("http://auto/plug/b"), new XDoc("foo"));
            Async.Fork(() => Plug.New("http://auto/plug/a").Post(), new Result());
            Assert.IsFalse(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
            Assert.AreEqual(1, autoPlug.MetExpectationCount);
        }
Esempio n. 18
0
        public void PostAsync_from_nested_async_workers()
        {
            AutoResetEvent resetEvent = new AutoResetEvent(false);

            MockPlug.Register(new XUri("http://foo/bar"), delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                resetEvent.Set();
                r2.Return(DreamMessage.Ok());
            });
            Plug.New("http://foo/bar").PostAsync();
            Assert.IsTrue(resetEvent.WaitOne(1000, false), "no async failed");
            Async.Fork(() => Async.Fork(() => Plug.New("http://foo/bar").PostAsync(), new Result()), new Result());
            Assert.IsTrue(resetEvent.WaitOne(1000, false), "async failed");
            Async.Fork(() => Async.Fork(() => Plug.New("http://foo/bar").PostAsync(), new Result()), new Result());
            Assert.IsTrue(resetEvent.WaitOne(1000, false), "nested async failed");
            Async.Fork(() => Async.Fork(() => Async.Fork(() => Plug.New("http://foo/bar").PostAsync(), new Result()), new Result()), new Result());
            Assert.IsTrue(resetEvent.WaitOne(1000, false), "double async failed");
        }
Esempio n. 19
0
        public void Result_timeout_superceeds_plug_timeout_and_results_in_RequestConnectionTimeout()
        {
            MockPlug.Register(new XUri("mock://mock"), (plug, verb, uri, request, response) => {
                Thread.Sleep(TimeSpan.FromSeconds(10));
                response.Return(DreamMessage.Ok());
            });
            var stopwatch = Stopwatch.StartNew();
            var r         = Plug.New(MockPlug.DefaultUri)
                            .WithTimeout(TimeSpan.FromSeconds(20))
                            .InvokeEx(Verb.GET, DreamMessage.Ok(), new Result <DreamMessage>(1.Seconds())).Block();

            stopwatch.Stop();
            Assert.LessOrEqual(stopwatch.Elapsed.Seconds, 2);
            Assert.IsFalse(r.HasTimedOut);
            Assert.IsFalse(r.HasException);
            Assert.AreEqual(DreamStatus.RequestConnectionTimeout, r.Value.Status);
        }
Esempio n. 20
0
        public void Register_twice_throws()
        {
            XUri uri = new XUri("http://www.mindtouch.com/foo");

            MockPlug.Register(uri, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                r2.Return(DreamMessage.Ok());
            });
            try {
                MockPlug.Register(uri, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                    r2.Return(DreamMessage.Ok());
                });
            } catch (ArgumentException) {
                return;
            } catch (Exception e) {
                Assert.Fail("wrong exception: " + e);
            }
            Assert.Fail("no exception`");
        }
Esempio n. 21
0
        public void Collects_excess_expectations()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect("POST");
            Async.Fork(() => {
                Plug.New("http://auto/plug/a").PostAsync().Block();
                Plug.New("http://auto/plug/b").PostAsync().Block();
                Plug.New("http://auto/plug/c").PostAsync().Block();
                Plug.New("http://auto/plug/d").PostAsync().Block();
            }, new Result());
            Assert.IsFalse(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(15)), autoPlug.VerificationFailure);
            Assert.IsTrue(autoPlug.HasInterceptsInExcessOfExpectations);
            Assert.AreEqual(3, autoPlug.ExcessInterceptions.Length);
            Assert.AreEqual("http://auto/plug/b", autoPlug.ExcessInterceptions[0].Uri.ToString());
            Assert.AreEqual("http://auto/plug/c", autoPlug.ExcessInterceptions[1].Uri.ToString());
            Assert.AreEqual("http://auto/plug/d", autoPlug.ExcessInterceptions[2].Uri.ToString());
        }
Esempio n. 22
0
        public void Waits_until_expectations_are_met_after_each_reset()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            autoPlug.Expect("POST", new XUri("http://auto/plug/a"));
            autoPlug.Expect("PUT", new XUri("http://auto/plug/b"), new XDoc("foo"));
            Async.Fork(() => {
                Plug.New("http://auto/plug/a").Post();
                Plug.New("http://auto/plug/b").Put(new XDoc("foo"));
            }, new Result());
            Assert.IsTrue(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(2)), autoPlug.VerificationFailure);
            autoPlug.Reset();
            autoPlug.Expect("GET", new XUri("http://auto/plug/c"));
            autoPlug.Expect("GET", new XUri("http://auto/plug/d"));
            Async.Fork(() => {
                Plug.New("http://auto/plug/c").Get();
                Plug.New("http://auto/plug/d").Get();
            }, new Result());
            Assert.IsTrue(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(2)), autoPlug.VerificationFailure);
        }
Esempio n. 23
0
        public void Deregister_allows_reregister_of_uri()
        {
            XUri uri         = new XUri("http://www.mindtouch.com/foo");
            int  firstCalled = 0;

            MockPlug.Register(uri, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                firstCalled++;
                r2.Return(DreamMessage.Ok());
            });
            Assert.IsTrue(Plug.New(uri).Get(new Result <DreamMessage>()).Wait().IsSuccessful);
            Assert.AreEqual(1, firstCalled);
            MockPlug.Deregister(uri);
            int secondCalled = 0;

            MockPlug.Register(uri, delegate(Plug p, string v, XUri u, DreamMessage r, Result <DreamMessage> r2) {
                secondCalled++;
                r2.Return(DreamMessage.Ok());
            });
            Assert.IsTrue(Plug.New(uri).Get(new Result <DreamMessage>()).Wait().IsSuccessful);
            Assert.AreEqual(1, firstCalled);
            Assert.AreEqual(1, secondCalled);
        }
Esempio n. 24
0
        private void SetupService(bool memorizeAliases)
        {
            DreamHostInfo host;

            if (memorizeAliases)
            {
                host = _hostAliasMemorize = DreamTestHelper.CreateRandomPortHost();
            }
            else
            {
                host = _hostNoAliasMemorize = DreamTestHelper.CreateRandomPortHost(new XDoc("config").Elem("memorize-aliases", false));
            }
            _service     = MockService.CreateMockService(host);
            _calledPaths = new List <string>();
            _service.Service.CatchAllCallback = (context, request, result) => {
                _calledPaths.Add(context.GetSuffixes(UriPathFormat.Original).First());
                var call = context.GetParam("call", null);
                if (!string.IsNullOrEmpty(call))
                {
                    Plug.New(call).Get();
                }
                result.Return(DreamMessage.Ok());
            };
            _external = new XUri("http://external1/").WithPort(_service.AtLocalHost.Uri.Port).At(_service.AtLocalHost.Uri.Segments);
            //_ext2 = new XUri("http://external2/").WithPort(_service.AtLocalHost.Uri.Port).At(_service.AtLocalHost.Uri.Segments);
            _externalCalled = 0;
            MockPlug.Register(_external, (plug, verb, uri, request, response) => {
                _externalCalled++;
                response.Return(DreamMessage.Ok());
            }, 100);
            //_ext2called = 0;
            //MockPlug.Register(_ext2, (plug, verb, uri, request, response) => {
            //    _ext2called++;
            //    response.Return(DreamMessage.Ok());
            //}, 100);
            Plug.New(_external).Get();
            //Plug.New(_ext2).Get();
            //return _calledPaths;
        }
Esempio n. 25
0
        public void Autoplug_without_expectations_wait_for_timeout()
        {
            AutoMockPlug autoPlug = MockPlug.Register(new XUri("http://auto/plug"));

            Assert.IsTrue(autoPlug.WaitAndVerify(TimeSpan.FromSeconds(1)), autoPlug.VerificationFailure);
        }