public function handle_exception_with_mapper() { $t = new \lang\Throwable('Test'); $this->fixture->addExceptionMapping('lang.Throwable', newinstance(ExceptionMapper::class, [], '{ public function asResponse($t, RestContext $ctx) { return Response::error(500)->withPayload(array("message" => $t->getMessage())); } }')); $this->assertEquals(\webservices\rest\srv\Response::status(500)->withPayload(new \webservices\rest\Payload(['message' => 'Test'], ['name' => 'exception'])), $this->fixture->handle($this, $this->getClass()->getMethod('raiseAnError'), [$t])); }
public function with_two_cookies() { $user = new Cookie('user', 'Test'); $lang = new Cookie('language', 'de'); $this->assertEquals([$user, $lang], Response::status(200)->withCookie($user)->withCookie($lang)->cookies); }
public function handle_string_class_in_parameters_and_return() { $handler = newinstance('lang.Object', array(), '{ #[@webmethod(verb= "GET")] public function fixture(String $name) { if ($name->startsWith("www.")) { return array("name" => $name->substring(4)); } else { return array("name" => $name); } } }'); $this->assertEquals(Response::status(200)->withPayload(new Payload(array('name' => 'example.com'))), $this->handle($handler, array(new \lang\types\String('example.com')))); }
public function no_return() { $handler = newinstance(Object::class, [], '{ #[@webmethod(verb= "GET")] public function fixture() { return; } }'); $this->assertEquals(Response::status(200)->withPayload(null), $this->handle($handler)); }
/** * 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); } } }