Пример #1
1
 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));
     }
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 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');
 }
Пример #4
0
 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;
 }
Пример #6
0
 /**
  * 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);
 }
Пример #7
0
 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);
 }
Пример #9
0
 /**
  * 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);
 }
Пример #10
0
 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);
 }
Пример #11
0
 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());
     }
 }
Пример #12
0
 /**
  * @param $message
  * @return HttpResponse
  */
 protected function getHttpResponseFromMessage($message)
 {
     $factory = new HttpMessageFactory();
     return $factory->fromMessage($message);
 }
Пример #13
0
 /**
  * Convert the Symfony request to a Guzzle request.
  *
  * @param  Request $request
  * @return RequestInterface
  */
 protected function convertRequest(Request $request)
 {
     return $this->messageFactory->fromMessage((string) $request);
 }
Пример #14
0
 /**
  * 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));
 }
Пример #15
0
 /**
  * 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;
 }
Пример #16
0
 /**
  * @param ResponseInterface|string $response
  * @return \GuzzleHttp\Message\RequestInterface|ResponseInterface
  */
 protected function createResponse($response)
 {
     return $this->messageFactory->fromMessage($response);
 }
Пример #17
0
 public function createResponse($statusCode, array $headers = [], $body = null, array $options = [])
 {
     $guzzleResponse = parent::createResponse($statusCode, $headers, $body, $options);
     return new Response($guzzleResponse->json());
 }
Пример #18
0
 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));
 }
Пример #19
0
 /**
  * @param string $message
  * @return Response
  */
 public function fromMessage($message)
 {
     return parent::fromMessage($this->setHttpProtocolToMessage($message));
 }