From f73dec817e78e9ba0ce1ae9ed1b7b64653bb07a9 Mon Sep 17 00:00:00 2001
From: tmountjr <tmountjr@3503589.no-reply.drupal.org>
Date: Wed, 3 Jan 2018 09:53:47 -0600
Subject: [PATCH] Issue #2931756 by tmountjr, edysmp, heddn: NULL headers
 causing error

---
 src/Plugin/migrate_plus/data_fetcher/Http.php |  2 +
 .../migrate_plus/data_fetcher/HttpTest.php    | 55 +++++++++++++------
 2 files changed, 39 insertions(+), 18 deletions(-)

diff --git a/src/Plugin/migrate_plus/data_fetcher/Http.php b/src/Plugin/migrate_plus/data_fetcher/Http.php
index 7f39246d..46da4b20 100755
--- a/src/Plugin/migrate_plus/data_fetcher/Http.php
+++ b/src/Plugin/migrate_plus/data_fetcher/Http.php
@@ -58,6 +58,8 @@ class Http extends DataFetcherPluginBase implements ContainerFactoryPluginInterf
     parent::__construct($configuration, $plugin_id, $plugin_definition);
     $this->httpClient = \Drupal::httpClient();
 
+    // Ensure there is a 'headers' key in the configuration.
+    $configuration += ['headers' => []];
     $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
index 09aef97b..8e824c82 100644
--- a/tests/src/Kernel/Plugin/migrate_plus/data_fetcher/HttpTest.php
+++ b/tests/src/Kernel/Plugin/migrate_plus/data_fetcher/HttpTest.php
@@ -18,26 +18,45 @@ class HttpTest extends KernelTestBase {
 
   /**
    * Test http headers option.
+   *
+   * @dataProvider headerDataProvider
    */
-  function testHttpHeaders() {
-    $expected = [
-      'Accept' => 'application/json',
-      'User-Agent' => 'Internet Explorer 6',
-      'Authorization-Key' => 'secret',
-      'Arbitrary-Header' => 'foobarbaz'
-    ];
+  public function testHttpHeaders(array $definition, array $expected, array $preSeed = []) {
+    $http = new Http($definition, 'http', []);
+    $this->assertEquals($expected, $http->getRequestHeaders());
+  }
 
-    $configuration = [
-      'headers' => [
-        'Accept' => 'application/json',
-        'User-Agent' => 'Internet Explorer 6',
-        'Authorization-Key' => 'secret',
-        'Arbitrary-Header' => 'foobarbaz'
-      ]
+  /**
+   * Provides multiple test cases for the testHttpHeaders method.
+   *
+   * @return array
+   *   The test cases
+   */
+  public function headerDataProvider() {
+    return [
+      'dummy headers specified' => [
+        'definition' => [
+          'headers' => [
+            'Accept' => 'application/json',
+            'User-Agent' => 'Internet Explorer 6',
+            'Authorization-Key' => 'secret',
+            'Arbitrary-Header' => 'foobarbaz',
+          ],
+        ],
+        'expected' => [
+          'Accept' => 'application/json',
+          'User-Agent' => 'Internet Explorer 6',
+          'Authorization-Key' => 'secret',
+          'Arbitrary-Header' => 'foobarbaz',
+        ],
+      ],
+      'no headers specified' => [
+        'definition' => [
+          'no_headers_here' => 'foo',
+        ],
+        'expected' => [],
+      ],
     ];
-
-    $http = new Http($configuration, 'http', []);
-
-    $this->assertEquals($expected, $http->getRequestHeaders());
   }
+
 }
-- 
GitLab