File: test_mechanize_file_connection.rb

package info (click to toggle)
ruby-mechanize 2.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,416 kB
  • sloc: ruby: 11,645; makefile: 7; sh: 4
file content (40 lines) | stat: -rw-r--r-- 976 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
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeFileConnection < Mechanize::TestCase

  def test_request
    file_path = File.expand_path(__FILE__)
    uri = URI.parse "file://#{file_path}"
    conn = Mechanize::FileConnection.new

    body = +''

    conn.request uri, nil do |response|
      assert_equal(file_path, response.file_path)
      response.read_body do |part|
        body << part
      end
    end

    assert_equal File.read(__FILE__), body.gsub(/\r\n/, "\n")
  end

  def test_request_on_uri_with_windows_drive
    uri_string = "file://C:/path/to/file.html"
    expected_file_path = "C:/path/to/file.html"

    uri = URI.parse(uri_string)
    conn = Mechanize::FileConnection.new

    called = false
    yielded_file_path = nil
    conn.request(uri, nil) do |response|
      called = true
      yielded_file_path = response.file_path
    end

    assert(called)
    assert_equal(expected_file_path, yielded_file_path)
  end
end