request_spec.rb 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 bare domain URL' do
  77. let(:url) { 'http://example.com' }
  78. before do
  79. stub_request(:get, 'http://example.com')
  80. end
  81. it 'normalizes path' do
  82. subject.perform do |response|
  83. expect(response.request.uri.path).to eq '/'
  84. end
  85. end
  86. it 'normalizes path used for request signing' do
  87. subject.perform
  88. headers = subject.instance_variable_get(:@headers)
  89. expect(headers[Request::REQUEST_TARGET]).to eq 'get /'
  90. end
  91. it 'normalizes path used in request line' do
  92. subject.perform do |response|
  93. expect(response.request.headline).to eq 'GET / HTTP/1.1'
  94. end
  95. end
  96. end
  97. context 'with unnormalized URL' do
  98. let(:url) { 'HTTP://EXAMPLE.com:80/foo%41%3A?bar=%41%3A#baz' }
  99. before do
  100. stub_request(:get, 'http://example.com/foo%41%3A?bar=%41%3A')
  101. end
  102. it 'normalizes scheme' do
  103. subject.perform do |response|
  104. expect(response.request.uri.scheme).to eq 'http'
  105. end
  106. end
  107. it 'normalizes host' do
  108. subject.perform do |response|
  109. expect(response.request.uri.authority).to eq 'example.com'
  110. end
  111. end
  112. it 'does not modify path' do
  113. subject.perform do |response|
  114. expect(response.request.uri.path).to eq '/foo%41%3A'
  115. end
  116. end
  117. it 'does not modify query string' do
  118. subject.perform do |response|
  119. expect(response.request.uri.query).to eq 'bar=%41%3A'
  120. end
  121. end
  122. it 'does not modify path used for request signing' do
  123. subject.perform
  124. headers = subject.instance_variable_get(:@headers)
  125. expect(headers[Request::REQUEST_TARGET]).to eq 'get /foo%41%3A'
  126. end
  127. it 'does not modify path used in request line' do
  128. subject.perform do |response|
  129. expect(response.request.headline).to eq 'GET /foo%41%3A?bar=%41%3A HTTP/1.1'
  130. end
  131. end
  132. it 'strips fragment' do
  133. subject.perform do |response|
  134. expect(response.request.uri.fragment).to be_nil
  135. end
  136. end
  137. end
  138. context 'with non-ASCII URL' do
  139. let(:url) { 'http://éxample.com:81/föo?bär=1' }
  140. before do
  141. stub_request(:get, 'http://xn--xample-9ua.com:81/f%C3%B6o?b%C3%A4r=1')
  142. end
  143. it 'IDN-encodes host' do
  144. subject.perform do |response|
  145. expect(response.request.uri.authority).to eq 'xn--xample-9ua.com:81'
  146. end
  147. end
  148. it 'IDN-encodes host in Host header' do
  149. subject.perform do |response|
  150. expect(response.request.headers['Host']).to eq 'xn--xample-9ua.com'
  151. end
  152. end
  153. it 'percent-escapes path used for request signing' do
  154. subject.perform
  155. headers = subject.instance_variable_get(:@headers)
  156. expect(headers[Request::REQUEST_TARGET]).to eq 'get /f%C3%B6o'
  157. end
  158. it 'normalizes path used in request line' do
  159. subject.perform do |response|
  160. expect(response.request.headline).to eq 'GET /f%C3%B6o?b%C3%A4r=1 HTTP/1.1'
  161. end
  162. end
  163. end
  164. context 'with redirecting URL' do
  165. let(:url) { 'http://example.com/foo' }
  166. before do
  167. stub_request(:get, 'http://example.com/foo').to_return(status: 302, headers: { 'Location' => 'HTTPS://EXAMPLE.net/Bar' })
  168. stub_request(:get, 'https://example.net/Bar').to_return(body: 'Lorem ipsum')
  169. end
  170. it 'resolves redirect' do
  171. subject.perform do |response|
  172. expect(response.body.to_s).to eq 'Lorem ipsum'
  173. end
  174. expect(a_request(:get, 'https://example.net/Bar')).to have_been_made
  175. end
  176. it 'normalizes destination scheme' do
  177. subject.perform do |response|
  178. expect(response.request.uri.scheme).to eq 'https'
  179. end
  180. end
  181. it 'normalizes destination host' do
  182. subject.perform do |response|
  183. expect(response.request.uri.authority).to eq 'example.net'
  184. end
  185. end
  186. it 'does modify path' do
  187. subject.perform do |response|
  188. expect(response.request.uri.path).to eq '/Bar'
  189. end
  190. end
  191. end
  192. end
  193. describe "response's body_with_limit method" do
  194. it 'rejects body more than 1 megabyte by default' do
  195. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes))
  196. expect { subject.perform(&:body_with_limit) }.to raise_error Mastodon::LengthValidationError
  197. end
  198. it 'accepts body less than 1 megabyte by default' do
  199. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.kilobytes))
  200. expect { subject.perform(&:body_with_limit) }.to_not raise_error
  201. end
  202. it 'rejects body by given size' do
  203. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.kilobytes))
  204. expect { subject.perform { |response| response.body_with_limit(1.kilobyte) } }.to raise_error Mastodon::LengthValidationError
  205. end
  206. it 'rejects too large chunked body' do
  207. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Transfer-Encoding' => 'chunked' })
  208. expect { subject.perform(&:body_with_limit) }.to raise_error Mastodon::LengthValidationError
  209. end
  210. it 'rejects too large monolithic body' do
  211. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Content-Length' => 2.megabytes })
  212. expect { subject.perform(&:body_with_limit) }.to raise_error Mastodon::LengthValidationError
  213. end
  214. it 'truncates large monolithic body' do
  215. stub_request(:any, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes), headers: { 'Content-Length' => 2.megabytes })
  216. expect(subject.perform { |response| response.truncated_body.bytesize }).to be < 2.megabytes
  217. end
  218. it 'uses binary encoding if Content-Type does not tell encoding' do
  219. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html' })
  220. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::BINARY
  221. end
  222. it 'uses binary encoding if Content-Type tells unknown encoding' do
  223. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html; charset=unknown' })
  224. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::BINARY
  225. end
  226. it 'uses encoding specified by Content-Type' do
  227. stub_request(:any, 'http://example.com').to_return(body: '', headers: { 'Content-Type' => 'text/html; charset=UTF-8' })
  228. expect(subject.perform { |response| response.body_with_limit.encoding }).to eq Encoding::UTF_8
  229. end
  230. end
  231. end