Exemplo n.º 1
0
        public void HttpMessagesAreSentAndReceivedWhenReceiverDoesRollback()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5000/"))
            {
                string payload = null;

                receiver.Start(m =>
                {
                    payload = m.StringPayload;
                    m.Rollback();
                });

                using (var sender = new HttpClientSender("foo", "http://localhost:5000/"))
                {
                    Assert.ThrowsAny <HttpRequestException>(() => sender.Send("Hello, world!"));
                }

                Assert.Equal("Hello, world!", payload);
            }
        }
Exemplo n.º 2
0
        public async Task HttpMessagesAreSentAndReceivedWhenReceiverDoesRollback()
        {
            using (var receiver = new HttpListenerReceiver("foo", new Uri("http://localhost:5002/")))
            {
                string?payload = null;

                receiver.Start(async m =>
                {
                    payload = m.StringPayload;
                    await m.RollbackAsync().ConfigureAwait(false);
                });

                using (var sender = new HttpClientSender("foo", new Uri("http://localhost:5002/")))
                {
                    await Assert.ThrowsAnyAsync <HttpRequestException>(() => sender.SendAsync("Hello, world!")).ConfigureAwait(false);
                }

                Assert.Equal("Hello, world!", payload);
            }
        }
Exemplo n.º 3
0
        public void TokensInHttpClientSenderUrlWithoutACorrespondingHeaderThrowsInvalidOperationException()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5000/"))
            {
                string payload = null;

                receiver.Start(m =>
                {
                    payload = m.StringPayload;
                    m.Acknowledge();
                });

                using (var sender = new HttpClientSender("foo", "http://{server}:5000/"))
                {
                    var message = new SenderMessage("Hello, world!");
                    Assert.Throws <InvalidOperationException>(() => sender.Send(message));
                }

                Assert.Null(payload);
            }
        }
Exemplo n.º 4
0
        public void MismatchedMethodsResultsIn405()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5000/", method: "POST"))
            {
                string payload = null;

                receiver.Start(m =>
                {
                    payload = m.StringPayload;
                    m.Acknowledge();
                });

                using (var sender = new HttpClientSender("foo", "http://localhost:5000/", "PUT"))
                {
                    var exception = Assert.Throws <HttpRequestException>(() => sender.Send("Hello, world!"));
                    Assert.Contains("405 (Method Not Allowed)", exception.Message);
                }

                Assert.Null(payload);
            }
        }
Exemplo n.º 5
0
        public async Task TokensInHttpClientSenderUrlWithoutACorrespondingHeaderThrowsInvalidOperationException()
        {
            using (var receiver = new HttpListenerReceiver("foo", new Uri("http://localhost:5004/")))
            {
                string?payload = null;

                receiver.Start(async m =>
                {
                    payload = m.StringPayload;
                    await m.AcknowledgeAsync().ConfigureAwait(false);
                });

                using (var sender = new HttpClientSender("foo", "http://{server}:5004/"))
                {
                    var message = new SenderMessage("Hello, world!");
                    await Assert.ThrowsAsync <InvalidOperationException>(() => sender.SendAsync(message)).ConfigureAwait(false);
                }

                Assert.Null(payload);
            }
        }
Exemplo n.º 6
0
        public void HttpMessagesAreSentAndReceived()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5000/", method: "PUT", requiredHeaders: new RequiredHttpRequestHeaders(contentType: "application/json", accept: "application/json")))
            {
                string payload = null;

                receiver.Start(m =>
                {
                    payload = m.StringPayload;
                    m.Acknowledge();
                });

                using (var sender = new HttpClientSender("foo", "http://localhost:5000/", method: "PUT", defaultHeaders: new Dictionary <string, string> {
                    { "Content-Type", "application/json" }, { "Accept", "application/json" }
                }))
                {
                    sender.Send("Hello, world!");
                }

                Assert.Equal("Hello, world!", payload);
            }
        }
