File: rack_sendfile_spec.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (50 lines) | stat: -rw-r--r-- 1,100 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

require 'spec_helper'

describe Rack::Sendfile do
  subject do
    content_object = file_object
    app = Class.new(Grape::API) do
      use Rack::Sendfile
      format :json
      get do
        if content_object.is_a?(String)
          sendfile content_object
        else
          stream content_object
        end
      end
    end

    options = {
      method: 'GET',
      'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect',
      'HTTP_X_ACCEL_MAPPING' => '/accel/mapping/=/replaced/'
    }
    env = Rack::MockRequest.env_for('/', options)
    app.call(env)
  end

  context 'when calling sendfile' do
    let(:file_object) do
      '/accel/mapping/some/path'
    end

    it 'contains Sendfile headers' do
      headers = subject[1]
      expect(headers).to include('X-Accel-Redirect')
    end
  end

  context 'when streaming non file content' do
    let(:file_object) do
      double(:file_object, each: nil)
    end

    it 'not contains Sendfile headers' do
      headers = subject[1]
      expect(headers).not_to include('X-Accel-Redirect')
    end
  end
end