diff --git a/core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php b/core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php
index faceb07..16e9d61 100644
--- a/core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php
+++ b/core/lib/Drupal/Core/Cache/Context/QueryArgsCacheContext.php
@@ -28,11 +28,12 @@ public static function getLabel() {
    */
   public function getContext($query_arg = NULL) {
     if ($query_arg === NULL) {
-      return $this->requestStack->getCurrentRequest()->getQueryString();
-    }
-    else {
-      return $this->requestStack->getCurrentRequest()->query->get($query_arg);
+      $query_string = $this->requestStack->getCurrentRequest()->getQueryString();
+      return $query_string === NULL ? '' : $query_string;
     }
+
+    $value = $this->requestStack->getCurrentRequest()->query->get($query_arg, NULL, TRUE);
+    return $value === NULL ? '' : http_build_query([$query_arg => $value], '', '&');
   }
 
 }
diff --git a/core/tests/Drupal/Tests/Core/Cache/Context/QueryArgsCacheContextTest.php b/core/tests/Drupal/Tests/Core/Cache/Context/QueryArgsCacheContextTest.php
new file mode 100644
index 0000000..f223d36
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Cache/Context/QueryArgsCacheContextTest.php
@@ -0,0 +1,64 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\Core\Cache\Context\QueryArgsCacheContextTest.
+ */
+
+namespace Drupal\Tests\Core\Cache\Context;
+
+use Drupal\Core\Cache\Context\CacheContextsManager;
+use Drupal\Core\Cache\Context\QueryArgsCacheContext;
+use Drupal\Tests\UnitTestCase;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RequestStack;
+
+/**
+ * @coversDefaultClass \Drupal\Core\Cache\Context\QueryArgsCacheContext
+ * @group Cache
+ */
+class QueryArgsCacheContextTest extends UnitTestCase {
+
+  /**
+   * @covers ::getContext
+   *
+   * @dataProvider providerTestGetContext
+   */
+  public function testGetContext($uri, array $context_tokens, array $expected_keys) {
+    $request_stack = new RequestStack();
+    $request_stack->push(Request::create($uri));
+
+    $container_prophecy = $this->prophesize('Symfony\Component\DependencyInjection\ContainerInterface');
+    $container_prophecy->get('cache_context.url.query_args')->willReturn(new QueryArgsCacheContext($request_stack));
+
+    $cache_contexts_manager = new CacheContextsManager($container_prophecy->reveal(), ['url.query_args']);
+
+    $this->assertSame($expected_keys, $cache_contexts_manager->convertTokensToKeys($context_tokens));
+  }
+
+  /**
+   * Data provider for testGetContext.
+   *
+   * @return array
+   */
+  public function providerTestGetContext() {
+    return [
+      ['/foo', ['url.query_args'], ['']],
+      ['/foo?', ['url.query_args'], ['']],
+      ['/foo?a', ['url.query_args'], ['a']],
+      ['/foo?a=', ['url.query_args'], ['a=']],
+      ['/foo?a=1', ['url.query_args'], ['a=1']],
+      ['/foo?b[value]=1&a=2', ['url.query_args'], ['a=2&b%5Bvalue%5D=1']],
+      ['/foo', ['url.query_args:a'], ['']],
+      ['/foo?', ['url.query_args:a'], ['']],
+      ['/foo?a', ['url.query_args:a'], ['a=']],
+      ['/foo?a=', ['url.query_args:a'], ['a=']],
+      ['/foo?a=1', ['url.query_args:a'], ['a=1']],
+      ['/foo?a=1', ['url.query_args:b'], ['']],
+      ['/foo?b[value]=1&a=2', ['url.query_args:a'], ['a=2']],
+      ['/foo?b[value]=1&a=2', ['url.query_args:b'], ['b%5Bvalue%5D=1']],
+      ['/foo?b[value]=1&a=2', ['url.query_args:a', 'url.query_args:b'], ['a=2', 'b%5Bvalue%5D=1']],
+      ['/foo?b[value]=1&a=2', ['url.query_args:b[value]'], ['b%5Bvalue%5D=1']],
+    ];
+  }
+}