Exemplo n.º 7
0
        public async Task HttpMessagesAreSentAndReceivedUsingStringUrlAsync()
        {
            using (var receiver = new HttpListenerReceiver("foo", new Uri("http://localhost:5001/"), method: "PUT", requiredHeaders: new RequiredHttpRequestHeaders(contentType: "application/json", accept: "application/json")))
            {
                string?payload = null;

                receiver.Start(async m =>
                {
                    payload = m.StringPayload;
                    await m.AcknowledgeAsync().ConfigureAwait(false);
                });

                using (var sender = new HttpClientSender("foo", "http://localhost:5001/", method: "PUT", defaultHeaders: new Dictionary <string, string> {
                    { "Content-Type", "application/json" }, { "Accept", "application/json" }
                }))
                {
                    await sender.SendAsync("Hello, world!").ConfigureAwait(false);
                }

                Assert.Equal("Hello, world!", payload);
            }
        }
Exemplo n.º 8
0
        public async Task MismatchedMethodsResultsIn405()
        {
            using (var receiver = new HttpListenerReceiver("foo", new Uri("http://localhost:5007/"), method: "POST"))
            {
                string?payload = null;

                receiver.Start(async m =>
                {
                    payload = m.StringPayload;
                    await m.AcknowledgeAsync().ConfigureAwait(false);
                });

                using (var sender = new HttpClientSender("foo", new Uri("http://localhost:5007/"), "PUT"))
                {
                    var exception = await Assert.ThrowsAsync <HttpRequestException>(() => sender.SendAsync("Hello, world!")).ConfigureAwait(false);

                    Assert.Contains("405 (Method Not Allowed)", exception.Message, StringComparison.InvariantCultureIgnoreCase);
                }

                Assert.Null(payload);
            }
        }
Exemplo n.º 9
0
        public void MismatchedAcceptResultsIn406()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5000/", requiredHeaders: new RequiredHttpRequestHeaders(accept: "application/json")))
            {
                string payload = null;

                receiver.Start(m =>
                {
                    payload = m.StringPayload;
                    m.Acknowledge();
                });

                using (var sender = new HttpClientSender("foo", "http://localhost:5000/", defaultHeaders: new Dictionary <string, string> {
                    { "Accept", "application/xml" }
                }))
                {
                    var exception = Assert.Throws <HttpRequestException>(() => sender.Send("Hello, world!"));
                    Assert.Contains("406 (Not Acceptable)", exception.Message);
                }

                Assert.Null(payload);
            }
        }
Exemplo n.º 10
0
        public void ExtraPathAfterTokenResultIn404()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5000/api/{api_version}"))
            {
                string payload    = null;
                string apiVersion = null;

                receiver.Start(m =>
                {
                    payload    = m.StringPayload;
                    apiVersion = m.Headers.GetValue <string>("api_version");
                    m.Acknowledge();
                });

                using (var sender = new HttpClientSender("foo", "http://localhost:5000/API/v2/extra"))
                {
                    var exception = Assert.Throws <HttpRequestException>(() => sender.Send("Hello, world!"));
                    Assert.Contains("404 (Not Found)", exception.Message);
                }

                Assert.Null(payload);
            }
        }
Exemplo n.º 11
0
        public void TokensInHttpListenerReceiverPathAreExtractedIntoHeaders()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5000/api/{api_version}"))
            {
                string payload    = null;
                string apiVersion = null;

                receiver.Start(m =>
                {
                    payload    = m.StringPayload;
                    apiVersion = m.Headers.GetValue <string>("api_version");
                    m.Acknowledge();
                });

                using (var sender = new HttpClientSender("foo", "http://localhost:5000/API/v2/"))
                {
                    sender.Send("Hello, world!");
                }

                Assert.Equal("Hello, world!", payload);
                Assert.Equal("v2", apiVersion);
            }
        }
