1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
# frozen_string_literal: true
# encoding: utf-8
require "spec_helper"
RSpec.describe Slack::Messenger do
{
{ text: "hello" } =>
{ payload: { text: "hello" } },
{ text: "[hello](http://example.com/world)" } =>
{ payload: { text: "<http://example.com/world|hello>" } },
{ text: '<a href="http://example.com">example</a>' } =>
{ payload: { text: "<http://example.com|example>" } },
{ text: "hello/こんにちは from messenger test" } =>
{ payload: { text: "hello/こんにちは from messenger test" } },
{ text: "Hello World, enjoy [](http://example.com)." } =>
{ payload: { text: "Hello World, enjoy <http://example.com>." } },
{ text: "Hello World, enjoy [this](http://example.com)[this2](http://example2.com)" } =>
{ payload: { text: "Hello World, enjoy <http://example.com|this><http://example2.com|this2>" } },
{ text: "[John](mailto:john@example.com)" } =>
{ payload: { text: "<mailto:john@example.com|John>" } },
{ text: '<a href="mailto:john@example.com">John</a>' } =>
{ payload: { text: "<mailto:john@example.com|John>" } },
{ text: "hello", channel: "hodor" } =>
{ payload: { text: "hello", channel: "hodor" } },
{ text: nil, attachments: [{ text: "attachment message" }] } =>
{ payload: { text: nil, attachments: [{ text: "attachment message" }] } },
{ text: "the message", channel: "foo", attachments: [{ color: "#000",
text: "attachment message",
fallback: "fallback message" }] } =>
{ payload: { text: "the message",
channel: "foo",
attachments: [{ color: "#000",
text: "attachment message",
fallback: "fallback message" }] } },
{ attachments: [{ color: "#000",
text: "attachment message",
fallback: "fallback message" }] } =>
{ payload: { attachments: [{ color: "#000",
text: "attachment message",
fallback: "fallback message" }] } },
{ attachments: { color: "#000",
text: "attachment message [hodor](http://winterfell.com)",
fallback: "fallback message" } } =>
{ payload: { attachments: [{ color: "#000",
text: "attachment message <http://winterfell.com|hodor>",
fallback: "fallback message" }] } },
{ attachments: { color: "#000",
text: nil,
fallback: "fallback message" } } =>
{ payload: { attachments: [{ color: "#000",
text: nil,
fallback: "fallback message" }] } },
{ text: "hello", http_options: { timeout: 5 } } =>
{ http_options: { timeout: 5 }, payload: { text: "hello" } }
}.each do |args, payload|
it "sends correct payload for #post(#{args})" do
http_client = class_double("Slack::Messenger::Util::HTTPClient", post: nil)
messenger = Slack::Messenger.new "http://example.com", http_client: http_client
payload[:payload] = payload[:payload].to_json
expect(http_client).to receive(:post)
.with(URI.parse("http://example.com"), payload)
messenger.post(args)
end
end
it "applies options given to middleware" do
client = class_double("Slack::Messenger::Util::HTTPClient", post: nil)
messenger = Slack::Messenger.new "http://example.com" do
http_client client
middleware format_message: { formats: [] }
end
expect(client).to receive(:post)
.with(URI.parse("http://example.com"), { payload: { text: "Hello [world](http://example.com)!" }.to_json })
messenger.post text: "Hello [world](http://example.com)!"
end
end
|