Sha256: ce12b542c5b3c4efec8ee2561d8fe16637697fcbadd7fde658ef561b37a3332c

Contents?: true

Size: 1.88 KB

Versions: 9

Compression:

Stored size: 1.88 KB

Contents

#--
# (c) Copyright 2007-2008 Nick Sieger.
# See the file README.txt included with the distribution for
# software license details.
#++

require 'net/http/post/multipart'

class Net::HTTP::Post::MultiPartTest < Test::Unit::TestCase
  TEMP_FILE = "temp.txt"

  HTTPPost = Struct.new("HTTPPost", :content_length, :body_stream, :content_type)
  HTTPPost.module_eval do
    def set_content_type(type, params = {})
      self.content_type = type + params.map{|k,v|"; #{k}=#{v}"}.join('')
    end
  end

  def teardown
    File.delete(TEMP_FILE) rescue nil
  end

  def test_form_multipart_body
    File.open(TEMP_FILE, "w") {|f| f << "1234567890"}
    @io = File.open(TEMP_FILE)
    UploadIO.convert! @io, "text/plain", TEMP_FILE, TEMP_FILE
    assert_results Net::HTTP::Post::Multipart.new("/foo/bar", :foo => 'bar', :file => @io)
  end
  def test_form_multipart_body_put
    File.open(TEMP_FILE, "w") {|f| f << "1234567890"}
    @io = File.open(TEMP_FILE)
    UploadIO.convert! @io, "text/plain", TEMP_FILE, TEMP_FILE
    assert_results Net::HTTP::Put::Multipart.new("/foo/bar", :foo => 'bar', :file => @io)
  end
  
  def test_form_multipart_body_with_stringio
    @io = StringIO.new("1234567890")
    UploadIO.convert! @io, "text/plain", TEMP_FILE, TEMP_FILE
    assert_results Net::HTTP::Post::Multipart.new("/foo/bar", :foo => 'bar', :file => @io)
  end

  def assert_results(post)
    assert post.content_length && post.content_length > 0
    assert post.body_stream
    assert_equal "multipart/form-data; boundary=#{Multipartable::DEFAULT_BOUNDARY}", post['content-type']
    body = post.body_stream.read
    boundary_regex = Regexp.quote Multipartable::DEFAULT_BOUNDARY
    assert body =~ /1234567890/
    # ensure there is at least one boundary
    assert body =~ /^--#{boundary_regex}\r\n/
    # ensure there is an epilogue
    assert body =~ /^--#{boundary_regex}--\r\n/
    assert body =~ /text\/plain/
  end
end

Version data entries

9 entries across 9 versions & 5 rubygems

Version Path
mlooney-multipart-post-0.1.1 test/net/http/post/test_multipart.rb
nicksieger-multipart-post-0.9 test/net/http/post/test_multipart.rb
puppet-module-0.3.4 vendor/multipart-post-1.0/test/net/http/post/test_multipart.rb
puppet-module-0.3.3 vendor/multipart-post-1.0/test/net/http/post/test_multipart.rb
puppet-module-0.3.2 vendor/multipart-post-1.0/test/net/http/post/test_multipart.rb
jwagener-multipart-post-1.0.3 test/net/http/post/test_multipart.rb
puppet-module-0.3.0 vendor/multipart-post-1.0/test/net/http/post/test_multipart.rb
multipart-post-1.0.1 test/net/http/post/test_multipart.rb
multipart-post-1.0 test/net/http/post/test_multipart.rb