File: streaming_response_checker.rb

package info (click to toggle)
ruby-faraday 2.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,008 kB
  • sloc: ruby: 6,509; sh: 10; makefile: 8
file content (35 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module Faraday
  module StreamingResponseChecker
    def check_streaming_response(streamed, options = {})
      opts = {
        prefix: '',
        streaming?: true
      }.merge(options)

      expected_response = opts[:prefix] + big_string

      chunks, sizes = streamed.transpose

      # Check that the total size of the chunks (via the last size returned)
      # is the same size as the expected_response
      expect(sizes.last).to eq(expected_response.bytesize)

      start_index = 0
      expected_chunks = []
      chunks.each do |actual_chunk|
        expected_chunk = expected_response[start_index..((start_index + actual_chunk.bytesize) - 1)]
        expected_chunks << expected_chunk
        start_index += expected_chunk.bytesize
      end

      # it's easier to read a smaller portion, so we check that first
      expect(expected_chunks[0][0..255]).to eq(chunks[0][0..255])

      [expected_chunks, chunks].transpose.each do |expected, actual|
        expect(actual).to eq(expected)
      end
    end
  end
end