/** * Create service with name * * @param ServiceLocatorInterface $serviceLocator * @param $name * @param $requestedName * @return HttpApi */ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { $config = $this->getConfig($serviceLocator)[$requestedName]; $httpClient = isset($config['http_client']) && $serviceLocator->has($config['http_client']) ? $serviceLocator->get($config['http_client']) : null; $baseRequest = isset($config['base_request']) && $serviceLocator->has($config['base_request']) ? $serviceLocator->get($config['base_request']) : null; $api = new HttpApi($httpClient, $baseRequest); // Array of int code valid if (isset($config['valid_status_code']) && is_array($config['valid_status_code'])) { $api->setValidStatusCodes($config['valid_status_code']); } // string json/xml if (isset($config['request_format'])) { $api->setRequestFormat($config['request_format']); } // Profiler if (isset($config['profiler']) && $serviceLocator->has($config['profiler'])) { $api->setProfiler($serviceLocator->get($config['profiler'])); } return $api; }
/** * @param string $method * @param string $id * @param array $query * @param array $data * @return Request */ public function prepareRequest($method, $id = null, array $data = [], array $query = []) { $request = parent::prepareRequest($method, null, $data, $query); $this->getUriResourceStrategy()->configureUri($request->getUri(), $this->resourceName, $id); return $request; }
public function testPrepareFileUploadRequest() { $httpClient = $this->getMockBuilder('Zend\\Http\\Client')->disableOriginalConstructor()->setMethods(['dispatch', 'getResponse'])->getMock(); $expectedResponse = new Response(); $expectedResponse->setContent('{"status":"ok"}'); $expectedResponse->getHeaders()->addHeaderLine('Content-Type', 'application/json'); $httpClient->expects($this->any())->method('dispatch')->will($this->returnValue($expectedResponse)); $httpClient->expects($this->any())->method('getResponse')->will($this->returnValue($expectedResponse)); $api = new HttpApi($httpClient); $request = $api->prepareFileUploadRequest(['myfile' => realpath(__DIR__ . '/fileToUpload.txt')], 'resourcePath', ['foo' => 'bar', 'myfile' => 'foo.txt'], ['test' => 'test']); $this->assertInstanceOf('\\Zend\\Http\\Request', $request); $this->assertEquals($api->getBaseRequest()->getUri()->getPath() . 'resourcePath', $request->getUri()->getPath()); $this->assertFalse($request->getHeader('Content-Type', false)); $this->assertSame(['test' => 'test'], $request->getQuery()->toArray()); $this->assertSame(['foo' => 'bar'], $request->getPost()->toArray()); $this->assertSame([realpath(__DIR__ . '/fileToUpload.txt') => ['formname' => 'myfile', 'filename' => 'foo.txt', 'ctype' => 'text/plain; charset=us-ascii', 'data' => 'Dummy']], $request->getFiles()->toArray()); $response = $api->dispatchRequest($request); $this->assertSame($api->getResponseDecoder()->decode($expectedResponse), $response); $this->assertSame($request, $api->getLastRequest()); $this->assertSame($expectedResponse, $api->getLastResponse()); $this->assertSame($api->getResponseDecoder()->getLastPayload(), $api->getLastResponseData()); }