Exemplo n.º 12
0
        public async Task TokensInHttpListenerReceiverPathAreExtractedIntoHeaders()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5005/api/{api_version}"))
            {
                string?payload    = null;
                string?apiVersion = null;

                receiver.Start(async m =>
                {
                    payload    = m.StringPayload;
                    apiVersion = m.Headers.GetValue <string>("api_version");
                    await m.AcknowledgeAsync().ConfigureAwait(false);
                });

                using (var sender = new HttpClientSender("foo", new Uri("http://localhost:5005/API/v2/")))
                {
                    await sender.SendAsync("Hello, world!").ConfigureAwait(false);
                }

                Assert.Equal("Hello, world!", payload);
                Assert.Equal("v2", apiVersion);
            }
        }
Exemplo n.º 13
0
        public void TokensInHttpClientSenderUrlAreReplacedByMatchingHeaders()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5000/"))
            {
                string payload = null;

                receiver.Start(m =>
                {
                    payload = m.StringPayload;
                    m.Acknowledge();
                });

                using (var sender = new HttpClientSender("foo", "http://{server}:5000/"))
                {
                    var message = new SenderMessage("Hello, world!")
                    {
                        Headers = { ["server"] = "localhost" }
                    };
                    sender.Send(message);
                }

                Assert.Equal("Hello, world!", payload);
            }
        }
Exemplo n.º 14
0
        public async Task TokensInHttpClientSenderUrlAreReplacedByMatchingHeaders()
        {
            using (var receiver = new HttpListenerReceiver("foo", new Uri("http://localhost:5003/")))
            {
                string?payload = null;

                receiver.Start(async m =>
                {
                    payload = m.StringPayload;
                    await m.AcknowledgeAsync().ConfigureAwait(false);
                });

                using (var sender = new HttpClientSender("foo", "http://{server}:5003/"))
                {
                    var message = new SenderMessage("Hello, world!")
                    {
                        Headers = { ["server"] = "localhost" }
                    };
                    await sender.SendAsync(message).ConfigureAwait(false);
                }

                Assert.Equal("Hello, world!", payload);
            }
        }
Exemplo n.º 15
0
        public async Task MismatchedAcceptResultsIn406()
        {
            using (var receiver = new HttpListenerReceiver("foo", new Uri("http://localhost:5009/"), requiredHeaders: new RequiredHttpRequestHeaders(accept: "application/json")))
            {
                string?payload = null;

                receiver.Start(async m =>
                {
                    payload = m.StringPayload;
                    await m.AcknowledgeAsync().ConfigureAwait(false);
                });

                using (var sender = new HttpClientSender("foo", new Uri("http://localhost:5009/"), defaultHeaders: new Dictionary <string, string> {
                    { "Accept", "application/xml" }
                }))
                {
                    var exception = await Assert.ThrowsAsync <HttpRequestException>(() => sender.SendAsync("Hello, world!")).ConfigureAwait(false);

                    Assert.Contains("406 (Not Acceptable)", exception.Message, StringComparison.InvariantCultureIgnoreCase);
                }

                Assert.Null(payload);
            }
        }
Exemplo n.º 16
0
        public async Task ExtraPathAfterTokenResultIn404()
        {
            using (var receiver = new HttpListenerReceiver("foo", "http://localhost:5006/api/{api_version}"))
            {
                string?payload    = null;
                string?apiVersion = null;

                receiver.Start(async m =>
                {
                    payload    = m.StringPayload;
                    apiVersion = m.Headers.GetValue <string>("api_version");
                    await m.AcknowledgeAsync().ConfigureAwait(false);
                });

                using (var sender = new HttpClientSender("foo", new Uri("http://localhost:5006/API/v2/extra")))
                {
                    var exception = await Assert.ThrowsAsync <HttpRequestException>(() => sender.SendAsync("Hello, world!")).ConfigureAwait(false);

                    Assert.Contains("404 (Not Found)", exception.Message, StringComparison.InvariantCultureIgnoreCase);
                }

                Assert.Null(payload);
            }
        }