class Hurley::MultipartTest

Public Instance Methods

test_build_form_with_flat_query() click to toggle source
# File test/multipart_test.rb, line 5
def test_build_form_with_flat_query
  ctype, body = Query::Flat.new(:a => [1,2,3]).to_form
  assert_equal "application/x-www-form-urlencoded", ctype
  assert_equal "a=1&a=2&a=3", body.read
end
test_build_form_with_nested_query() click to toggle source
# File test/multipart_test.rb, line 11
def test_build_form_with_nested_query
  ctype, body = Query::Nested.new(:a => {:b => 2}).to_form
  assert_equal "application/x-www-form-urlencoded", ctype
  assert_equal "a%5Bb%5D=2", body.read
end
test_build_multipart_with_flat_query() click to toggle source
# File test/multipart_test.rb, line 25
def test_build_multipart_with_flat_query
  ctype, body = Query::Flat.new(
    :a => 1,
    :array => [1, 2],
    :file => UploadIO.new(__FILE__, "text/plain"),
  ).to_form

  src = IO.read(__FILE__)

  boundary = nil
  if ctype =~ %r{\Amultipart/form-data; boundary=(Hurley\-.*)}
    boundary = $1
  else
    fail "bad ctype: #{ctype.inspect}"
  end

  assert_match %r{\Amultipart/form-data; boundary=Hurley\-}, ctype
  expected = %Q(--#{boundary}\r\n) +
    %Q(Content-Disposition: form-data; name="a"\r\n) +
    %Q(\r\n1\r\n) +

    %Q(--#{boundary}\r\n) +
    %Q(Content-Disposition: form-data; name="array"\r\n) +
    %Q(\r\n1\r\n) +

    %Q(--#{boundary}\r\n) +
    %Q(Content-Disposition: form-data; name="array"\r\n) +
    %Q(\r\n2\r\n) +

    %Q(--#{boundary}\r\n) +
    %Q(Content-Disposition: form-data; name="file"; filename="multipart_test.rb"\r\n) +
    %Q(Content-Length: #{src.size}\r\n) +
    %Q(Content-Type: text/plain\r\n) +
    %Q(Content-Transfer-Encoding: binary\r\n) +
    %Q(\r\n#{src}\r\n) +

    %Q(--#{boundary}--\r\n\r\n)

  assert_equal expected, body.read.to_s
end
test_build_multipart_with_nested_query() click to toggle source
# File test/multipart_test.rb, line 66
def test_build_multipart_with_nested_query
  ctype, body = Query::Nested.new(:a => {
    :num => 1,
    :arr => [1, 2],
    :file => UploadIO.new(__FILE__, "text/plain"),
  }).to_form

  src = IO.read(__FILE__)

  boundary = nil
  if ctype =~ %r{\Amultipart/form-data; boundary=(Hurley\-.*)}
    boundary = $1
  else
    fail "bad ctype: #{ctype.inspect}"
  end

  expected = %Q(--#{boundary}\r\n) +
    %Q(Content-Disposition: form-data; name="a[num]"\r\n) +
    %Q(\r\n1\r\n) +

    %Q(--#{boundary}\r\n) +
    %Q(Content-Disposition: form-data; name="a[arr][]"\r\n) +
    %Q(\r\n1\r\n) +

    %Q(--#{boundary}\r\n) +
    %Q(Content-Disposition: form-data; name="a[arr][]"\r\n) +
    %Q(\r\n2\r\n) +

    %Q(--#{boundary}\r\n) +
    %Q(Content-Disposition: form-data; name="a[file]"; filename="multipart_test.rb"\r\n) +
    %Q(Content-Length: #{src.size}\r\n) +
    %Q(Content-Type: text/plain\r\n) +
    %Q(Content-Transfer-Encoding: binary\r\n) +
    %Q(\r\n#{src}\r\n) +

    %Q(--#{boundary}--\r\n\r\n)

  assert_equal expected, body.read.to_s
end
test_build_multipart_with_set_boundary() click to toggle source
# File test/multipart_test.rb, line 17
def test_build_multipart_with_set_boundary
  options = RequestOptions.new
  options.boundary = :wat

  ctype, _ = Query::Nested.new(:file => UploadIO.new(__FILE__, "text/plain")).to_form(options)
  assert_equal "multipart/form-data; boundary=wat", ctype
end