/** * 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()); }
/** * 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); }