public function testNegotiationException()
 {
     $this->setExpectedException(TelnetExceptionInterface::class);
     $socket = m::mock(Socket::class)->shouldReceive('read')->andThrow(\Exception::class)->getMock();
     $iac = new InterpretAsCommand();
     $iac->interpret(chr(255), $socket);
 }
Esempio n. 2
0
 /**
  * @param string $prompt
  *
  * @return \Graze\TelnetClient\TelnetResponseInterface
  * @throws TelnetExceptionInterface
  */
 protected function getResponse($prompt = null)
 {
     $isError = false;
     $buffer = '';
     do {
         // process one character at a time
         try {
             $character = $this->socket->read(1);
         } catch (Exception $e) {
             throw new TelnetException('failed reading from socket', 0, $e);
         }
         if (in_array($character, [$this->NULL, $this->DC1])) {
             break;
         }
         if ($this->interpretAsCommand->interpret($character, $this->socket)) {
             continue;
         }
         $buffer .= $character;
         // check for prompt
         if ($this->promptMatcher->isMatch($prompt ?: $this->prompt, $buffer, $this->lineEnding)) {
             break;
         }
         // check for error prompt
         if ($this->promptMatcher->isMatch($this->promptError, $buffer, $this->lineEnding)) {
             $isError = true;
             break;
         }
     } while (true);
     return new TelnetResponse($isError, $this->promptMatcher->getResponseText(), $this->promptMatcher->getMatches());
 }