Quellcode durchsuchen

[BrowserKit] fix Response::__toString() method to take care of multiple headers, for example when setting more than one cookie.

hhamon vor 14 Jahren
Ursprung
Commit
beaaa6d457

+ 22 - 2
src/Symfony/Component/BrowserKit/Response.php

@@ -48,12 +48,32 @@ class Response
     {
         $headers = '';
         foreach ($this->headers as $name => $value) {
-            $headers .= sprintf("%s: %s\n", $name, $value);
+            
+            if (is_string($value)) {
+                $headers .= $this->buildHeader($name, $value);
+            } else {
+                foreach ($value as $headerValue) {
+                    $headers .= $this->buildHeader($name, $headerValue);
+                }
+            }
         }
 
         return $headers."\n".$this->content;
     }
 
+    /**
+     * Returns the build header line.
+     *
+     * @param string $name  The header name
+     * @param string $value The header value
+     *
+     * @return string The buil header line
+     */
+    protected function buildHeader($name, $value)
+    {
+        return sprintf("%s: %s\n", $name, $value);
+    }
+
     /**
      * Gets the response content.
      *
@@ -106,4 +126,4 @@ class Response
 
         return $first ? null : array();
     }
-}
+}

+ 18 - 1
tests/Symfony/Tests/Component/BrowserKit/ResponseTest.php

@@ -56,4 +56,21 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals("foo: bar\n\nfoo", $response->__toString(), '->__toString() returns the headers and the content as a string');
     }
-}
+
+    public function testMagicToStringWithMultipleSetCookieHeader()
+    {
+        $headers = array(
+            'content-type' => 'text/html; charset=utf-8',
+            'set-cookie'   => array('foo=bar', 'bar=foo')  
+        );
+        
+        $expected = 'content-type: text/html; charset=utf-8'."\n";
+        $expected.= 'set-cookie: foo=bar'."\n";
+        $expected.= 'set-cookie: bar=foo'."\n\n";
+        $expected.= 'foo';
+        
+        $response = new Response('foo', 304, $headers);
+
+        $this->assertEquals($expected, $response->__toString(), '->__toString() returns the headers and the content as a string');
+    }
+}