Browse Source

Merge pull request #11335 from saeideng/response/gettype

Implement response::getType()
Mark Story 8 years ago
parent
commit
5cbedd382f
2 changed files with 35 additions and 3 deletions
  1. 13 3
      src/Http/Response.php
  2. 22 0
      tests/TestCase/Http/ResponseTest.php

+ 13 - 3
src/Http/Response.php

@@ -1047,14 +1047,14 @@ class Response implements ResponseInterface
     public function type($contentType = null)
     {
         if ($contentType === null) {
-            return $this->_contentType;
+            return $this->getType();
         }
         if (is_array($contentType)) {
             foreach ($contentType as $type => $definition) {
                 $this->_mimeTypes[$type] = $definition;
             }
 
-            return $this->_contentType;
+            return $this->getType();
         }
         if (isset($this->_mimeTypes[$contentType])) {
             $contentType = $this->_mimeTypes[$contentType];
@@ -1070,6 +1070,16 @@ class Response implements ResponseInterface
     }
 
     /**
+     * Returns the current content type.
+     *
+     * @return string
+     */
+    public function getType()
+    {
+        return $this->_contentType;
+    }
+
+    /**
      * Get an updated response with the content type set.
      *
      * If you attempt to set the type on a 304 or 204 status code response, the
@@ -1168,7 +1178,7 @@ class Response implements ResponseInterface
     }
 
     /**
-     * Retruns the current charset.
+     * Returns the current charset.
      *
      * @return string
      */

+ 22 - 0
tests/TestCase/Http/ResponseTest.php

@@ -214,6 +214,28 @@ class ResponseTest extends TestCase
     }
 
     /**
+     * Tests the getType method
+     *
+     * @return void
+     */
+    public function testGetType()
+    {
+        $response = new Response();
+        $this->assertEquals('text/html', $response->getType());
+        $response->type('pdf');
+        $this->assertEquals('application/pdf', $response->getType());
+
+        $this->assertEquals(
+            'custom/stuff',
+            $response->withType('custom/stuff')->getType()
+        );
+        $this->assertEquals(
+            'application/json',
+            $response->withType('json')->getType()
+        );
+    }
+
+    /**
      * Tests the withType method
      *
      * @return void