示例#1
0
 public function writeTo_does_not_fully_qualify_url_in_location_header()
 {
     $res = new HttpScriptletResponse();
     Response::see('http://localhost/')->writeTo($res, new URL('http://example.com/'), null);
     $this->assertEquals('Location: http://localhost/', $res->headers[0]);
 }
 public function marshal_exceptions()
 {
     $this->fixture->addMarshaller('unittest.AssertionFailedError', newinstance(TypeMarshaller::class, [], '{
   public function marshal($t) {
     return "assert:".$t->message;
   }
   public function unmarshal(\\lang\\Type $target, $name) {
     // Not needed
   }
 }'));
     $this->assertEquals(\webservices\rest\srv\Response::error(500)->withPayload(new \webservices\rest\Payload('assert:expected 1 but was 2', ['name' => 'exception'])), $this->fixture->asResponse(new \unittest\AssertionFailedError('expected 1 but was 2')));
 }
    public function raised_exception($status, $class)
    {
        $handler = newinstance('lang.Object', array($class), '{
      protected $class;

      public function __construct($class) {
        $this->class= $class;
      }

      #[@webmethod(verb= "GET")]
      public function fixture() {
        raise($this->class, "Test", NULL);
      }
    }');
        $this->assertEquals(Response::error($status)->withPayload(new Payload(array('message' => 'Test'), array('name' => 'exception'))), $this->handle($handler));
    }
 public function raised_exception($status, $exception)
 {
     $handler = newinstance(Object::class, [$exception], ['exception' => null, '__construct' => function ($exception) {
         $this->exception = $exception;
     }, '#[@webmethod(verb= "GET")] fixture' => function () {
         throw new $this->exception('Test', null);
     }]);
     $this->assertEquals(Response::error($status)->withPayload(new Payload(['message' => 'Test'], ['name' => 'exception'])), $this->handle($handler));
 }
 public function greet_and_go($name)
 {
     return Response::noContent();
 }
 public function marshal_exceptions()
 {
     $this->fixture->addMarshaller('unittest.AssertionFailedError', newinstance('webservices.rest.TypeMarshaller', array(), '{
   public function marshal($t) {
     return "expected ".xp::stringOf($t->expect)." but was ".xp::stringOf($t->actual);
   }
   public function unmarshal(Type $target, $name) {
     // Not needed
   }
 }'));
     $this->assertEquals(\webservices\rest\srv\Response::error(500)->withPayload(new \webservices\rest\Payload('expected 1 but was 2', array('name' => 'exception'))), $this->fixture->mapException(new \unittest\AssertionFailedError('Test', 2, 1)));
 }
示例#7
0
 /**
  * Handle routing item
  *
  * @param  lang.Oject instance
  * @param  lang.reflect.Method method
  * @param  var[] args
  * @param  webservices.rest.srv.RestContext context
  * @return webservices.rest.srv.Response
  */
 public function handle($instance, $method, $args)
 {
     // HACK: Ungeneric XML-related
     $properties = [];
     if ($method->hasAnnotation('xmlfactory', 'element')) {
         $properties['name'] = $method->getAnnotation('xmlfactory', 'element');
     } else {
         if (($class = $method->getDeclaringClass()) && $class->hasAnnotation('xmlfactory', 'element')) {
             $properties['name'] = $class->getAnnotation('xmlfactory', 'element');
         }
     }
     // Invoke the method
     try {
         $result = $method->invoke($instance, $args);
         $this->cat && $this->cat->debug('<-', $result);
     } catch (TargetInvocationException $e) {
         $this->cat && $this->cat->warn('<-', $e);
         $cause = $e->getCause();
         return $this->asResponse($cause, $this->findMapper($cause));
     }
     // For "VOID" methods, set status to "no content". If a response is returned,
     // use its status, headers and payload. For any other methods, set status to "OK".
     if (Type::$VOID->equals($method->getReturnType())) {
         return Response::status(HttpConstants::STATUS_NO_CONTENT);
     } else {
         if ($result instanceof Output) {
             $result->payload = $this->marshal($result->payload, $properties);
             return $result;
         } else {
             $payload = $this->marshal(new Payload($result), $properties);
             return Response::status(HttpConstants::STATUS_OK)->withPayload($payload);
         }
     }
 }
 public function prev_header_on_last_page()
 {
     $this->assertEquals(['Link' => new LinkHeader(['prev' => self::BASE_URL . '?page=1'])], $this->fixture->paginate($this->newRequest('?page=2'), Response::ok(), true)->headers);
 }
 public function paginate_removes_excess_elements_from_payload($by)
 {
     $this->assertEquals(array_fill(0, self::SIZE, 'element'), $this->newFixture()->paginate(Response::ok(), array_fill(0, self::SIZE + $by, 'element'))->payload->value);
 }