示例#1
0
 public function testSetNotModified()
 {
     $response = new Response();
     $response->setHeaders(array('Allow' => 'test-value-to-be-removed', 'Content-Encoding' => 'test-value-to-be-removed', 'Content-Language' => 'test-value-to-be-removed', 'X-Some-Header' => 'test-value-to-NOT-be-removed'));
     $response->setNotModified();
     $this->assertEquals($response->getStatusCode(), 304);
     $this->assertNull($response->getHeader('Allow'));
     $this->assertNull($response->getHeader('Content-Encoding'));
     $this->assertNull($response->getHeader('Content-Language'));
     $this->assertNotNull($response->getHeader('X-Some-Header'));
 }
示例#2
0
 /**
  * Process an incoming request.
  * Determine the appropriate route for the request using a Router, and then
  * execute the resource method to obtain and return a result.
  *
  * @param \Sonno\Http\Request\RequestInterface $request The incoming request
  * @throws MalformedResourceRepresentationException
  * @throws \Sonno\Http\Exception\NotAcceptableException
  * @return \Sonno\Http\Response\Response
  */
 public function run(RequestInterface $request)
 {
     $selectedVariant = $result = null;
     try {
         // attempt to find routes that match the current request
         $router = new Router($this->_config);
         $routes = $router->match($request, $pathParams);
         // construct a hash map of Variants based on Routes
         $variantMap = array();
         // variant hash => <Sonno\Http\Variant>
         $variants = array();
         // array<Sonno\Http\Variant>
         /** @var $route \Sonno\Configuration\Route */
         foreach ($routes as $route) {
             $routeProduces = $route->getProduces();
             foreach ($routeProduces as $produces) {
                 $variant = new Variant(null, null, $produces);
                 $variantHash = spl_object_hash($variant);
                 $variantMap[$variantHash] = $route;
                 $variants[] = $variant;
             }
         }
         // select a Variant and find the corresponding route
         $selectedVariant = $request->selectVariant($variants);
         if (null == $selectedVariant) {
             throw new NotAcceptableException();
         }
         $selectedVariantHash = spl_object_hash($selectedVariant);
         $selectedRoute = $variantMap[$selectedVariantHash];
         // maintain URI information for resource class context injection
         $uriInfo = new UriInfo($this->_config, $request, $selectedRoute);
         $uriInfo->setPathParameters($pathParams);
         $uriInfo->setQueryParameters($request->getQueryParams());
         // execute the resource class method and obtain the result
         $dispatcher = $this->getDispatcher();
         $dispatcher->setRequest($request);
         $dispatcher->setUriInfo($uriInfo);
         $result = $dispatcher->dispatch($selectedRoute);
     } catch (MethodNotAllowedException $e) {
         // rewrite the response as a 200 OK when the request is OPTIONS
         if ('OPTIONS' == $request->getMethod()) {
             $e->getResponse()->setStatusCode(200);
         }
         $result = $e->getResponse();
     } catch (WebApplicationException $e) {
         $result = $e->getResponse();
     }
     // object is a scalar value: construct a new Response
     if (is_scalar($result)) {
         $response = new Response(200, $result);
         // object is already a Response
     } else {
         if ($result instanceof Response) {
             $response = $result;
             // object implements the Renderable interface: construct a Response
             // using the representation produced by render()
         } else {
             if ($result instanceof Renderable) {
                 $response = new Response(200, $result->render($selectedVariant));
                 // cannot determine how to handle the object returned
             } else {
                 throw new MalformedResourceRepresentationException();
             }
         }
     }
     // ensure a Content-Type header is present
     if (!$response->hasHeader('Content-Type') && $response->getStatusCode() < 300 && $response->getContent()) {
         $response->setHeaders(array('Content-Type' => $selectedVariant->getMediaType()));
     }
     // ensure a Content-Length header is present
     if (!$response->hasHeader('Content-Length')) {
         $response->setHeaders(array('Content-Length' => strlen($response->getContent())));
     }
     // process any HTTP status filter callbacks
     $statusCode = $response->getStatusCode();
     if (isset($this->_responseFilters[$statusCode])) {
         foreach ($this->_responseFilters[$statusCode] as $filterCallback) {
             $filterCallback($request, $response);
         }
     }
     $response->sendResponse();
     return $response;
 }
示例#3
0
 /**
  * This is the first example in this Resource that doesn't use a GET
  * annotation. Here, the resource will handle HTTP POST requests that are
  * sent in 'application/xml' form. Also, this method will also handle
  * requests to the same Path as the method above. The router determines
  * what resource method to call, not only based on the Path annotation, but
  * also what the HTTP method is as well as the Produces annotations.
  *
  * Another annotation that's being introduced here is the Consumes
  * annotation. This specifies what type of content it can accept based on
  * what the client's "Content-Type" header is set to. If the client
  * specifies a type not supported by a resource, a 415 Unsupported Media
  * Type is set, and this function is never executed.
  *
  * Looking at the function body, we're also using the property that was
  * injected via the Context annotation with a Sonno\Http\Request\Request
  * object. We use this object to pull the raw XML data out of the request
  * body.
  *
  * Finally, take notice that the function is returning a Response object.
  * But before the object is returned the method "setCreated" is called.
  * This will set the location header with the argument passed into the
  * constructor as well as set the response code to 201. There are several
  * other convenience methods on the Response object that can be used for
  * similar purposes.
  *
  * @POST
  * @Path("/users")
  * @Consumes({"application/xml"})
  */
 public function saveUserXml()
 {
     // Retrieve the request body.
     $data = $this->_request->getRequestBody();
     // ... Do some processing of $data, then save it...
     $response = new Response();
     return $response->setCreated('http://example.sonno.dev/users/10');
 }
示例#4
0
 /**
  * Setup a text fixture.
  * Set the Response content output stream to the abyss.
  *
  * @return void
  */
 protected function setUp()
 {
     \Sonno\Http\Response\Response::setContentOutputStream('php://temp');
 }