1
0

request_spec.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require 'securerandom'
  4. RSpec.describe Request do
  5. subject { described_class.new(:get, 'http://example.com') }
  6. describe '#headers' do
  7. it 'returns user agent' do
  8. expect(subject.headers['User-Agent']).to be_present
  9. end
  10. it 'returns the date header' do
  11. expect(subject.headers['Date']).to be_present
  12. end
  13. it 'returns the host header' do
  14. expect(subject.headers['Host']).to be_present
  15. end
  16. it 'does not return virtual request-target header' do
  17. expect(subject.headers['(request-target)']).to be_nil
  18. end
  19. end
  20. describe '#on_behalf_of' do
  21. it 'when used, adds signature header' do
  22. subject.on_behalf_of(Fabricate(:account))
  23. expect(subject.headers['Signature']).to be_present
  24. end
  25. end
  26. describe '#add_headers' do
  27. it 'adds headers to the request' do
  28. subject.add_headers('Test' => 'Foo')
  29. expect(subject.headers['Test']).to eq 'Foo'
  30. end
  31. end
  32. describe '#perform' do
  33. context 'with valid host' do
  34. before { stub_request(:get, 'http://example.com') }
  35. it 'executes a HTTP request' do
  36. expect { |block| subject.perform(&block) }.to yield_control
  37. expect(a_request(:get, 'http://example.com')).to have_been_made.once
  38. end
  39. it 'executes a HTTP request when the first address is private' do
  40. resolver = instance_double(Resolv::DNS)
  41. allow(resolver).to receive(:getaddresses).with('example.com').and_return(%w(0.0.0.0 2001:4860:4860::8844))
  42. allow(resolver).to receive(:timeouts=).and_return(nil)
  43. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  44. expect { |block| subject.perform(&block) }.to yield_control
  45. expect(a_request(:get, 'http://example.com')).to have_been_made.once
  46. end
  47. it 'sets headers' do
  48. expect { |block| subject.perform(&block) }.to yield_control
  49. expect(a_request(:get, 'http://example.com').with(headers: subject.headers)).to have_been_made
  50. end
  51. it 'closes underlying connection' do
  52. allow(subject.send(:http_client)).to receive(:close)
  53. expect { |block| subject.perform(&block) }.to yield_control
  54. expect(subject.send(:http_client)).to have_received(:close)
  55. end
  56. it 'returns response which implements body_with_limit' do
  57. subject.perform do |response|
  58. expect(response).to respond_to :body_with_limit
  59. end
  60. end
  61. end
  62. context 'with private host' do
  63. around do |example|
  64. WebMock.disable!
  65. example.run
  66. WebMock.enable!
  67. end
  68. it 'raises Mastodon::ValidationError' do
  69. resolver = instance_double(Resolv::DNS)
  70. allow(resolver).to receive(:getaddresses).with('example.com').and_return(%w(0.0.0.0 2001:db8::face))
  71. allow(resolver).to receive(:timeouts=).and_return(nil)
  72. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  73. expect { subject.perform }.to raise_error Mastodon::ValidationError
  74. end
  75. end
  76. end
  77. describe "response's body_with_limit method" do
  78. it 'rejects body more than 1 megabyte by default' do
  79. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes))
  80. expect { subject.perform(&:body_with_limit) }.to raise_error(Mastodon::LengthValidationError, 'Body size exceeds limit of 1048576')
  81. end
  82. it 'accepts body less than 1 megabyte by default' do
  83. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.kilobytes))
  84. expect { subject.perform(&:body_with_limit) }.to_not raise_error
  85. end
  86. it 'rejects body by given size' do
  87. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.kilobytes))
  88. expect { subject.perform { |response| response.body_with_limit(1.kilobyte) } }.to raise_error(Mastodon::LengthValidationError, 'Body size exceeds limit of 1024')
  89. end
  90. it 'rejects too large chunked body' do
  91. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Transfer-Encoding' => 'chunked' })
  92. expect { subject.perform(&:body_with_limit) }.to raise_error(Mastodon::LengthValidationError, 'Body size exceeds limit of 1048576')
  93. end
  94. it 'rejects too large monolithic body' do
  95. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Content-Length' => 2.megabytes })
  96. expect { subject.perform(&:body_with_limit) }.to raise_error(Mastodon::LengthValidationError, 'Content-Length 2097152 exceeds limit of 1048576')
  97. end
  98. it 'truncates large monolithic body' do
  99. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Content-Length' => 2.megabytes })
  100. expect(subject.perform { |response| response.truncated_body.bytesize }).to be < 2.megabytes
  101. end
  102. it 'uses binary encoding if Content-Type does not tell encoding' do
  103. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html' })
  104. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::BINARY
  105. end
  106. it 'uses binary encoding if Content-Type tells unknown encoding' do
  107. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html; charset=unknown' })
  108. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::BINARY
  109. end
  110. it 'uses encoding specified by Content-Type' do
  111. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html; charset=UTF-8' })
  112. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::UTF_8
  113. end
  114. end
  115. end