File: multipart_spec.rb

package info (click to toggle)
ruby-rack-test 0.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 296 kB
  • sloc: ruby: 1,900; makefile: 5
file content (145 lines) | stat: -rw-r--r-- 4,957 bytes parent folder | download | duplicates (4)
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
# encoding: UTF-8

require "spec_helper"

describe Rack::Test::Session do

  def test_file_path
    File.dirname(__FILE__) + "/../../fixtures/foo.txt"
  end

  def second_test_file_path
    File.dirname(__FILE__) + "/../../fixtures/bar.txt"
  end

  def uploaded_file
    Rack::Test::UploadedFile.new(test_file_path)
  end

  def second_uploaded_file
    Rack::Test::UploadedFile.new(second_test_file_path)
  end

  context "uploading a file" do
    it "sends the multipart/form-data content type" do
      post "/", "photo" => uploaded_file
      expect(last_request.env["CONTENT_TYPE"]).to include("multipart/form-data;")
    end

    it "sends regular params" do
      post "/", "photo" => uploaded_file, "foo" => "bar"
      expect(last_request.POST["foo"]).to eq("bar")
    end

    it "sends nested params" do
      post "/", "photo" => uploaded_file, "foo" => {"bar" => "baz"}
      expect(last_request.POST["foo"]["bar"]).to eq("baz")
    end

    it "sends multiple nested params" do
      post "/", "photo" => uploaded_file, "foo" => {"bar" => {"baz" => "bop"}}
      expect(last_request.POST["foo"]["bar"]["baz"]).to eq("bop")
    end

    it "sends params with arrays" do
      post "/", "photo" => uploaded_file, "foo" => ["1", "2"]
      expect(last_request.POST["foo"]).to eq(["1", "2"])
    end

    it "sends params with encoding sensitive values" do
      post "/", "photo" => uploaded_file, "foo" => "bar? baz"
      expect(last_request.POST["foo"]).to eq("bar? baz")
    end

    it "sends params encoded as ISO-8859-1" do
      post "/", "photo" => uploaded_file, "foo" => "bar", "utf8" => "☃"
      expect(last_request.POST["foo"]).to eq("bar")

      if Rack::Test.encoding_aware_strings?
        expect(last_request.POST["utf8"]).to eq("☃")
      else
        expect(last_request.POST["utf8"]).to eq("\xE2\x98\x83")
      end
    end

    it "sends params with parens in names" do
      post "/", "photo" => uploaded_file, "foo(1i)" => "bar"
      expect(last_request.POST["foo(1i)"]).to eq("bar")
    end

    it "sends params with encoding sensitive names" do
      post "/", "photo" => uploaded_file, "foo bar" => "baz"
      expect(last_request.POST["foo bar"]).to eq("baz")
    end

    it "sends files with the filename" do
      post "/", "photo" => uploaded_file
      expect(last_request.POST["photo"][:filename]).to eq("foo.txt")
    end

    it "sends files with the text/plain MIME type by default" do
      post "/", "photo" => uploaded_file
      expect(last_request.POST["photo"][:type]).to eq("text/plain")
    end

    it "sends files with the right name" do
      post "/", "photo" => uploaded_file
      expect(last_request.POST["photo"][:name]).to eq("photo")
    end

    it "allows overriding the content type" do
      post "/", "photo" => Rack::Test::UploadedFile.new(test_file_path, "image/jpeg")
      expect(last_request.POST["photo"][:type]).to eq("image/jpeg")
    end

    it "sends files with a Content-Length in the header" do
      post "/", "photo" => uploaded_file
      expect(last_request.POST["photo"][:head]).to include("Content-Length: 4")
    end

    it "sends files as Tempfiles" do
      post "/", "photo" => uploaded_file
      expect(last_request.POST["photo"][:tempfile]).to be_a(::Tempfile)
    end
  end


  context "uploading two files" do
    it "sends the multipart/form-data content type" do
      post "/", "photos" => [uploaded_file, second_uploaded_file]
      expect(last_request.env["CONTENT_TYPE"]).to include("multipart/form-data;")
    end

    it "sends files with the filename" do
      post "/", "photos" => [uploaded_file, second_uploaded_file]
      expect(last_request.POST["photos"].collect{|photo| photo[:filename]}).to eq(["foo.txt", "bar.txt"])
    end

    it "sends files with the text/plain MIME type by default" do
      post "/", "photos" => [uploaded_file, second_uploaded_file]
      expect(last_request.POST["photos"].collect{|photo| photo[:type]}).to eq(["text/plain", "text/plain"])
    end

    it "sends files with the right names" do
      post "/", "photos" => [uploaded_file, second_uploaded_file]
      last_request.POST["photos"].all?{|photo| expect(photo[:name]).to eq("photos[]") }
    end

    it "allows mixed content types" do
      image_file = Rack::Test::UploadedFile.new(test_file_path, "image/jpeg")

      post "/", "photos" => [uploaded_file, image_file]
      expect(last_request.POST["photos"].collect{|photo| photo[:type]}).to eq(["text/plain", "image/jpeg"])
    end

    it "sends files with a Content-Length in the header" do
      post "/", "photos" => [uploaded_file, second_uploaded_file]
      last_request.POST["photos"].all?{|photo| expect(photo[:head]).to include("Content-Length: 4") }
    end

    it "sends both files as Tempfiles" do
      post "/", "photos" => [uploaded_file, second_uploaded_file]
      last_request.POST["photos"].all?{|photo| expect(photo[:tempfile]).to be_a(::Tempfile) }
    end
  end
end