File: rack_response_spec.rb

package info (click to toggle)
ruby-webmock 3.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,172 kB
  • sloc: ruby: 12,829; makefile: 6
file content (125 lines) | stat: -rw-r--r-- 4,333 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'spec_helper'

describe WebMock::RackResponse do
  before :each do
    @rack_response = WebMock::RackResponse.new(MyRackApp)
  end

  it "should hook up to a rack appliance" do
    request = WebMock::RequestSignature.new(:get, 'www.example.com')
    response = @rack_response.evaluate(request)

    expect(response.status.first).to eq(200)
    expect(response.body).to include('This is my root!')
  end

  it "should set the reason phrase based on the status code" do
    request = WebMock::RequestSignature.new(:get, 'www.example.com')
    response = @rack_response.evaluate(request)
    expect(response.status).to eq([200, "OK"])

    request = WebMock::RequestSignature.new(:get, 'www.example.com/error')
    response = @rack_response.evaluate(request)
    expect(response.status).to eq([500, "Internal Server Error"])
  end

  it "should behave correctly when the rack response is not a simple array of strings" do
    request = WebMock::RequestSignature.new(:get, 'www.example.com/non_array_response')
    response = @rack_response.evaluate(request)

    expect(response.status.first).to eq(200)
    expect(response.body).to include('This is not in an array!')
  end

  it "should shouldn't blow up when hitting a locked resource twice" do
    @locked_rack_response = WebMock::RackResponse.new(MyLockedRackApp)
    request = WebMock::RequestSignature.new(:get, 'www.example.com/locked')
    @locked_rack_response.evaluate(request)
    response2 = @locked_rack_response.evaluate(request)

    expect(response2.body).to include('Single threaded response.')
    expect(response2.status.first).to eq(200)
  end

  it "should send along params" do
    request = WebMock::RequestSignature.new(:get, 'www.example.com/greet?name=Johnny')

    response = @rack_response.evaluate(request)

    expect(response.status.first).to eq(200)
    expect(response.body).to include('Hello, Johnny')
  end

  it "should send along POST params" do
    request = WebMock::RequestSignature.new(:post, 'www.example.com/greet',
      body: 'name=Jimmy'
    )

    response = @rack_response.evaluate(request)
    expect(response.body).to include('Good to meet you, Jimmy!')
  end

  it "should send params with proper content length if params have non-ascii symbols" do
    request = WebMock::RequestSignature.new(:post, 'www.example.com/greet',
      body: 'name=Олег'
    )

    response = @rack_response.evaluate(request)
    expect(response.body).to include('Good to meet you, Олег!')
  end

  it "should send or not a rack.version depending on the Rack version" do
    request = WebMock::RequestSignature.new(:get, 'www.example.com/env')
    response = @rack_response.evaluate(request)

    expect(response.status.first).to eq(200)
    body = JSON.parse(response.body)

    if Gem.loaded_specs["rack"].version > Gem::Version.new("3.0.0")
      expect(body).not_to include("rack.version")
    else
      expect(body).to include("rack.version")
    end
  end

  describe 'rack error output' do
    before :each do
      @original_stderr = $stderr
      $stderr = StringIO.new
    end

    after :each do
      $stderr = @original_stderr
    end

    it 'should behave correctly when an app uses rack.errors' do
      request = WebMock::RequestSignature.new(:get, 'www.example.com/error')

      expect { @rack_response.evaluate(request) }.to_not raise_error
      expect($stderr.length).to_not eq 0
    end
  end

  describe 'basic auth request' do
    before :each do
      @rack_response_with_basic_auth = WebMock::RackResponse.new(
        Rack::Auth::Basic.new(MyRackApp) do |username, password|
          username == 'username' && password == 'password'
        end
      )
    end
    it 'should be failure when wrong credentials' do
      request = WebMock::RequestSignature.new(:get, 'foo:bar@www.example.com')
      response = @rack_response_with_basic_auth.evaluate(request)
      expect(response.status.first).to eq(401)
      expect(response.body).not_to include('This is my root!')
    end

    it 'should be success when valid credentials' do
      request = WebMock::RequestSignature.new(:get, 'username:password@www.example.com')
      response = @rack_response_with_basic_auth.evaluate(request)
      expect(response.status.first).to eq(200)
      expect(response.body).to include('This is my root!')
    end
  end
end