request_spec.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require 'securerandom'
  4. describe Request do
  5. subject { described_class.new(:get, url) }
  6. let(:url) { 'http://example.com' }
  7. describe '#headers' do
  8. it 'returns user agent' do
  9. expect(subject.headers['User-Agent']).to be_present
  10. end
  11. it 'returns the date header' do
  12. expect(subject.headers['Date']).to be_present
  13. end
  14. it 'returns the host header' do
  15. expect(subject.headers['Host']).to be_present
  16. end
  17. it 'does not return virtual request-target header' do
  18. expect(subject.headers['(request-target)']).to be_nil
  19. end
  20. end
  21. describe '#on_behalf_of' do
  22. it 'when used, adds signature header' do
  23. subject.on_behalf_of(Fabricate(:account))
  24. expect(subject.headers['Signature']).to be_present
  25. end
  26. end
  27. describe '#add_headers' do
  28. it 'adds headers to the request' do
  29. subject.add_headers('Test' => 'Foo')
  30. expect(subject.headers['Test']).to eq 'Foo'
  31. end
  32. end
  33. describe '#perform' do
  34. context 'with valid host' do
  35. before { stub_request(:get, 'http://example.com') }
  36. it 'executes a HTTP request' do
  37. expect { |block| subject.perform(&block) }.to yield_control
  38. expect(a_request(:get, 'http://example.com')).to have_been_made.once
  39. end
  40. it 'executes a HTTP request when the first address is private' do
  41. resolver = instance_double(Resolv::DNS)
  42. allow(resolver).to receive(:getaddresses).with('example.com').and_return(%w(0.0.0.0 2001:4860:4860::8844))
  43. allow(resolver).to receive(:timeouts=).and_return(nil)
  44. allow(Resolv::DNS).to receive(:open).and_yield(resolver)
  45. expect { |block| subject.perform(&block) }.to yield_control
  46. expect(a_request(:get, 'http://example.com')).to have_been_made.once
  47. end
  48. it 'sets headers' do
  49. expect { |block| subject.perform(&block) }.to yield_control
  50. expect(a_request(:get, 'http://example.com').with(headers: subject.headers)).to have_been_made
  51. end
  52. it 'closes underlying connection' do
  53. expect_any_instance_of(HTTP::Client).to receive(:close)
  54. expect { |block| subject.perform(&block) }.to yield_control
  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. context 'with unnormalized URL' do
  77. let(:url) { 'HTTP://EXAMPLE.com:80/foo%41%3A?bar=%41%3A#baz' }
  78. before do
  79. stub_request(:get, 'http://example.com/foo%41%3A?bar=%41%3A')
  80. end
  81. it 'normalizes scheme' do
  82. subject.perform do |response|
  83. expect(response.request.uri.scheme).to eq 'http'
  84. end
  85. end
  86. it 'normalizes host' do
  87. subject.perform do |response|
  88. expect(response.request.uri.authority).to eq 'example.com'
  89. end
  90. end
  91. it 'does modify path' do
  92. subject.perform do |response|
  93. expect(response.request.uri.path).to eq '/foo%41%3A'
  94. end
  95. end
  96. it 'does modify query string' do
  97. subject.perform do |response|
  98. expect(response.request.uri.query).to eq 'bar=%41%3A'
  99. end
  100. end
  101. it 'strips fragment' do
  102. subject.perform do |response|
  103. expect(response.request.uri.fragment).to be_nil
  104. end
  105. end
  106. end
  107. context 'with non-ASCII URL' do
  108. let(:url) { 'http://éxample.com/föo?bär=1' }
  109. before do
  110. stub_request(:get, 'http://xn--xample-9ua.com/f%C3%B6o?b%C3%A4r=1')
  111. end
  112. it 'IDN-encodes host' do
  113. subject.perform do |response|
  114. expect(response.request.uri.authority).to eq 'xn--xample-9ua.com'
  115. end
  116. end
  117. it 'percent-escapes path and query string' do
  118. subject.perform
  119. expect(a_request(:get, 'http://xn--xample-9ua.com/f%C3%B6o?b%C3%A4r=1')).to have_been_made
  120. end
  121. end
  122. context 'with redirecting URL' do
  123. let(:url) { 'http://example.com/foo' }
  124. before do
  125. stub_request(:get, 'http://example.com/foo').to_return(status: 302, headers: { 'Location' => 'HTTPS://EXAMPLE.net/Bar' })
  126. stub_request(:get, 'https://example.net/Bar').to_return(body: 'Lorem ipsum')
  127. end
  128. it 'resolves redirect' do
  129. subject.perform do |response|
  130. expect(response.body.to_s).to eq 'Lorem ipsum'
  131. end
  132. expect(a_request(:get, 'https://example.net/Bar')).to have_been_made
  133. end
  134. it 'normalizes destination scheme' do
  135. subject.perform do |response|
  136. expect(response.request.uri.scheme).to eq 'https'
  137. end
  138. end
  139. it 'normalizes destination host' do
  140. subject.perform do |response|
  141. expect(response.request.uri.authority).to eq 'example.net'
  142. end
  143. end
  144. it 'does modify path' do
  145. subject.perform do |response|
  146. expect(response.request.uri.path).to eq '/Bar'
  147. end
  148. end
  149. end
  150. end
  151. describe "response's body_with_limit method" do
  152. it 'rejects body more than 1 megabyte by default' do
  153. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes))
  154. expect { subject.perform(&:body_with_limit) }.to raise_error Mastodon::LengthValidationError
  155. end
  156. it 'accepts body less than 1 megabyte by default' do
  157. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.kilobytes))
  158. expect { subject.perform(&:body_with_limit) }.to_not raise_error
  159. end
  160. it 'rejects body by given size' do
  161. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.kilobytes))
  162. expect { subject.perform { |response| response.body_with_limit(1.kilobyte) } }.to raise_error Mastodon::LengthValidationError
  163. end
  164. it 'rejects too large chunked body' do
  165. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Transfer-Encoding' => 'chunked' })
  166. expect { subject.perform(&:body_with_limit) }.to raise_error Mastodon::LengthValidationError
  167. end
  168. it 'rejects too large monolithic body' do
  169. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Content-Length' => 2.megabytes })
  170. expect { subject.perform(&:body_with_limit) }.to raise_error Mastodon::LengthValidationError
  171. end
  172. it 'truncates large monolithic body' do
  173. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Content-Length' => 2.megabytes })
  174. expect(subject.perform { |response| response.truncated_body.bytesize }).to be < 2.megabytes
  175. end
  176. it 'uses binary encoding if Content-Type does not tell encoding' do
  177. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html' })
  178. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::BINARY
  179. end
  180. it 'uses binary encoding if Content-Type tells unknown encoding' do
  181. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html; charset=unknown' })
  182. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::BINARY
  183. end
  184. it 'uses encoding specified by Content-Type' do
  185. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html; charset=UTF-8' })
  186. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::UTF_8
  187. end
  188. end
  189. end