/** * Test adding a part with a filehandle. * * @return void */ public function testAddFileHandle() { $file = CORE_PATH . 'VERSION.txt'; $fh = fopen($file, 'r'); $data = new FormData(); $data->add('upload', $fh); $boundary = $data->boundary(); $result = (string) $data; rewind($fh); $contents = stream_get_contents($fh); $expected = array('--' . $boundary, 'Content-Disposition: form-data; name="upload"', 'Content-Type: application/octet-stream', '', $contents, '--' . $boundary . '--', '', ''); $this->assertEquals(implode("\r\n", $expected), $result); }
/** * Builds the request content based on the request object. * * If the $request->body() is a string, it will be used as is. * Array data will be processed with Cake\Network\Http\FormData * * @param \Cake\Network\Http\Request $request The request being sent. * @param array $options Array of options to use. * @return void */ protected function _buildContent(Request $request, $options) { $content = $request->body(); if (empty($content)) { return; } if (is_string($content)) { $this->_contextOptions['content'] = $content; return; } if (is_array($content)) { $formData = new FormData(); $formData->addMany($content); $type = 'multipart/form-data; boundary="' . $formData->boundary() . '"'; $request->header('Content-Type', $type); $this->_contextOptions['content'] = (string) $formData; return; } $this->_contextOptions['content'] = $content; }
/** * Test adding a part with a filehandle. * * @return void */ public function testAddFileHandle() { $file = CORE_PATH . 'VERSION.txt'; $fh = fopen($file, 'r'); $data = new FormData(); $data->add('upload', $fh); $boundary = $data->boundary(); $result = (string) $data; rewind($fh); $contents = stream_get_contents($fh); $expected = ['--' . $boundary, 'Content-Disposition: form-data; name="upload"; filename="VERSION.txt"', 'Content-Type: text/plain; charset=us-ascii', '', $contents, '--' . $boundary . '--', '', '']; $this->assertEquals(implode("\r\n", $expected), $result); }