/**
  * Dispatches a BanchaRequestCollection object. It uses the standard CakePHP dispatcher to dispatch the single
  * CakeRequest objects returned by BanchaRequest. Further it uses BanchaResponseCollection to transform the responses
  * into a single CakeResponse object. If the 'return' option in the $additionalParams argument is TRUE, the body of the
  * response is returned instead of directly sent to the browser.
  *
  * @param BanchaRequestCollection $requests A BanchaRequestCollection can contain multiple CakeRequest objects.
  * @param array $additionalParams If 'return' is TRUE, the body is returned instead of sent to the browser.
  * @return string|void If 'return' is TRUE, the body is returned otherwise void is returned.
  */
 public function dispatch(BanchaRequestCollection $requests, $additionalParams = array())
 {
     $collection = new BanchaResponseCollection();
     // Iterate through all requests, dispatch them and add the response to the transformer object.
     foreach ($requests->getRequests() as $request) {
         $skip_request = false;
         if (!$skip_request) {
             // Call dispatcher for the given CakeRequest.
             // We need to use a sub classes disaptcher, because some parameters are missing in Bancha requests and
             // because we need to full response, not only the body of the response.
             $dispatcher = new BanchaSingleDispatcher();
             try {
                 // dispatch the request
                 $response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
                 $dispatcher->dispatch($request, $response, array('return' => true));
                 // add result to response colection
                 $collection->addResponse($request['tid'], $response, $request);
             } catch (Exception $e) {
                 $collection->addException($request['tid'], $e, $request);
             }
             // try catch
         }
         // if (!$skip_request)
     }
     // foreach
     // Combine the responses and return or output them.
     $responses = $collection->getResponses();
     if (isset($additionalParams['return']) && $additionalParams['return']) {
         return $responses->body();
     }
     $responses->send();
 }
 /**
  * Tests the getResponses() method in combination with the 'extUpload' request parameter. If this parameter is true,
  * the response should not be JSON encoded but rather a valid HTML structure which contains the result inside a
  * <textarea>-element.
  *
  */
 public function testGetResponses_extUpload()
 {
     $response1 = array('body' => array('message' => 'Hello World'));
     $request = new CakeRequest();
     $request->addParams(array('controller' => 'foo', 'action' => 'bar', 'extUpload' => true));
     $collection = new BanchaResponseCollection();
     $collection->addResponse(2, new CakeResponse($response1), $request);
     $expected = '<html><body><textarea>[{"type":"rpc","tid":2,"action":"foo","method":"bar",' . '"result":' . json_encode($response1['body']) . ',"extUpload":true}]</textarea></body></html>';
     $this->assertEquals($expected, $collection->getResponses()->body());
 }