public function testReadsRequestBody() { $response = new Response(200); $m = new MockAdapter($response); $m->setResponse($response); $body = Stream\create('foo'); $request = new Request('PUT', 'http://httpbin.org/put', [], $body); $this->assertSame($response, $m->send(new Transaction(new Client(), $request))); $this->assertEquals(3, $body->tell()); }
/** * Drain the steam into the destination stream */ private function getSaveToBody(RequestInterface $request, $stream) { if ($saveTo = $request->getConfig()['save_to']) { // Stream the response into the destination stream $saveTo = is_string($saveTo) ? Stream\create(fopen($saveTo, 'r+')) : Stream\create($saveTo); } else { // Stream into the default temp stream $saveTo = Stream\create(); } while (!feof($stream)) { $saveTo->write(fread($stream, 8096)); } fclose($stream); $saveTo->seek(0); return $saveTo; }
/** * Create the aggregate stream that will be used to upload the POST data */ private function createStream(array $fields, array $files) { $stream = new Stream\AppendStream(); foreach ($fields as $name => $fieldValues) { foreach ((array) $fieldValues as $value) { $stream->addStream(Stream\create($this->getFieldString($name, $value))); } } foreach ($files as $file) { if (!$file instanceof PostFileInterface) { throw new \InvalidArgumentException('All POST fields must ' . 'implement PostFieldInterface'); } $stream->addStream(Stream\create($this->getFileHeaders($file))); $stream->addStream($file->getContent()); $stream->addStream(Stream\create("\r\n")); } // Add the trailing boundary $stream->addStream(Stream\create("--{$this->boundary}--")); return $stream; }
/** * Creates an application/x-www-form-urlencoded stream body * * @return Stream\StreamInterface */ private function createUrlEncoded() { return Stream\create($this->getFields(true)); }
/** * Read data from a POST file, fill the read buffer with any overflow * * @param int $length Amount of data to read from the file * * @return string */ private function readFile($length) { $current = $this->files[$this->currentFile]; // Got to the next file and recursively return the read value, or bail // if no more data can be read. if ($current->getContent()->eof()) { return ++$this->currentFile == count($this->files) ? '' : $this->readFile($length); } // If this is the start of a file, then send the headers to the read // buffer. if (!isset($this->bufferedHeaders[$this->currentFile])) { $this->buffer = Stream\create($this->getFileHeaders($current)); $this->bufferedHeaders[$this->currentFile] = true; } // More data needs to be read to meet the limit, so pull from the file $content = $this->buffer ? $this->buffer->read($length) : ''; if (($remaining = $length - strlen($content)) > 0) { $content .= $current->getContent()->read($remaining); } return $content; }
private function add_save_to( RequestInterface $request, RequestMediator $mediator, &$options, $value ) { $mediator->setResponseBody(is_string($value) ? new Stream\LazyOpenStream($value, 'w') : Stream\create($value)); }
public function immutableProvider() { return [['setBody', Stream\create('foo')], ['removeHeader', 'abc'], ['addHeader', 'abc', '123'], ['addHeaders', []], ['setHeader', 'foo', 'bar'], ['setHeaders', []], ['json'], ['xml']]; }
/** * @expectedException \InvalidArgumentException */ public function testThrowsExceptionForUnknown() { Stream\create(new \stdClass()); }
private function add_json(RequestInterface $request, $value) { if (!$request->hasHeader('Content-Type')) { $request->setHeader('Content-Type', 'application/json'); } $request->setBody(Stream\create(json_encode($value))); }
/** * @expectedException Indigo\Sms\Exception\ResponseException * @expectedExceptionMessage Your email or password is wrong * @expectedExceptionCode 4 * @group Sms */ public function testFaultyResponse() { $this->client->shouldReceive('get')->andReturn(new Response(200, array(), Stream\create('{"result":"ERR","code":"4","message":"Your email or password is wrong"}'))); $this->gateway->getBalance(); }
private function add_body(RequestInterface $request, $value) { if ($value !== null) { if (is_array($value)) { $this->addPostData($request, $value); } else { $request->setBody(Stream\create($value)); } } }