File: test_response_headers.rb

package info (click to toggle)
ruby-fakeweb 1.3.0%2Bgit20170806%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 436 kB
  • sloc: ruby: 2,057; sh: 24; makefile: 3
file content (79 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download | duplicates (6)
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
require 'test_helper'

class TestResponseHeaders < Test::Unit::TestCase
  def test_content_type_when_registering_with_string_and_content_type_header_as_symbol_option
    FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', :content_type => "application/json")
    response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
    assert_equal '[{"username": "chrisk"}]', response.body
    assert_equal "application/json", response['Content-Type']
  end

  def test_content_type_when_registering_with_string_and_content_type_header_as_string_option
    FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', 'Content-Type' => "application/json")
    response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
    assert_equal "application/json", response['Content-Type']
  end

  def test_content_type_when_registering_with_string_only
    FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]')
    response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
    assert_equal '[{"username": "chrisk"}]', response.body
    assert_nil response['Content-Type']
  end

  def test_cookies_when_registering_with_file_and_set_cookie_header
    FakeWeb.register_uri(:get, "http://example.com/", :body => fixture_path("test_example.txt"),
                                                      :set_cookie => "user_id=1; example=yes")
    response = Net::HTTP.start("example.com") { |query| query.get("/") }
    assert_equal "test example content", response.body
    assert_equal "user_id=1; example=yes", response['Set-Cookie']
  end

  def test_multiple_set_cookie_headers
    FakeWeb.register_uri(:get, "http://example.com", :set_cookie => ["user_id=1", "example=yes"])
    response = Net::HTTP.start("example.com") { |query| query.get("/") }
    assert_equal ["user_id=1", "example=yes"], response.get_fields('Set-Cookie')
    assert_equal "user_id=1, example=yes", response['Set-Cookie']
  end

  def test_registering_with_baked_response_ignores_header_options
    fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
    fake_response["Server"] = "Apache/1.3.27 (Unix)"
    FakeWeb.register_uri(:get, "http://example.com/", :response => fake_response,
                                                      :server => "FakeWeb/1.2.3 (Ruby)")
    response = Net::HTTP.start("example.com") { |query| query.get("/") }
    assert_equal "200", response.code
    assert_equal "OK", response.message
    assert_equal "Apache/1.3.27 (Unix)", response["Server"]
  end

  def test_headers_are_rotated_when_registering_with_response_rotation
    FakeWeb.register_uri(:get, "http://example.com",
                               [{:body => 'test1', :expires => "Thu, 14 Jun 2009 16:00:00 GMT",
                                                   :content_type => "text/plain"},
                                {:body => 'test2', :expires => "Thu, 14 Jun 2009 16:00:01 GMT"}])

    first_response = second_response = nil
    Net::HTTP.start("example.com") do |query|
      first_response = query.get("/")
      second_response = query.get("/")
    end
    assert_equal 'test1', first_response.body
    assert_equal "Thu, 14 Jun 2009 16:00:00 GMT", first_response['Expires']
    assert_equal "text/plain", first_response['Content-Type']
    assert_equal 'test2', second_response.body
    assert_equal "Thu, 14 Jun 2009 16:00:01 GMT", second_response['Expires']
    assert_nil second_response['Content-Type']
  end

  def test_registering_with_status_option_and_response_headers
    FakeWeb.register_uri(:get, "http://example.com", :status => ["301", "Moved Permanently"],
                                                     :location => "http://www.example.com")

    response = Net::HTTP.start("example.com") { |query| query.get("/") }
    assert_equal "301", response.code
    assert_equal "Moved Permanently", response.message
    assert_equal "http://www.example.com", response["Location"]
  end

end