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 testCreatesRequestFromMessage() { $f = new MessageFactory(); $req = $f->fromMessage("GET / HTTP/1.1\r\nBaz: foo\r\n\r\n"); $this->assertEquals('GET', $req->getMethod()); $this->assertEquals('/', $req->getPath()); $this->assertEquals('foo', $req->getHeader('Baz')); $this->assertNull($req->getBody()); }
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'); }
/** * 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); }
/** * @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); }
/** * 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); }
/** * @param string $message * @return Response */ public function fromMessage($message) { return parent::fromMessage($this->setHttpProtocolToMessage($message)); }