public function onBefore(BeforeEvent $event) { $request = $event->getRequest(); if (file_exists($this->getFullFilePath($request))) { $responsedata = file_get_contents($this->getFullFilePath($request)); $mf = new MessageFactory(); $event->intercept($mf->fromMessage($responsedata)); } }
/** * Add a response to the end of the queue * * @param string|ResponseInterface $response Response or path to response file * * @return self * @throws \InvalidArgumentException if a string or Response is not passed */ public function addResponse($response) { if (is_string($response)) { $response = file_exists($response) ? $this->factory->fromMessage(file_get_contents($response)) : $this->factory->fromMessage($response); } elseif (!$response instanceof ResponseInterface) { throw new \InvalidArgumentException('Response must a message ' . 'string, response object, or path to a file'); } $this->queue[] = $response; return $this; }
public function testFormat() { $factory = new MessageFactory(); $requestString = file_get_contents(MOCK_BASE_PATH . '/sendpayment_request.txt'); $request = $factory->fromMessage($requestString); $formatter = new Formatter('{req_body}'); $tpl = $formatter->format($request); $this->assertContains('<ewayCardNumber modified>XXXXXXXXXXXX1111</ewayCardNumber>', $tpl, 'Formatted request body should hide the credit card number'); $this->assertContains('<ewayCardNumber>4444333322221111</ewayCardNumber>', $request->getBody()->__toString(), 'Original request should not be modified'); }
public function testUrl() { $app = $this->getApp(); $factory = new MessageFactory(); $request = $factory->createRequest('GET', '/'); $response = new Response(Response::HTTP_OK); $guzzle = $this->getMock('GuzzleHttp\\Client', ['get']); $guzzle->expects($this->once())->method('get')->with('http://loripsum.net/api/1/veryshort')->will($this->returnValue($request)); $app['guzzle.client'] = $guzzle; $app['prefill']->get('/1/veryshort'); }
/** * Returns the content of a json fixture as array * * @param string $fixture the path to the json fixture file * @return array|mixed */ protected function getResponseFixtureContentAsArray($fixture) { $messageFactory = new MessageFactory(); $fixtureFilename = dirname(dirname(__FILE__)) . '/Fixtures/' . $fixture; if (!file_exists($fixtureFilename)) { $this->fail(sprintf('Fixture "%s" not found!', $fixtureFilename)); } $response = $messageFactory->fromMessage(file_get_contents($fixtureFilename)); $result = array(); try { $result = $response->json(); } catch (\GuzzleHttp\Exception\ParseException $exception) { $this->fail(sprintf('Fixture "%s" does not contain a valid JSON body!', $fixtureFilename)); } return $result; }
/** * Queue an array of responses or a single response on the server. * * Any currently queued responses will be overwritten. Subsequent requests * on the server will return queued responses in FIFO order. * * @param array $responses Responses to queue. * @throws \Exception */ public static function enqueue(array $responses) { static $factory; if (!$factory) { $factory = new MessageFactory(); } $data = []; foreach ($responses as $response) { // Create the response object from a string if (is_string($response)) { $response = $factory->fromMessage($response); } elseif (!$response instanceof ResponseInterface) { throw new \Exception('Responses must be strings or Responses'); } $data[] = self::convertResponse($response); } TestServer::enqueue($data); }
public function testUrl() { $app = $this->getApp(); if ($app['deprecated.php']) { $factory = new RequestFactory(); $request = $factory->create('GET', '/'); $response = new V3Response('200'); $guzzle = $this->getMock('Guzzle\\Service\\Client', array('get', 'send')); $request->setClient($guzzle); $guzzle->expects($this->once())->method('send')->will($this->returnValue($response)); } else { $factory = new MessageFactory(); $request = $factory->createRequest('GET', '/'); $response = new Response('200'); $guzzle = $this->getMock('GuzzleHttp\\Client', array('get')); } $guzzle->expects($this->once())->method('get')->with('http://loripsum.net/api/1/veryshort')->will($this->returnValue($request)); $app['guzzle.client'] = $guzzle; $app['prefill']->get('/1/veryshort'); }
private function createResponse($code = 200) { $factory = new MessageFactory(); return $factory->createResponse($code); }
/** * Creates a response * * @param string $statusCode HTTP status code * @param array $headers Response headers * @param mixed $body Response body * @param array $options Response options * - protocol_version: HTTP protocol version * - header_factory: Factory used to create headers * - And any other options used by a concrete message implementation * * @return ResponseInterface */ public function createResponse($statusCode, array $headers = [], $body = null, array $options = []) { return parent::createResponse($statusCode, $headers, $body, $options); }
public function testCanUseCustomRequestOptions() { $c = false; $f = new MessageFactory(['foo' => function (RequestInterface $request, $value) use(&$c) { $c = true; $this->assertEquals('bar', $value); }]); $f->createRequest('PUT', 'http://f.com', ['headers' => ['Content-Type' => 'foo'], 'foo' => 'bar']); $this->assertTrue($c); }
public function testCanUseCustomSubclassesWithMethods() { (new ExtendedFactory())->createRequest('PUT', 'http://f.com', ['headers' => ['Content-Type' => 'foo'], 'foo' => 'bar']); try { $f = new MessageFactory(); $f->createRequest('PUT', 'http://f.com', ['headers' => ['Content-Type' => 'foo'], 'foo' => 'bar']); } catch (\InvalidArgumentException $e) { $this->assertContains('foo config', $e->getMessage()); } }
/** * @param $message * @return HttpResponse */ protected function getHttpResponseFromMessage($message) { $factory = new HttpMessageFactory(); return $factory->fromMessage($message); }
/** * Convert the Symfony request to a Guzzle request. * * @param Request $request * @return RequestInterface */ protected function convertRequest(Request $request) { return $this->messageFactory->fromMessage((string) $request); }
/** * Test that responses that can't have freshness determined return null. */ public function testGetFreshnessNull() { $messageFactory = new MessageFactory(); $response = $messageFactory->createResponse(200); $this->assertSame(null, Utils::getFreshness($response)); }
/** * Get all of the received requests * * @param bool $hydrate Set to TRUE to turn the messages into * actual {@see RequestInterface} objects. If $hydrate is FALSE, * requests will be returned as strings. * * @return array * @throws \RuntimeException */ public static function received($hydrate = false) { if (!self::$started) { return []; } $response = self::getClient()->get('guzzle-server/requests'); $data = array_filter(explode(self::REQUEST_DELIMITER, (string) $response->getBody())); if ($hydrate) { $factory = new MessageFactory(); $data = array_map(function ($message) use($factory) { return $factory->fromMessage($message); }, $data); } return $data; }
/** * @param ResponseInterface|string $response * @return \GuzzleHttp\Message\RequestInterface|ResponseInterface */ protected function createResponse($response) { return $this->messageFactory->fromMessage($response); }
public function createResponse($statusCode, array $headers = [], $body = null, array $options = []) { $guzzleResponse = parent::createResponse($statusCode, $headers, $body, $options); return new Response($guzzleResponse->json()); }
public function testAddsCookieUsingTrue() { $factory = new MessageFactory(); $request1 = $factory->createRequest('GET', '/', ['cookies' => true]); $request2 = $factory->createRequest('GET', '/', ['cookies' => true]); $listeners = function ($r) { return array_filter($r->getEmitter()->listeners('before'), function ($l) { return $l[0] instanceof Cookie; }); }; $this->assertSame($listeners($request1), $listeners($request2)); }
/** * @param string $message * @return Response */ public function fromMessage($message) { return parent::fromMessage($this->setHttpProtocolToMessage($message)); }