summaryrefslogtreecommitdiff
path: root/spec/ruby/library/cgi/shared/http_header.rb
blob: ed65b20abd619bc229b80671b5315937cf3db63e (plain)
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
require File.expand_path('../../../../spec_helper', __FILE__)
require 'cgi'

describe :cgi_http_header, shared: true do
  describe "CGI#http_header when passed no arguments" do
    before :each do
      ENV['REQUEST_METHOD'], @old_request_method = "GET", ENV['REQUEST_METHOD']
      @cgi = CGI.new
    end

    after :each do
      ENV['REQUEST_METHOD'] = @old_request_method
    end


    it "returns a HTTP header specifiying the Content-Type as text/html" do
      @cgi.send(@method).should == "Content-Type: text/html\r\n\r\n"
    end

    it "includes Cookies in the @output_cookies field" do
      @cgi.instance_variable_set(:@output_cookies, ["multiple", "cookies"])
      @cgi.send(@method).should == "Content-Type: text/html\r\nSet-Cookie: multiple\r\nSet-Cookie: cookies\r\n\r\n"
    end
  end

  describe "CGI#http_header when passed String" do
    before :each do
      ENV['REQUEST_METHOD'], @old_request_method = "GET", ENV['REQUEST_METHOD']
      @cgi = CGI.new
    end

    after :each do
      ENV['REQUEST_METHOD'] = @old_request_method
    end


    it "returns a HTTP header specifiying the Content-Type as the passed String's content" do
      @cgi.send(@method, "text/plain").should == "Content-Type: text/plain\r\n\r\n"
    end

    it "includes Cookies in the @output_cookies field" do
      @cgi.instance_variable_set(:@output_cookies, ["multiple", "cookies"])
      @cgi.send(@method, "text/plain").should == "Content-Type: text/plain\r\nSet-Cookie: multiple\r\nSet-Cookie: cookies\r\n\r\n"
    end
  end

  describe "CGI#http_header when passed Hash" do
    before :each do
      ENV['REQUEST_METHOD'], @old_request_method = "GET", ENV['REQUEST_METHOD']
      @cgi = CGI.new
    end

    after :each do
      ENV['REQUEST_METHOD'] = @old_request_method
    end


    it "returns a HTTP header based on the Hash's key/value pairs" do
      header = @cgi.send(@method, "type" => "text/plain")
      header.should == "Content-Type: text/plain\r\n\r\n"

      header = @cgi.send(@method, "type" => "text/plain", "charset" => "UTF-8")
      header.should == "Content-Type: text/plain; charset=UTF-8\r\n\r\n"

      header = @cgi.send(@method, "nph" => true)
      header.should include("HTTP/1.0 200 OK\r\n")
      header.should include("Date: ")
      header.should include("Server: ")
      header.should include("Connection: close\r\n")
      header.should include("Content-Type: text/html\r\n")

      header = @cgi.send(@method, "status" => "OK")
      header.should == "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n"

      header = @cgi.send(@method, "status" => "PARTIAL_CONTENT")
      header.should == "Status: 206 Partial Content\r\nContent-Type: text/html\r\n\r\n"

      header = @cgi.send(@method, "status" => "MULTIPLE_CHOICES")
      header.should == "Status: 300 Multiple Choices\r\nContent-Type: text/html\r\n\r\n"

      header = @cgi.send(@method, "server" => "Server Software used")
      header.should == "Server: Server Software used\r\nContent-Type: text/html\r\n\r\n"

      header = @cgi.send(@method, "connection" => "connection type")
      header.should == "Connection: connection type\r\nContent-Type: text/html\r\n\r\n"

      header = @cgi.send(@method, "length" => 103)
      header.should == "Content-Type: text/html\r\nContent-Length: 103\r\n\r\n"

      header = @cgi.send(@method, "language" => "ja")
      header.should == "Content-Type: text/html\r\nContent-Language: ja\r\n\r\n"

      header = @cgi.send(@method, "expires" => Time.at(0))
      header.should == "Content-Type: text/html\r\nExpires: Thu, 01 Jan 1970 00:00:00 GMT\r\n\r\n"

      header = @cgi.send(@method, "cookie" => "my cookie's content")
      header.should == "Content-Type: text/html\r\nSet-Cookie: my cookie's content\r\n\r\n"

      header = @cgi.send(@method, "cookie" => ["multiple", "cookies"])
      header.should == "Content-Type: text/html\r\nSet-Cookie: multiple\r\nSet-Cookie: cookies\r\n\r\n"
    end

    it "includes Cookies in the @output_cookies field" do
      @cgi.instance_variable_set(:@output_cookies, ["multiple", "cookies"])
      @cgi.send(@method, {}).should == "Content-Type: text/html\r\nSet-Cookie: multiple\r\nSet-Cookie: cookies\r\n\r\n"
    end

    it "returns a HTTP header specifiying the Content-Type as text/html when passed an empty Hash" do
      @cgi.send(@method, {}).should == "Content-Type: text/html\r\n\r\n"
    end
  end
end