Exemple #1
0
        public async Task WhenQueryParametersAreAddedViaDictionary_UrlIsCorrectlyConstructed()
        {
            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK));
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            http.Query(new Dictionary<string, string>{{"foo", "Foo"}, {"bar", "Bar"}});
            
            await http.Get();

            Assert.AreEqual("http://foo.com/?foo=Foo&bar=Bar", stubHandler.LastRequest.RequestUri.ToString());
        }
Exemple #2
0
        public async Task WhenGetXmlIsCalled_AndContentIsGarbage_ExceptionIsThrown()
        {
            var expectedString = "!@#$%^&*()_+<root><foo@#$%^&*()_>$%^&*(OP)_Foo</foo><bar>Bar</bar></root>";

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                                       new TestLogger(),
                                       new Envelope(new TextMessage(CreateTestUser(), "test")),
                                       stubHandler);

            Assert.Throws <AggregateException>(() => http.GetXml().Result);
        }
Exemple #3
0
        public async Task WhenGetJson_AndContentContainsGarbage_ThrowsException()
        {
            var expected = "dfsgsdf%#@$%^&*()";

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expected)
            });
            var http = new HttpWrapper("http://foo.com/",
                                       new TestLogger(),
                                       new Envelope(new TextMessage(CreateTestUser(), "test")),
                                       stubHandler);

            Assert.Throws <AggregateException>(() => http.GetJson().Result);
        }
Exemple #4
0
        public async Task WhenQueryParametersAreAddedindividually_UrlIsCorrectlyConstructed()
        {
            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK));
            var http        = new HttpWrapper("http://foo.com/",
                                              new TestLogger(),
                                              new Envelope(new TextMessage(CreateTestUser(), "test")),
                                              stubHandler);

            http.Query("foo", "Foo");
            http.Query("bar", "Bar");

            await http.Get();

            Assert.Equal("http://foo.com/?foo=Foo&bar=Bar", stubHandler.LastRequest.RequestUri.ToString());
        }
Exemple #5
0
        public async Task WhenQueryParametersAreAddedByAnonymousType_UrlIsCorrectlyConstructed()
        {
            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK));
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(), 
                new Envelope(new TextMessage(CreateTestUser(), "test")),
                stubHandler);

            http.Query(new
            {
                foo = "Foo",
                bar = "Bar"
            });

            await http.Get();

            Assert.Equal("http://foo.com/?foo=Foo&bar=Bar", stubHandler.LastRequest.RequestUri.ToString());
        }
Exemple #6
0
        public async Task WhenGetJsonIsCalled_ResponseContentIsDeserialized()
        {
            var expectedString = JsonConvert.SerializeObject(new { Id = 4, Foo = "Foo", Bar = "Bar", Date = DateTime.Now });
            var expected       = JsonConvert.DeserializeObject(expectedString);

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                                       new TestLogger(),
                                       new Envelope(new TextMessage(CreateTestUser(), "test")),
                                       stubHandler);

            var actual = await http.GetJson();


            Assert.True(new JTokenEqualityComparer().Equals((JToken)expected, (JToken)actual));
        }
Exemple #7
0
        public async Task WhenGetJsonIsCalled_ResponseContentIsDeserialized()
        {
            var expectedString = JsonConvert.SerializeObject(new {Id=4, Foo = "Foo", Bar = "Bar", Date = DateTime.Now});
            var expected = JsonConvert.DeserializeObject(expectedString);

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            var actual = await http.GetJson();


            Assert.IsTrue(new JTokenEqualityComparer().Equals((JToken)expected, (JToken)actual));
        }
Exemple #8
0
        public async Task WhenGetXmlIsCalled_ResponseContentIsDeserialized()
        {
            var expectedString = "<root><foo>Foo</foo><bar>Bar</bar></root>";
            var expected       = new XmlDocument();

            expected.LoadXml(expectedString);

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                                       new TestLogger(),
                                       new Envelope(new TextMessage(CreateTestUser(), "test")),
                                       stubHandler);

            var actual = await http.GetXml();

            Assert.Equal(expected.ToString(), actual.ToString());
        }
Exemple #9
0
        public async Task WhenGetJsonWithCallbackReturnsErrorHttpStatusCode_CodeIsAccessibleViaResponseParameter()
        {
            var expectedString = JsonConvert.SerializeObject(new { Id = 4, Foo = "Foo", Bar = "Bar", Date = DateTime.Now });
            var expected       = JsonConvert.DeserializeObject(expectedString);

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                                       new TestLogger(),
                                       new Envelope(new TextMessage(CreateTestUser(), "test")),
                                       stubHandler);

            var callback = false;
            await http.GetJson((err, res, body) =>
            {
                callback = true;
                Assert.Equal(HttpStatusCode.NotFound, res.StatusCode);
            });

            Assert.True(callback);
        }
