Browse Source

Adding a part by hand should result in multipart

Adding a part will make a payload into a multipart message. Preserve
that fact.
Mark Story 10 years ago
parent
commit
7e5c0d7f92
2 changed files with 26 additions and 2 deletions
  1. 24 2
      src/Network/Http/FormData.php
  2. 2 0
      tests/TestCase/Network/Http/FormDataTest.php

+ 24 - 2
src/Network/Http/FormData.php

@@ -42,6 +42,13 @@ class FormData implements Countable
     protected $_hasFile = false;
 
     /**
+     * Whether or not this formdata object has a complex part.
+     *
+     * @var bool
+     */
+    protected $_hasComplexPart = false;
+
+    /**
      * The parts in the form data.
      *
      * @var array
@@ -102,6 +109,7 @@ class FormData implements Countable
             );
             $this->_parts[] = $this->addFile($name, $value);
         } elseif ($name instanceof Part && $value === null) {
+            $this->_hasComplexPart = true;
             $this->_parts[] = $name;
         } else {
             $this->_parts[] = $this->newPart($name, $value);
@@ -199,6 +207,20 @@ class FormData implements Countable
     }
 
     /**
+     * Check whether or not the current payload
+     * is multipart.
+     *
+     * A payload will become multipart when you add files
+     * or use add() with a Part instance.
+     *
+     * @return bool Whether or not the payload is multipart.
+     */
+    public function isMultipart()
+    {
+        return $this->hasFile() || $this->_hasComplexPart;
+    }
+
+    /**
      * Get the content type for this payload.
      *
      * If this object contains files, `multipart/form-data` will be used,
@@ -208,7 +230,7 @@ class FormData implements Countable
      */
     public function contentType()
     {
-        if (!$this->hasFile()) {
+        if (!$this->isMultipart()) {
             return 'application/x-www-form-urlencoded';
         }
         return 'multipart/form-data; boundary="' . $this->boundary() . '"';
@@ -222,7 +244,7 @@ class FormData implements Countable
      */
     public function __toString()
     {
-        if ($this->hasFile()) {
+        if ($this->isMultipart()) {
             $boundary = $this->boundary();
             $out = '';
             foreach ($this->_parts as $part) {

+ 2 - 0
tests/TestCase/Network/Http/FormDataTest.php

@@ -84,6 +84,8 @@ class FormDataTest extends TestCase
         $part->contentId('abc123');
         $data->add($part);
 
+        $this->assertTrue($data->isMultipart());
+        $this->assertFalse($data->hasFile());
         $this->assertCount(1, $data, 'Should have 1 part');
         $expected = [
             '--' . $boundary,