diff --git a/src/Plugin/migrate_plus/data_fetcher/Http.php b/src/Plugin/migrate_plus/data_fetcher/Http.php index df5c6b773d76e4443c83fbceeb86a4c5ed1e7fc8..7f39246d68bb2d39b2c36f1d9aff097dfc0089b5 100755 --- a/src/Plugin/migrate_plus/data_fetcher/Http.php +++ b/src/Plugin/migrate_plus/data_fetcher/Http.php @@ -10,6 +10,19 @@ use GuzzleHttp\Exception\RequestException; /** * Retrieve data over an HTTP connection for migration. * + * * Example: + * + * @code + * source: + * plugin: url + * data_fetcher_plugin: http + * headers: + * Accept: application/json + * User-Agent: Internet Explorer 6 + * Authorization-Key: secret + * Arbitrary-Header: foobarbaz + * @endcode + * * @DataFetcher( * id = "http", * title = @Translation("HTTP") @@ -44,6 +57,8 @@ class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterf public function __construct(array $configuration, $plugin_id, $plugin_definition) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->httpClient = \Drupal::httpClient(); + + $this->setRequestHeaders($configuration['headers']); } /** diff --git a/tests/src/Kernel/Plugin/migrate_plus/data_fetcher/HttpTest.php b/tests/src/Kernel/Plugin/migrate_plus/data_fetcher/HttpTest.php new file mode 100644 index 0000000000000000000000000000000000000000..09aef97b359b0153b9bdeb98f60edc141f76d48a --- /dev/null +++ b/tests/src/Kernel/Plugin/migrate_plus/data_fetcher/HttpTest.php @@ -0,0 +1,43 @@ +<?php + +namespace Drupal\Tests\migrate_plus\Kernel\Plugin\migrate_plus\data_fetcher; + +use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\KernelTests\KernelTestBase; +use Drupal\migrate_plus\Plugin\migrate_plus\data_fetcher\Http; +use Drupal\Tests\Core\Test\KernelTestBaseTest; +use Drupal\Tests\UnitTestCase; + +/** + * Class HttpTest + * + * @group migrate_plus + * @package Drupal\Tests\migrate_plus\Unit\migrate_plus\data_fetcher + */ +class HttpTest extends KernelTestBase { + + /** + * Test http headers option. + */ + function testHttpHeaders() { + $expected = [ + 'Accept' => 'application/json', + 'User-Agent' => 'Internet Explorer 6', + 'Authorization-Key' => 'secret', + 'Arbitrary-Header' => 'foobarbaz' + ]; + + $configuration = [ + 'headers' => [ + 'Accept' => 'application/json', + 'User-Agent' => 'Internet Explorer 6', + 'Authorization-Key' => 'secret', + 'Arbitrary-Header' => 'foobarbaz' + ] + ]; + + $http = new Http($configuration, 'http', []); + + $this->assertEquals($expected, $http->getRequestHeaders()); + } +}