Beispiel #1
0
        public override Task <bool> Send(NotifyMessage message)
        {
            var             httpSender      = new HttpClientSender();
            HttpSendContent httpSendContent = null;
            var             httpContent     = (HttpDictionaryContent)message.Content;

            switch (SendType)
            {
            case SendType.FormUrlEncoding:
                httpSendContent = MakeRawContent(httpContent);
                break;

            case SendType.Raw:
                httpSendContent = MakeRawContent(httpContent);
                break;
            }

            return(Task.Run(() =>
            {
                var result = httpSender.Send(httpSendContent, this);
                httpContent.ResponseCode = httpSendContent.ResponseCode;
                httpContent.ResponseContent = httpSendContent.ResponseContent;
                return result;
            }));
        }
Beispiel #2
0
        private void InitHttp(IContainer container)
        {
            var factory = container.Resolve <ClientSenderFactory>();
            var logger  = container.Resolve <ILogger>();

            logger.Info($"[config]use http for transfer");

            factory.ClientSenderCreator += (global::Jimu.JimuAddress address, ref IClientSender client) =>
            {
                //if (client == null && address.GetType() == typeof(HttpAddress))
                if (client == null && address.Protocol == "Http")
                {
                    var listener = new ClientListener();
                    //var sender = new HttpClientSender(address, listener);
                    client = new HttpClientSender(listener, logger, address);
                }
            };
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }
Beispiel #11
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);
            }
        }
Beispiel #12
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);
            }
        }
Beispiel #13
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);
            }
        }
        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);
            }
        }
Beispiel #15
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);
            }
        }
        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);
            }
        }
        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);
            }
        }
        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);
            }
        }