|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe Slack::Web::Client do |
| 6 | + subject(:client) { described_class.new(token: 'example-token') } |
| 7 | + |
| 8 | + before do |
| 9 | + stub_request(:post, 'https://slack.com/api/auth.test').to_return( |
| 10 | + body: '{"ok":true}', |
| 11 | + headers: { 'Content-Type' => 'application/json', 'X-OAuth-Scopes' => 'chat:write' } |
| 12 | + ) |
| 13 | + end |
| 14 | + |
| 15 | + it 'returns a configured copy without changing body returns' do |
| 16 | + responses = [] |
| 17 | + observed = client.with_response { |response| responses << response } |
| 18 | + result = observed.auth_test |
| 19 | + expect(result).to be_a(Slack::Messages::Message) |
| 20 | + expect(responses.first.body).to equal(result) |
| 21 | + expect(responses.first.headers['x-oauth-scopes']).to eq('chat:write') |
| 22 | + expect(observed.token).to eq(client.token) |
| 23 | + client.auth_test |
| 24 | + expect(responses.size).to eq(1) |
| 25 | + end |
| 26 | + |
| 27 | + it 'chains request and response callbacks in registration order' do |
| 28 | + calls = [] |
| 29 | + observed = client.with_request do |request| |
| 30 | + calls << :request |
| 31 | + request.headers['X-Custom'] = 'value' |
| 32 | + end |
| 33 | + observed = observed.with_response do |response| |
| 34 | + calls << response.status |
| 35 | + end |
| 36 | + observed = observed.with_response do |
| 37 | + calls << :second |
| 38 | + end |
| 39 | + observed.auth_test |
| 40 | + expect(calls).to eq([:request, 200, :second]) |
| 41 | + expect(a_request(:post, 'https://slack.com/api/auth.test') |
| 42 | + .with(headers: { 'X-Custom' => 'value', 'Authorization' => 'Bearer example-token' })).to have_been_made.once |
| 43 | + end |
| 44 | + |
| 45 | + it 'requires callback blocks' do |
| 46 | + expect { client.with_response }.to raise_error(ArgumentError, 'response callback block is required') |
| 47 | + expect { client.with_request }.to raise_error(ArgumentError, 'request callback block is required') |
| 48 | + end |
| 49 | + |
| 50 | + it 'observes every pagination request while preserving page blocks' do |
| 51 | + stub_request(:post, 'https://slack.com/api/users.list') |
| 52 | + .to_return(body: '{"ok":true,"members":[{"id":"U1"}],"response_metadata":{"next_cursor":"next"}}') |
| 53 | + .then.to_return(body: '{"ok":true,"members":[{"id":"U2"}],"response_metadata":{"next_cursor":""}}') |
| 54 | + requests = [] |
| 55 | + responses = [] |
| 56 | + pages = [] |
| 57 | + observed = client.with_request { |request| requests << request } |
| 58 | + observed = observed.with_response { |response| responses << response } |
| 59 | + observed.users_list { |page| pages << page } |
| 60 | + expect(requests.size).to eq(2) |
| 61 | + expect(responses.map(&:body)).to eq(pages) |
| 62 | + expect(pages.map { |page| page.members.first.id }).to eq(%w[U1 U2]) |
| 63 | + end |
| 64 | + |
| 65 | + it 'does not share cached connections' do |
| 66 | + original_connection = client.send(:connection) |
| 67 | + observed = client.with_response { |_response| nil } |
| 68 | + expect(observed.send(:connection)).not_to equal(original_connection) |
| 69 | + end |
| 70 | + |
| 71 | + it 'propagates response callback errors unchanged' do |
| 72 | + error = Faraday::ParsingError.new('callback error') |
| 73 | + observed = client.with_response { raise error } |
| 74 | + expect { observed.auth_test }.to(raise_error { |raised| expect(raised).to equal(error) }) |
| 75 | + end |
| 76 | + |
| 77 | + it 'does not dispatch an HTTP request when a request callback fails' do |
| 78 | + observed = client.with_request { raise ArgumentError, 'callback error' } |
| 79 | + expect { observed.auth_test }.to raise_error(ArgumentError, 'callback error') |
| 80 | + expect(a_request(:post, 'https://slack.com/api/auth.test')).not_to have_been_made |
| 81 | + end |
| 82 | + |
| 83 | + it 'does not translate parsing errors raised by request callbacks' do |
| 84 | + error = Faraday::ParsingError.new('callback error') |
| 85 | + observed = client.with_request { raise error } |
| 86 | + expect { observed.auth_test }.to(raise_error { |raised| expect(raised).to equal(error) }) |
| 87 | + end |
| 88 | + |
| 89 | + it 'does not call response callbacks on Slack errors' do |
| 90 | + stub_request(:post, 'https://slack.com/api/auth.test').to_return(body: '{"ok":false,"error":"not_authed"}') |
| 91 | + responses = [] |
| 92 | + observed = client.with_response { |response| responses << response } |
| 93 | + expect { observed.auth_test }.to raise_error(Slack::Web::Api::Errors::SlackError) |
| 94 | + expect(responses).to be_empty |
| 95 | + end |
| 96 | + |
| 97 | + it 'does not call response callbacks on transport errors' do |
| 98 | + stub_request(:post, 'https://slack.com/api/auth.test').to_timeout |
| 99 | + responses = [] |
| 100 | + observed = client.with_response { |response| responses << response } |
| 101 | + expect { observed.auth_test }.to raise_error(Slack::Web::Api::Errors::TimeoutError) |
| 102 | + expect(responses).to be_empty |
| 103 | + end |
| 104 | +end |
0 commit comments