File: test_parts.rb

package info (click to toggle)
ruby-multipart-post 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: ruby: 423; makefile: 4
file content (86 lines) | stat: -rw-r--r-- 2,122 bytes parent folder | download | duplicates (2)
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
#--
# Copyright (c) 2007-2012 Nick Sieger.
# See the file README.txt included with the distribution for
# software license details.
#++

require 'test/unit'

require 'parts'
require 'stringio'
require 'composite_io'
require 'tempfile'


MULTIBYTE = File.dirname(__FILE__)+'/multibyte.txt'
TEMP_FILE = "temp.txt"

module AssertPartLength
  def assert_part_length(part)
    bytes = part.to_io.read
    bytesize = bytes.respond_to?(:bytesize) ? bytes.bytesize : bytes.length
    assert_equal bytesize, part.length
  end
end

class PartTest < Test::Unit::TestCase
  def setup
    @string_with_content_type = Class.new(String) do
      def content_type; 'application/data'; end
    end
  end

  def test_file_with_upload_io
    assert Parts::Part.file?(UploadIO.new(__FILE__, "text/plain"))
  end

  def test_file_with_modified_string
    assert !Parts::Part.file?(@string_with_content_type.new("Hello"))
  end

  def test_new_with_modified_string
    assert_kind_of Parts::ParamPart,
      Parts::Part.new("boundary", "multibyte", @string_with_content_type.new("Hello"))
  end
end

class FilePartTest < Test::Unit::TestCase
  include AssertPartLength

  def setup
    File.open(TEMP_FILE, "w") {|f| f << "1234567890"}
    io =  UploadIO.new(TEMP_FILE, "text/plain")
    @part = Parts::FilePart.new("boundary", "afile", io)
  end

  def teardown
    File.delete(TEMP_FILE) rescue nil
  end

  def test_correct_length
    assert_part_length @part
  end

  def test_multibyte_file_length
    assert_part_length Parts::FilePart.new("boundary", "multibyte", UploadIO.new(MULTIBYTE, "text/plain"))
  end

  def test_multibyte_filename
    name = File.read(MULTIBYTE, 300)
    file = Tempfile.new(name.respond_to?(:force_encoding) ? name.force_encoding("UTF-8") : name)
    assert_part_length Parts::FilePart.new("boundary", "multibyte", UploadIO.new(file, "text/plain"))
    file.close
  end
end

class ParamPartTest < Test::Unit::TestCase
  include AssertPartLength

  def setup
    @part = Parts::ParamPart.new("boundary", "multibyte", File.read(MULTIBYTE))
  end

  def test_correct_length
    assert_part_length @part
  end
end