File: net_http.feature

package info (click to toggle)
ruby-vcr 6.0.0%2Breally5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: ruby: 8,456; sh: 177; makefile: 7
file content (174 lines) | stat: -rw-r--r-- 5,229 bytes parent folder | download | duplicates (3)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
Feature: Net::HTTP

  There are many ways to use Net::HTTP.  The scenarios below provide regression
  tests for some Net::HTTP APIs that have not worked properly with VCR and
  WebMock in the past (but have since been fixed).

  Background:
    Given a file named "vcr_setup.rb" with:
      """ruby
      require 'ostruct'

      if ARGV[0] == '--with-server'
        $server = start_sinatra_app do
          get('/')  { 'VCR works with Net::HTTP gets!' }
          post('/') { 'VCR works with Net::HTTP posts!' }
        end
      else
        $server = OpenStruct(:port => 0)
      end

      require 'vcr'

      VCR.configure do |c|
        c.default_cassette_options = {
          :match_requests_on => [:method, :host, :path]
        }
      end
      """

  Scenario Outline: Calling #post on new Net::HTTP instance
    Given a file named "vcr_net_http.rb" with:
      """ruby
      require 'vcr_setup.rb'

      VCR.configure do |c|
        c.hook_into <hook_into>
        c.cassette_library_dir = 'cassettes'
      end

      VCR.use_cassette('net_http') do
        puts Net::HTTP.new('localhost', $server.port).post('/', '').body
      end
      """
    When I run `ruby vcr_net_http.rb --with-server`
    Then the output should contain "VCR works with Net::HTTP posts!"
    And the file "cassettes/net_http.yml" should contain "VCR works with Net::HTTP posts!"

    When I run `ruby vcr_net_http.rb`
    Then the output should contain "VCR works with Net::HTTP posts!"

    Examples:
      | hook_into |
      | :webmock  |

  Scenario Outline: Return from yielded block
    Given a file named "vcr_net_http.rb" with:
      """ruby
      require 'vcr_setup.rb'

      VCR.configure do |c|
        c.hook_into <hook_into>
        c.cassette_library_dir = 'cassettes'
      end

      def perform_request
        Net::HTTP.new('localhost', $server.port).request(Net::HTTP::Get.new('/', {})) do |response|
          return response
        end
      end

      VCR.use_cassette('net_http') do
        puts perform_request.body
      end
      """
    When I run `ruby vcr_net_http.rb --with-server`
    Then the output should contain "VCR works with Net::HTTP gets!"
    And the file "cassettes/net_http.yml" should contain "VCR works with Net::HTTP gets!"

    When I run `ruby vcr_net_http.rb`
    Then the output should contain "VCR works with Net::HTTP gets!"

    Examples:
      | hook_into |
      | :webmock  |

  Scenario Outline: Use Net::ReadAdapter to read body in fragments
    Given a file named "vcr_net_http.rb" with:
      """ruby
      require 'vcr_setup.rb'

      VCR.configure do |c|
        c.hook_into <hook_into>
        c.cassette_library_dir = 'cassettes'
      end

      VCR.use_cassette('net_http') do
        body = ''

        Net::HTTP.new('localhost', $server.port).request_get('/') do |response|
          response.read_body { |frag| body << frag }
        end

        puts body
      end
      """
    When I run `ruby vcr_net_http.rb --with-server`
    Then the output should contain "VCR works with Net::HTTP gets!"
    And the file "cassettes/net_http.yml" should contain "VCR works with Net::HTTP gets!"

    When I run `ruby vcr_net_http.rb`
    Then the output should contain "VCR works with Net::HTTP gets!"

    Examples:
      | hook_into |
      | :webmock  |

  Scenario Outline: Use open-uri (which is built on top of Net::HTTP and uses a seldom-used Net::HTTP API)
    Given a file named "vcr_net_http.rb" with:
      """ruby
      require 'open-uri'
      require 'vcr_setup.rb'

      VCR.configure do |c|
        c.hook_into <hook_into>
        c.cassette_library_dir = 'cassettes'
      end

      VCR.use_cassette('net_http') do
        puts open("http://localhost:#{$server.port}/").read
      end
      """
    When I run `ruby vcr_net_http.rb --with-server`
    Then the output should contain "VCR works with Net::HTTP gets!"
    And the file "cassettes/net_http.yml" should contain "VCR works with Net::HTTP gets!"

    When I run `ruby vcr_net_http.rb`
    Then the output should contain "VCR works with Net::HTTP gets!"

    Examples:
      | hook_into |
      | :webmock  |

    Scenario Outline: Make an HTTPS request
      Given a file named "vcr_https.rb" with:
        """ruby
        require 'vcr'

        VCR.configure do |c|
          c.hook_into <hook_into>
          c.cassette_library_dir = 'cassettes'
        end

        uri = URI("https://gist.githubusercontent.com/myronmarston/fb555cb593f3349d53af/raw/6921dd638337d3f6a51b0e02e7f30e3c414f70d6/vcr_gist")

        VCR.use_cassette('https') do
          http = Net::HTTP.new(uri.host, uri.port)
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
          response = http.request_get(uri.path)

          puts response.body
        end
        """
      When I run `ruby vcr_https.rb`
      Then the output should contain "VCR gist"
      And the file "cassettes/https.yml" should contain "VCR gist"

      When I modify the file "cassettes/https.yml" to replace "VCR gist" with "HTTPS replaying works"
      And I run `ruby vcr_https.rb`
      Then the output should contain "HTTPS replaying works"

      Examples:
        | hook_into |
        | :webmock  |