From 583a9e6fb03f1b365356d63d5b33329520bc49bf Mon Sep 17 00:00:00 2001 From: Brandon Boone Date: Wed, 2 Sep 2020 11:33:19 -0400 Subject: [PATCH 1/2] Switch to Post from Get Fix for #83 --- .../Clients/Chat/FlurlChatClient.cs | 21 +++++++--------- .../Clients/Flurl/FlurlChatClientTests.cs | 25 +++++++++++++------ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/SlackConnector/Connections/Clients/Chat/FlurlChatClient.cs b/src/SlackConnector/Connections/Clients/Chat/FlurlChatClient.cs index 9dfa7c7..7d0f5d1 100644 --- a/src/SlackConnector/Connections/Clients/Chat/FlurlChatClient.cs +++ b/src/SlackConnector/Connections/Clients/Chat/FlurlChatClient.cs @@ -21,21 +21,18 @@ public FlurlChatClient(IResponseVerifier responseVerifier) public async Task PostMessage(string slackKey, string channel, string text, IList attachments) { - var request = ClientConstants + var response = await ClientConstants .SlackApiHost .AppendPathSegment(SEND_MESSAGE_PATH) - .SetQueryParam("token", slackKey) - .SetQueryParam("channel", channel) - .SetQueryParam("text", text) - .SetQueryParam("as_user", "true") - .SetQueryParam("link_names", "true"); - - if (attachments != null && attachments.Any()) - { - request.SetQueryParam("attachments", JsonConvert.SerializeObject(attachments)); - } + .PostJsonAsync(new { + token = slackKey, + channel = channel, + text = text, + as_user = true, + link_names = true, + attachments = attachments + }).ReceiveJson(); - var response = await request.GetJsonAsync(); _responseVerifier.VerifyResponse(response); } } diff --git a/tests/SlackConnector.Tests.Unit/Connections/Clients/Flurl/FlurlChatClientTests.cs b/tests/SlackConnector.Tests.Unit/Connections/Clients/Flurl/FlurlChatClientTests.cs index 13f879d..f479462 100644 --- a/tests/SlackConnector.Tests.Unit/Connections/Clients/Flurl/FlurlChatClientTests.cs +++ b/tests/SlackConnector.Tests.Unit/Connections/Clients/Flurl/FlurlChatClientTests.cs @@ -50,12 +50,15 @@ public async Task should_call_expected_url_with_given_slack_key() _responseVerifierMock.Verify(x => x.VerifyResponse(Looks.Like(expectedResponse))); _httpTest .ShouldHaveCalled(ClientConstants.SlackApiHost.AppendPathSegment(FlurlChatClient.SEND_MESSAGE_PATH)) - .WithQueryParamValue("token", slackKey) - .WithQueryParamValue("channel", channel) - .WithQueryParamValue("text", text) - .WithQueryParamValue("as_user", "true") - .WithQueryParamValue("link_names", "true") - .WithoutQueryParam("attachments") + .WithRequestJson(new + { + token = slackKey, + channel = channel, + text = text, + as_user = true, + link_names = true, + attachments = (object)null + }) .Times(1); } @@ -76,7 +79,15 @@ public async Task should_add_attachments_if_given() // then _httpTest .ShouldHaveCalled(ClientConstants.SlackApiHost.AppendPathSegment(FlurlChatClient.SEND_MESSAGE_PATH)) - .WithQueryParamValue("attachments", JsonConvert.SerializeObject(attachments)) + .WithRequestJson(new + { + token = (string)null, + channel = (string)null, + text = (string)null, + as_user = true, + link_names = true, + attachments = attachments + }) .Times(1); } } From 2c7b4671e8255d74dd8bab987749f325efd5ccaf Mon Sep 17 00:00:00 2001 From: Brandon Boone Date: Tue, 15 Sep 2020 10:05:43 -0400 Subject: [PATCH 2/2] Switched to OAuthBearerToken in Post https://api.slack.com/web#slack-web-api__basics__post-bodies__json-encoded-bodies --- .../Connections/Clients/Chat/FlurlChatClient.cs | 2 +- .../Connections/Clients/Flurl/FlurlChatClientTests.cs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/SlackConnector/Connections/Clients/Chat/FlurlChatClient.cs b/src/SlackConnector/Connections/Clients/Chat/FlurlChatClient.cs index 7d0f5d1..f248c5f 100644 --- a/src/SlackConnector/Connections/Clients/Chat/FlurlChatClient.cs +++ b/src/SlackConnector/Connections/Clients/Chat/FlurlChatClient.cs @@ -24,8 +24,8 @@ public async Task PostMessage(string slackKey, string channel, string text, ILis var response = await ClientConstants .SlackApiHost .AppendPathSegment(SEND_MESSAGE_PATH) + .WithOAuthBearerToken(slackKey) .PostJsonAsync(new { - token = slackKey, channel = channel, text = text, as_user = true, diff --git a/tests/SlackConnector.Tests.Unit/Connections/Clients/Flurl/FlurlChatClientTests.cs b/tests/SlackConnector.Tests.Unit/Connections/Clients/Flurl/FlurlChatClientTests.cs index f479462..5dc1288 100644 --- a/tests/SlackConnector.Tests.Unit/Connections/Clients/Flurl/FlurlChatClientTests.cs +++ b/tests/SlackConnector.Tests.Unit/Connections/Clients/Flurl/FlurlChatClientTests.cs @@ -50,9 +50,9 @@ public async Task should_call_expected_url_with_given_slack_key() _responseVerifierMock.Verify(x => x.VerifyResponse(Looks.Like(expectedResponse))); _httpTest .ShouldHaveCalled(ClientConstants.SlackApiHost.AppendPathSegment(FlurlChatClient.SEND_MESSAGE_PATH)) + .WithOAuthBearerToken(slackKey) .WithRequestJson(new { - token = slackKey, channel = channel, text = text, as_user = true, @@ -65,6 +65,7 @@ public async Task should_call_expected_url_with_given_slack_key() [Fact] public async Task should_add_attachments_if_given() { + const string slackKey = "something-that-looks-like-a-slack-key"; // given _httpTest.RespondWithJson(new StandardResponse()); var attachments = new List @@ -74,14 +75,14 @@ public async Task should_add_attachments_if_given() }; // when - await _chatClient.PostMessage(It.IsAny(), It.IsAny(), It.IsAny(), attachments); + await _chatClient.PostMessage(slackKey, It.IsAny(), It.IsAny(), attachments); // then _httpTest .ShouldHaveCalled(ClientConstants.SlackApiHost.AppendPathSegment(FlurlChatClient.SEND_MESSAGE_PATH)) + .WithOAuthBearerToken(slackKey) .WithRequestJson(new { - token = (string)null, channel = (string)null, text = (string)null, as_user = true,