2018-02-06 09:18:32 -05:00
|
|
|
module HttpIOHelpers
|
2018-07-09 08:18:31 -04:00
|
|
|
def stub_remote_url_206(url, file_path)
|
|
|
|
WebMock.stub_request(:get, url)
|
|
|
|
.to_return { |request| remote_url_response(file_path, request, 206) }
|
2018-02-06 09:18:32 -05:00
|
|
|
end
|
|
|
|
|
2018-07-09 08:18:31 -04:00
|
|
|
def stub_remote_url_200(url, file_path)
|
|
|
|
WebMock.stub_request(:get, url)
|
|
|
|
.to_return { |request| remote_url_response(file_path, request, 200) }
|
2018-02-06 09:18:32 -05:00
|
|
|
end
|
|
|
|
|
2018-07-09 08:18:31 -04:00
|
|
|
def stub_remote_url_500(url)
|
|
|
|
WebMock.stub_request(:get, url)
|
2018-02-06 09:18:32 -05:00
|
|
|
.to_return(status: [500, "Internal Server Error"])
|
|
|
|
end
|
|
|
|
|
2018-07-09 08:18:31 -04:00
|
|
|
def remote_url_response(file_path, request, response_status)
|
2018-02-06 09:18:32 -05:00
|
|
|
range = request.headers['Range'].match(/bytes=(\d+)-(\d+)/)
|
|
|
|
|
2018-07-09 08:18:31 -04:00
|
|
|
body = File.read(file_path).force_encoding(Encoding::BINARY)
|
|
|
|
size = body.bytesize
|
|
|
|
|
2018-02-06 09:18:32 -05:00
|
|
|
{
|
2018-07-09 08:18:31 -04:00
|
|
|
status: response_status,
|
|
|
|
headers: remote_url_response_headers(response_status, range[1].to_i, range[2].to_i, size),
|
|
|
|
body: body[range[1].to_i..range[2].to_i]
|
2018-02-06 09:18:32 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-07-09 08:18:31 -04:00
|
|
|
def remote_url_response_headers(response_status, from, to, size)
|
|
|
|
{ 'Content-Type' => 'text/plain' }.tap do |headers|
|
|
|
|
if response_status == 206
|
|
|
|
headers.merge('Content-Range' => "bytes #{from}-#{to}/#{size}")
|
|
|
|
end
|
2018-02-06 09:18:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_smaller_buffer_size_than(file_size)
|
|
|
|
blocks = (file_size / 128)
|
|
|
|
new_size = (blocks / 2) * 128
|
2018-07-09 07:34:18 -04:00
|
|
|
stub_const("Gitlab::HttpIO::BUFFER_SIZE", new_size)
|
2018-02-06 09:18:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_larger_buffer_size_than(file_size)
|
|
|
|
blocks = (file_size / 128)
|
|
|
|
new_size = (blocks * 2) * 128
|
2018-07-09 07:34:18 -04:00
|
|
|
stub_const("Gitlab::HttpIO::BUFFER_SIZE", new_size)
|
2018-02-06 09:18:32 -05:00
|
|
|
end
|
|
|
|
end
|