public function testCastsToString() { $m = new puzzle_message_Request('GET', 'http://foo.com'); $m->setHeader('foo', 'bar'); $m->setBody(puzzle_stream_Stream::factory('baz')); $this->assertEquals("GET / HTTP/1.1\r\nHost: foo.com\r\nfoo: bar\r\n\r\nbaz", (string) $m); }
public function testReadsFromRequestBody() { $body = puzzle_stream_Stream::factory('foo'); $t = new puzzle_adapter_Transaction(new puzzle_Client(), new puzzle_message_Request('PUT', 'http://httbin.org', array(), $body)); $m = new puzzle_adapter_curl_RequestMediator($t, new puzzle_message_MessageFactory()); $this->assertEquals('foo', $m->readRequestBody(null, null, 3)); }
public function testCanAddHeaders() { $p = new puzzle_post_PostFile('foo', puzzle_stream_Stream::factory('hi'), 'test.php', array('X-Foo' => '123', 'Content-Disposition' => 'bar')); $headers = $p->getHeaders(); $this->assertEquals('bar', $headers['Content-Disposition']); $this->assertEquals('123', $headers['X-Foo']); }
public function testHasStream() { $s = puzzle_stream_Stream::factory('foo'); $e = new puzzle_stream_exception_SeekException($s, 10); $this->assertSame($s, $e->getStream()); $this->assertContains('10', $e->getMessage()); }
public function testCastsToString() { $m = new tubepress_test_lib_http_impl_puzzle_message_MessageTest__foo(new tubepress_test_lib_http_impl_puzzle_message_MessageTest__bar()); $m->setHeader('foo', 'bar'); $m->setBody(new tubepress_http_impl_puzzle_streams_PuzzleBasedStream(puzzle_stream_Stream::factory('baz'))); $this->assertEquals("GET / HTTP/1.1\r\nfoo: bar\r\n\r\nbaz", (string) $m); }
public function testHandlesClose() { $s = puzzle_stream_Stream::factory('foo'); $wrapped = new puzzle_stream_NoSeekStream($s); $wrapped->close(); $this->assertFalse($wrapped->write('foo')); }
public function testInflatesStreams() { $content = gzencode('test'); $a = puzzle_stream_Stream::factory($content); $b = new puzzle_stream_InflateStream($a); $this->assertEquals('test', (string) $b); }
public function setUp() { $this->c = fopen('php://temp', 'r+'); fwrite($this->c, 'foo'); fseek($this->c, 0); $this->a = puzzle_stream_Stream::factory($this->c); $this->b = new puzzle_test_stream_Str($this->a); }
public function testDecoratesWithCustomizations() { $this->_closure_var_testDecoratesWithCustomizations_called = false; $this->_closure_var_testDecoratesWithCustomizations_a = puzzle_stream_Stream::factory('foo'); $b = puzzle_stream_FnStream::decorate($this->_closure_var_testDecoratesWithCustomizations_a, array('read' => array($this, '__callback_testDecoratesWithCustomizations'))); $this->assertEquals('foo', $b->read(3)); $this->assertTrue($this->_closure_var_testDecoratesWithCustomizations_called); }
public function testReadsBodiesFromMockedRequests() { $m = new puzzle_subscriber_Mock(array(new puzzle_message_Response(200))); $client = new puzzle_Client(array('base_url' => 'http://test.com')); $client->getEmitter()->attach($m); $body = puzzle_stream_Stream::factory('foo'); $client->put('/', array('body' => $body)); $this->assertEquals(3, $body->tell()); }
public function testReadsRequestBody() { $response = new puzzle_message_Response(200); $m = new puzzle_adapter_MockAdapter($response); $m->setResponse($response); $body = puzzle_stream_Stream::factory('foo'); $request = new puzzle_message_Request('PUT', 'http://httpbin.org/put', array(), $body); $this->assertSame($response, $m->send(new puzzle_adapter_Transaction(new puzzle_Client(), $request))); $this->assertEquals(3, $body->tell()); }
public function testCastsToString() { $r = new puzzle_message_Request('GET', 'http://test.com/test', array('foo' => 'baz'), puzzle_stream_Stream::factory('body')); $s = explode("\r\n", (string) $r); $this->assertEquals("GET /test HTTP/1.1", $s[0]); $this->assertContains('Host: test.com', $s); $this->assertContains('foo: baz', $s); $this->assertContains('', $s); $this->assertContains('body', $s); }
public function testCanDetermineSizeFromMultipleStreams() { $a = new puzzle_stream_AppendStream(array(puzzle_stream_Stream::factory('foo'), puzzle_stream_Stream::factory('bar'))); $this->assertEquals(6, $a->getSize()); $s = $this->getMockBuilder('puzzle_stream_StreamInterface')->setMethods(array('isSeekable', 'isReadable'))->getMockForAbstractClass(); $s->expects($this->once())->method('isSeekable')->will($this->returnValue(null)); $s->expects($this->once())->method('isReadable')->will($this->returnValue(true)); $a->addStream($s); $this->assertNull($a->getSize()); }
public function testPreventsComplexExternalEntities() { $xml = '<?xml version="1.0"?><!DOCTYPE scan[<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=ResponseTest.php">]><scan>&test;</scan>'; $response = new puzzle_message_Response(200, array(), puzzle_stream_Stream::factory($xml)); $oldCwd = getcwd(); chdir(dirname(__FILE__)); try { $xml = $response->xml(); chdir($oldCwd); $this->markTestIncomplete('Did not throw the expected exception! XML resolved as: ' . $xml->asXML()); } catch (Exception $e) { chdir($oldCwd); } }
public function testThrowsExceptionWhenFailsToParseJsonResponse() { try { $response = new tubepress_http_impl_puzzle_PuzzleBasedResponse(new puzzle_message_Response(200, array(), puzzle_stream_Stream::factory('{"foo": "'))); $response->toJson(); } catch (puzzle_exception_ParseException $e) { if (version_compare(PHP_VERSION, '7.0') >= 0) { $this->assertEquals('Unable to parse JSON data: JSON_ERROR_CTRL_CHAR - Unexpected control character found', $e->getMessage()); } else { $this->assertEquals('Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON', $e->getMessage()); } return; } $this->fail('Should have thrown exception'); }
/** * Prepares the contents of a POST file. * * @param mixed $content Content of the POST file */ private function prepareContent($content) { $this->content = $content; if (!$this->content instanceof puzzle_stream_StreamInterface) { $this->content = puzzle_stream_Stream::factory($this->content); } elseif ($this->content instanceof puzzle_post_MultipartBody) { if (!$this->hasHeader('Content-Disposition')) { $disposition = 'form-data; name="' . $this->name . '"'; $this->headers['Content-Disposition'] = $disposition; } if (!$this->hasHeader('Content-Type')) { $this->headers['Content-Type'] = sprintf("multipart/form-data; boundary=%s", $this->content->getBoundary()); } } }
public function testCanCastToString() { $client = new puzzle_Client(array('base_url' => 'http://localhost/')); $h = new puzzle_subscriber_History(); $client->getEmitter()->attach($h); $mock = new puzzle_subscriber_Mock(array(new puzzle_message_Response(301, array('Location' => '/redirect1', 'Content-Length' => 0)), new puzzle_message_Response(307, array('Location' => '/redirect2', 'Content-Length' => 0)), new puzzle_message_Response(200, array('Content-Length' => '2'), puzzle_stream_Stream::factory('HI')))); $client->getEmitter()->attach($mock); $request = $client->createRequest('GET', '/'); $client->send($request); $this->assertEquals(3, count($h)); $h = str_replace("\r", '', $h); $this->assertContains("> GET / HTTP/1.1\nHost: localhost\nUser-Agent:", $h); $this->assertContains("< HTTP/1.1 301 Moved Permanently\nLocation: /redirect1", $h); $this->assertContains("< HTTP/1.1 307 Temporary Redirect\nLocation: /redirect2", $h); $this->assertContains("< HTTP/1.1 200 OK\nContent-Length: 2\n\nHI", $h); }
public function testResource() { $stream = puzzle_stream_Stream::factory('foo'); $handle = puzzle_stream_GuzzleStreamWrapper::getResource($stream); $this->assertSame('foo', fread($handle, 3)); $this->assertSame(3, ftell($handle)); $this->assertSame(3, fwrite($handle, 'bar')); $this->assertSame(0, fseek($handle, 0)); $this->assertSame('foobar', fread($handle, 6)); $this->assertTrue(feof($handle)); // This fails on HHVM for some reason if (!defined('HHVM_VERSION')) { $this->assertEquals(array('dev' => 0, 'ino' => 0, 'mode' => 33206, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 6, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0, 0 => 0, 1 => 0, 2 => 33206, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 6, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0), fstat($handle)); } $this->assertTrue(fclose($handle)); $this->assertSame('foobar', (string) $stream); }
public function onRequest(tubepress_api_event_EventInterface $event) { /* * @var tubepress_api_http_message_RequestInterface */ $httpRequest = $event->getSubject(); if (!$this->_shouldExecute($httpRequest)) { return; } $url = $httpRequest->getUrl(); $item = $this->_getCachedItem($url); if ($item->isMiss()) { return; } $response = new tubepress_http_impl_puzzle_PuzzleBasedResponse(new puzzle_message_Response(200, array(self::HTTP_HEADER_CACHE_HIT => 'true'), puzzle_stream_Stream::factory($item->get()))); $event->setArgument('response', $response); $event->stopPropagation(); }
public function onComplete(puzzle_event_CompleteEvent $event) { $transferInfo = $event->getTransferInfo(); if (array_key_exists('http_code', $transferInfo)) { //curl return; } $response = $event->getResponse(); if (!$response->hasHeader('Transfer-Encoding')) { return; } $encoding = $response->getHeader('Transfer-Encoding'); if (strcasecmp($encoding, 'chunked') !== 0) { return; } $body = $response->getBody()->__toString(); $decodedBody = $this->_decode($body); $response->setBody(puzzle_stream_Stream::factory($decodedBody)); }
/** * Creates the underlying stream lazily when required. * * @return puzzle_stream_StreamInterface */ protected function createStream() { return puzzle_stream_Stream::factory(puzzle_stream_Utils::open($this->filename, $this->mode)); }
/** * Creates an application/x-www-form-urlencoded stream body * * @return puzzle_stream_StreamInterface */ private function createUrlEncoded() { return puzzle_stream_Stream::factory($this->getFields(true)); }
private function add_json(puzzle_message_RequestInterface $request, $value) { if (!$request->hasHeader('Content-Type')) { $request->setHeader('Content-Type', 'application/json'); } $request->setBody(puzzle_stream_Stream::factory(json_encode($value))); }
/** * Drain the stream into the destination stream */ private function getSaveToBody(puzzle_message_RequestInterface $request, puzzle_stream_StreamInterface $stream) { $config = $request->getConfig(); if ($saveTo = $config['save_to']) { // Stream the response into the destination stream $saveTo = is_string($saveTo) ? new puzzle_stream_Stream(puzzle_stream_Utils::open($saveTo, 'r+')) : puzzle_stream_Stream::factory($saveTo); } else { // Stream into the default temp stream $saveTo = puzzle_stream_Stream::factory(); } puzzle_stream_Utils::copyToStream($stream, $saveTo); $saveTo->seek(0); $stream->close(); return $saveTo; }
public function testRetriesRewindableStreamsWhenClosedConnectionErrors() { $this->runConnectionTest(array("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", "HTTP/1.1 201 OK\r\nContent-Length: 0\r\n\r\n"), puzzle_stream_Stream::factory('foo'), false, 201); }
/** * @expectedException InvalidArgumentException */ public function testThrowsExceptionForUnknown() { puzzle_stream_Stream::factory(new stdClass()); }
private function add_save_to(puzzle_message_RequestInterface $request, puzzle_adapter_curl_RequestMediator $mediator, $value) { $mediator->setResponseBody(is_string($value) ? new puzzle_stream_LazyOpenStream($value, 'w') : puzzle_stream_Stream::factory($value)); }
/** * Alias of puzzle_stream_Stream::factory. * * @param mixed $resource Resource to create * @param int $size Size if known up front * * @return puzzle_stream_MetadataStreamInterface * * @see puzzle_stream_Stream::factory */ public static function create($resource, $size = null) { return puzzle_stream_Stream::factory($resource, $size); }
public function testLengthLessOffsetWhenNoLimitSize() { $a = puzzle_stream_Stream::factory('foo_bar'); $b = new puzzle_stream_LimitStream($a, -1, 4); $this->assertEquals(3, $b->getSize()); }
/** * @deprecated Moved to puzzle_stream_Stream::factory */ function puzzle_stream_create($resource = '', $size = null) { return puzzle_stream_Stream::factory($resource, $size); }