예제 #1
0
 /**
  * @param Request $request
  *
  * @throws UnexpectedResponseException
  *
  * @return Response
  */
 public function sendRequest(Request $request)
 {
     if ('' === $request->getBaseUrl()) {
         $request = $request->withBaseUrl($this->baseUrl);
     }
     $headers = ['Accept-Language: ' . $this->acceptLanguage, 'Accept: application/vnd.trivago.affiliate.hal+json;version=1', 'X-Trv-Api-Key: ' . $this->apiKey];
     if ($this->trackingIdHandler->get() !== '') {
         $headers[] = 'Cookie: tid=' . $this->trackingIdHandler->get();
     }
     $httpRequest = new HttpRequest($request, $request->getMethod(), $headers);
     $response = $this->httpHandler->sendRequest($httpRequest);
     $headers = $response->getHeaders();
     if (!isset($headers['content-type']) || !preg_match('/json$/', $headers['content-type'])) {
         throw new UnexpectedResponseException('The response is not a valid JSON response.');
     }
     $this->trackingIdHandler->store($response->getTrivagoTrackingId());
     return $response;
 }
예제 #2
0
 public function test_throw_problem_exception_on_error()
 {
     $httpResponse = new Response('{"type":"http:\\/\\/www.w3.org\\/Protocols\\/rfc2616\\/rfc2616-sec10.html","title":"Internal Server Error","detail":"An error occurred.","code":"GENERIC-ERROR"}', '500', ['content-type' => 'application/json']);
     $this->httpHandler->sendRequest(Argument::type(HttpRequest::class))->willReturn($httpResponse);
     $tas = $this->createTas();
     try {
         $locations = $tas->getLocations(new LocationsRequest('Berlin'));
         $this->fail('Excepted that an exception will be thrown.');
     } catch (ProblemException $exception) {
         $this->assertSame(500, $exception->getHttpCode());
         $this->assertSame('http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html', $exception->getProblemType());
         $this->assertSame('Internal Server Error', $exception->getTitle());
         $this->assertSame('An error occurred.', $exception->getMessage());
         $this->assertSame('GENERIC-ERROR', $exception->getCode());
     }
 }