public function testBar() { $request = new Request(array('SERVER_NAME' => 'www.example.org', 'SERVER_PORT' => 80, 'QUERY_STRING' => '', 'REQUEST_URI' => '/bar', 'SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/bar', 'REQUEST_METHOD' => 'GET')); $service = new Service(); $service->addModule(new TestModule('foo')); $service->addModule(new TestModule('bar')); $response = $service->run($request); $this->assertSame(array('HTTP/1.1 200 OK', 'Content-Type: text/html;charset=UTF-8', 'Content-Length: 3', '', 'bar'), $response->toArray()); }
* you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ require_once dirname(__DIR__) . '/vendor/autoload.php'; use fkooman\Rest\Service; use fkooman\Http\Response; use fkooman\Http\Session; $session = new Session('foo'); $service = new Service(); $service->get('/', function () { $response = new Response(200, 'text/plain'); $response->setBody('Welcome!'); return $response; }); $service->get('/:key', function ($key) use($session) { $newCount = $session->has($key) ? $session->get($key) + 1 : 1; $session->set($key, $newCount); $response = new Response(200, 'text/plain'); $response->setBody(sprintf('count: %d', $newCount)); return $response; }); $service->run()->send();
$service = new Service($request); // require authentication? if (null !== $config->getValue('basicUser')) { $basicAuthPlugin = new BasicAuthentication($config->getValue('basicUser'), $config->getValue('basicPass'), $config->getValue('serviceName')); $service->registerBeforeMatchingPlugin($basicAuthPlugin); } // GROUPS $service->match("GET", "/groups/:uid", function ($uid) use($request, $vootStorage) { $groups = $vootStorage->isMemberOf($uid, $request->getQueryParameter("startIndex"), $request->getQueryParameter("count")); $response = new JsonResponse(200); $response->setContent($groups); return $response; }); // PEOPLE IN GROUP $service->match("GET", "/people/:uid/:gid", function ($uid, $gid) use($request, $vootStorage) { $users = $vootStorage->getGroupMembers($uid, $gid, $request->getQueryParameter("startIndex"), $request->getQueryParameter("count")); $response = new JsonResponse(200); $response->setContent($users); return $response; }); $service->run()->sendResponse(); } catch (VootStorageException $e) { $response = new JsonResponse($e->getResponseCode()); $response->setContent(array("error" => $e->getMessage(), "error_description" => $e->getDescription())); $response->sendResponse(); } catch (Exception $e) { // any other error thrown by any of the modules, assume internal server error $response = new JsonResponse(500); $response->setContent(array("error" => "internal_server_error", "error_description" => $e->getMessage())); $response->sendResponse(); }
public function testInit() { $testPlugin = new TestPlugin(); $request = new Request(array('SERVER_NAME' => 'www.example.org', 'SERVER_PORT' => 80, 'QUERY_STRING' => '', 'REQUEST_URI' => '/index.php/foo', 'SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/foo', 'REQUEST_METHOD' => 'GET')); $service = new Service(); $service->getPluginRegistry()->registerDefaultPlugin($testPlugin); $this->assertSame(array('HTTP/1.1 200 OK', 'Content-Type: text/html;charset=UTF-8', 'Content-Length: 3', '', 'foo'), $service->run($request)->toArray()); }