|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2018 OpenCensus Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace OpenCensus\Tests\Unit\Trace\Integrations\Guzzle; |
| 19 | + |
| 20 | +use OpenCensus\Core\Context; |
| 21 | +use OpenCensus\Trace\Tracer; |
| 22 | +use OpenCensus\Trace\Exporter\ExporterInterface; |
| 23 | +use OpenCensus\Trace\Integrations\Guzzle\Middleware; |
| 24 | +use Prophecy\Argument; |
| 25 | +use Psr\Http\Message\RequestInterface; |
| 26 | +use Psr\Http\Message\ResponseInterface; |
| 27 | + |
| 28 | +/** |
| 29 | + * @group trace |
| 30 | + */ |
| 31 | +class MiddlewareTest extends \PHPUnit_Framework_TestCase |
| 32 | +{ |
| 33 | + private $exporter; |
| 34 | + |
| 35 | + public function setUp() |
| 36 | + { |
| 37 | + $this->exporter = $this->prophesize(ExporterInterface::class); |
| 38 | + if (extension_loaded('opencensus')) { |
| 39 | + opencensus_trace_clear(); |
| 40 | + } else { |
| 41 | + Context::reset(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public function testAddsSpanContextHeader() |
| 46 | + { |
| 47 | + $this->exporter->report(Argument::that(function ($tracer) { |
| 48 | + $spans = $tracer->spans(); |
| 49 | + return count($spans) == 3 && $spans[2]->name() == 'GuzzleHttp::request'; |
| 50 | + }))->shouldBeCalled(); |
| 51 | + |
| 52 | + $rt = Tracer::start($this->exporter->reveal(), [ |
| 53 | + 'skipReporting' => true |
| 54 | + ]); |
| 55 | + |
| 56 | + $handler = function ($request, $options) { |
| 57 | + $response = $this->prophesize(ResponseInterface::class); |
| 58 | + return $response->reveal(); |
| 59 | + }; |
| 60 | + |
| 61 | + $middleware = new Middleware(); |
| 62 | + $stack = $middleware($handler); |
| 63 | + $req = $this->prophesize(RequestInterface::class); |
| 64 | + $req->getMethod()->willReturn('GET')->shouldBeCalled(); |
| 65 | + $req->getUri()->willReturn('/')->shouldBeCalled(); |
| 66 | + $req->withHeader('HTTP_X_CLOUD_TRACE_CONTEXT', Argument::that(function ($val) { |
| 67 | + return preg_match('/[0-9a-f]+\/4660;o=1/', $val); |
| 68 | + }))->willReturn($req->reveal())->shouldBeCalled(); |
| 69 | + $request = $req->reveal(); |
| 70 | + |
| 71 | + $response = Tracer::inSpan(['name' => 'parentSpan', 'spanId' => '1234'], function () use ($stack, $request) { |
| 72 | + return $stack($request, []); |
| 73 | + }); |
| 74 | + |
| 75 | + $rt->onExit(); |
| 76 | + } |
| 77 | +} |
0 commit comments