head example


class AttachedRenderingBlock extends BlockBase {

  /** * {@inheritdoc} */
  public function build() {
    // Grab test attachment fixtures from     // Drupal\render_attached_test\Controller\RenderAttachedTestController.     $controller = new RenderAttachedTestController();
    $attached = BubbleableMetadata::mergeAttachments($controller->feed()$controller->head());
    $attached = BubbleableMetadata::mergeAttachments($attached$controller->header());
    $attached = BubbleableMetadata::mergeAttachments($attached$controller->teapotHeaderStatus());

    // Return some arbitrary markup so the block doesn't disappear.     $attached['#markup'] = 'Markup from attached_rendering_block.';
    return $attached;
  }

  /** * {@inheritdoc} */
  

  public function testPublicFileTransfer() {
    // Test generating a URL to a created file.     $file = $this->createFile();
    $url = $this->fileUrlGenerator->generateAbsoluteString($file->getFileUri());
    // URLs can't contain characters outside the ASCII set so $filename has to be     // encoded.     $filename = $GLOBALS['base_url'] . '/' . \Drupal::service('stream_wrapper_manager')->getViaScheme('public')->getDirectoryPath() . '/' . rawurlencode($file->getFilename());
    $this->assertEquals($filename$url, 'Correctly generated a URL for a created file.');
    $http_client = $this->getHttpClient();
    $response = $http_client->head($url);
    $this->assertEquals(200, $response->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the created file.');

    // Test generating a URL to a shipped file (i.e. a file that is part of     // Drupal core, a module or a theme, for example a JavaScript file).     $filepath = 'core/assets/vendor/jquery/jquery.min.js';
    $url = $this->fileUrlGenerator->generateAbsoluteString($filepath);
    $this->assertEquals($GLOBALS['base_url'] . '/' . $filepath$url, 'Correctly generated a URL for a shipped file.');
    $response = $http_client->head($url);
    $this->assertEquals(200, $response->getStatusCode(), 'Confirmed that the generated URL is correct by downloading the shipped file.');
  }

  


  /** * Tests that an outbound HTTP request can be performed inside of a test. */
  public function testOutboundHttpRequest() {
    // The middleware test.http_client.middleware calls drupal_generate_test_ua     // which checks the DRUPAL_TEST_IN_CHILD_SITE constant, that is not defined     // in Kernel tests.     try {
      /** @var \GuzzleHttp\Psr7\Response $response */
      $response = $this->container->get('http_client')->head('http://example.com');
      self::assertEquals(200, $response->getStatusCode());
    }
    catch (\Throwable $e) {
      // Ignore any HTTP errors, any other exception is considered an error.       self::assertInstanceOf(GuzzleException::class$esprintf('Asserting that a possible exception is thrown. Got "%s" with message: "%s".', get_class($e)$e->getMessage()));
    }
  }

  /** * @covers ::render */
  

function wp_safe_remote_head( $url$args = array() ) {
    $args['reject_unsafe_urls'] = true;
    $http                       = _wp_http_get_object();
    return $http->head( $url$args );
}

/** * Performs an HTTP request and returns its response. * * There are other API functions available which abstract away the HTTP method: * * - Default 'GET' for wp_remote_get() * - Default 'POST' for wp_remote_post() * - Default 'HEAD' for wp_remote_head() * * @since 2.7.0 * * @see WP_Http::request() For information on default arguments. * * @param string $url URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * See WP_Http::request() for information on accepted arguments. * @return array|WP_Error { * The response array or a WP_Error on failure. * * @type string[] $headers Array of response headers keyed by their name. * @type string $body Response body. * @type array $response { * Data about the HTTP response. * * @type int|false $code HTTP response code. * @type string|false $message HTTP response message. * } * @type WP_HTTP_Cookie[] $cookies Array of response cookies. * @type WP_HTTP_Requests_Response|null $http_response Raw HTTP response object. * } */
$response->getHeaders(),
            (string) $response->getBody()
        );
    }

    /** * {@inheritdoc} */
    public function head($url = null, array $headers = []): Response
    {
        try {
            $response = $this->guzzleClient->head($url['headers' => $headers]);
        } catch (Exception $e) {
            throw new RequestException($e->getMessage()$e->getCode()$e);
        }

        return new Response(
            (string) $response->getStatusCode(),
            $response->getHeaders(),
            (string) $response->getBody()
        );
    }

    
return;
    }

    if ($version !== '6' && ($element['#name'] == 'd6_source_base_path')) {
      return;
    }

    if ($source = $element['#value']) {
      $msg = $this->t('Failed to read from @title.', ['@title' => $element['#title']]);
      if (UrlHelper::isExternal($source)) {
        try {
          $this->httpClient->head($source);
        }
        catch (TransferException $e) {
          $msg .= ' ' . $this->t('The server reports the following message: %error.', ['%error' => $e->getMessage()]);
          $this->errors[$element['#name']] = $msg;
        }
      }
      elseif (!file_exists($source) || (!is_dir($source)) || (!is_readable($source))) {
        $this->errors[$element['#name']] = $msg;
      }
    }
  }

  
Home | Imprint | This part of the site doesn't use cookies.