Exemple #10
0
        public async Task WhenGetXmlWithCallback_AndContentIsGarbage_ErrContainsException()
        {
            var expectedString = "!@#$%^&*()_+<root><foo@#$%^&*()_>$%^&*(OP)_Foo</foo><bar>Bar</bar></root>";

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                                       new TestLogger(),
                                       new Envelope(new TextMessage(CreateTestUser(), "test")),
                                       stubHandler);

            var callback = false;
            await http.GetXml((err, res, body) =>
            {
                callback = true;
                Assert.NotNull(err);
                Assert.IsType <HttpRequestException>(err);
            });

            Assert.True(callback);
        }
Exemple #11
0
        public async Task WhenGetJsonWithCallback_AndContentContainsGarbage_ThrowsException()
        {
            var expected = "dfsgsdf%#@$%^&*()";

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expected)
            });
            var http = new HttpWrapper("http://foo.com/",
                                       new TestLogger(),
                                       new Envelope(new TextMessage(CreateTestUser(), "test")),
                                       stubHandler);

            var callback = false;
            await http.GetJson((err, res, body) =>
            {
                callback = true;
                Assert.NotNull(err);
                Assert.IsType <JsonReaderException>(err);
            });

            Assert.True(callback);
        }
Exemple #12
0
        public async Task WhenGetJsonWithCallbackReturnsErrorHttpStatusCode_CodeIsAccessibleViaResponseParameter()
        {
            var expectedString = JsonConvert.SerializeObject(new { Id = 4, Foo = "Foo", Bar = "Bar", Date = DateTime.Now });
            var expected = JsonConvert.DeserializeObject(expectedString);

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            var callback = false;
            await http.GetJson((err, res, body) =>
            {
                callback = true;
                Assert.AreEqual(HttpStatusCode.NotFound, res.StatusCode);
            });
            Assert.IsTrue(callback);

        }
Exemple #13
0
        public async Task WhenGetJsonWithCallback_AndContentContainsGarbage_ThrowsException()
        {
            var expected = "dfsgsdf%#@$%^&*()";

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expected)
            });
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            var callback = false;
            await http.GetJson((err, res, body) =>
            {
                callback = true;
                Assert.IsNotNull(err);
                Assert.IsInstanceOfType(err, typeof(Exception));
                
            });

            Assert.IsTrue(callback);
        }
Exemple #14
0
        public async Task WhenGetXmlIsCalled_ResponseContentIsDeserialized()
        {
            var expectedString = "<root><foo>Foo</foo><bar>Bar</bar></root>";
            var expected = new XmlDocument();
            expected.LoadXml(expectedString);

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            var actual = await http.GetXml();

            Assert.AreEqual(expected.ToString(), actual.ToString());
        }
Exemple #15
0
        public async Task WhenGetXmlWithCallbackReturnsErrorHttpStatusCode_CodeIsAccessibleViaResponseParameter()
        {
            var expectedString = "<root><foo>Foo</foo><bar>Bar</bar></root>";
            var expected = new XmlDocument();
            expected.LoadXml(expectedString);

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            var callback = false;
            await http.GetXml((err, res, body) =>
            {
                callback = true;
                Assert.AreEqual(HttpStatusCode.NotFound, res.StatusCode);
            });
            Assert.IsTrue(callback);

        }
Exemple #16
0
        public async Task WhenGetXmlIsCalled_AndContentIsGarbage_ExceptionIsThrown()
        {
            var expectedString = "!@#$%^&*()_+<root><foo@#$%^&*()_>$%^&*(OP)_Foo</foo><bar>Bar</bar></root>";
            
            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            await http.GetXml();

            Assert.Fail("Should have thrown");
        }
Exemple #17
0
        public async Task WhenGetXmlWithCallback_AndContentIsGarbage_ErrContainsException()
        {
            var expectedString = "!@#$%^&*()_+<root><foo@#$%^&*()_>$%^&*(OP)_Foo</foo><bar>Bar</bar></root>";

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(expectedString)
            });
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            var callback = false;
            await http.GetXml((err, res, body) =>
            {
                callback = true;
                Assert.IsNotNull(err);
                Assert.IsInstanceOfType(err, typeof(Exception));
            });
            Assert.IsTrue(callback);

        }
Exemple #18
0
        public async Task WhenGetJson_AndContentContainsGarbage_ThrowsException()
        {
            var expected = "dfsgsdf%#@$%^&*()";

            var stubHandler = new FakeHttpMessageHandler(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expected)
            });
            var http = new HttpWrapper("http://foo.com/",
                new TestLogger(),
                new Envelope(new TextMessage(CreateTestUser(), "test", "id")),
                stubHandler);

            await http.GetJson();
            Assert.Fail("Should have thrown");
            
        }