|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LaunchDarkly\Tests\Impl\Integrations; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use LaunchDarkly\EventPublisher; |
| 7 | +use LaunchDarkly\Impl\Integrations; |
| 8 | +use LaunchDarkly\LDClient; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class CurlEventPublisherTest extends TestCase |
| 12 | +{ |
| 13 | + public function setUp(): void |
| 14 | + { |
| 15 | + if (!getenv("LD_INCLUDE_INTEGRATION_TESTS")) { |
| 16 | + $this->markTestSkipped("Skipping integration test"); |
| 17 | + } |
| 18 | + |
| 19 | + $client = new Client(); |
| 20 | + $client->request('DELETE', 'http://localhost:8080/__admin/requests'); |
| 21 | + } |
| 22 | + |
| 23 | + public function testSendsCorrectBodyAndHeaders() |
| 24 | + { |
| 25 | + $event = json_encode(["key" => "user-key"]); |
| 26 | + $publisher = new Integrations\CurlEventPublisher('sdk-key', ['events_uri' => 'http://localhost:8080']); |
| 27 | + $publisher->publish($event); |
| 28 | + |
| 29 | + $requests = []; |
| 30 | + $client = new Client(); |
| 31 | + |
| 32 | + // Provide time for the curl to execute |
| 33 | + $start = time(); |
| 34 | + while (time() - $start < 5) { |
| 35 | + $response = $client->request('GET', 'http://localhost:8080/__admin/requests'); |
| 36 | + $body = json_decode($response->getBody()->getContents(), true); |
| 37 | + $requests = $body['requests']; |
| 38 | + |
| 39 | + if ($requests) { |
| 40 | + break; |
| 41 | + } |
| 42 | + usleep(100); |
| 43 | + } |
| 44 | + |
| 45 | + if (!$requests) { |
| 46 | + $this->fail("Unable to connect to endpoint within specified timeout"); |
| 47 | + } |
| 48 | + |
| 49 | + $this->assertCount(1, $requests); |
| 50 | + |
| 51 | + $request = $requests[0]['request']; |
| 52 | + |
| 53 | + // Validate that we hit the right endpoint with the right data |
| 54 | + $this->assertEquals($event, $request['body']); |
| 55 | + $this->assertEquals('/bulk', $request['url']); |
| 56 | + |
| 57 | + // And validate that we provided all the correct headers |
| 58 | + $headers = $request['headers']; |
| 59 | + $this->assertEquals('application/json', $headers['Content-Type']); |
| 60 | + $this->assertEquals('application/json', $headers['Accept']); |
| 61 | + $this->assertEquals('sdk-key', $headers['Authorization']); |
| 62 | + $this->assertEquals('PHPClient/' . LDClient::VERSION, $headers['User-Agent']); |
| 63 | + $this->assertEquals(EventPublisher::CURRENT_SCHEMA_VERSION, $headers['X-LaunchDarkly-Event-Schema']); |
| 64 | + } |
| 65 | +} |
0 commit comments