WebApiExtension icon indicating copy to clipboard operation
WebApiExtension copied to clipboard

Is it possible to configure the guzzle client ?

Open greg0ire opened this issue 9 years ago • 12 comments

I'm testing an API that I just moved to https, and since I'm using a self-signed-certificate, guzzle gives me errors. Maybe the verify option should be set to false by default, and maybe it should even be configurable (not sure why though).

Here is how I worked around this problem.

greg0ire avatar Dec 16 '14 17:12 greg0ire

How to configure the url from localhost:80?

eddiejaoude avatar Dec 17 '14 11:12 eddiejaoude

Figured it out, to use https, could you do the same...

Behat\WebApiExtension:
        base_url: http://localhost:8000

eddiejaoude avatar Dec 17 '14 12:12 eddiejaoude

Hum… why port 8000 ? I do not understand what you are trying to do…

greg0ire avatar Dec 17 '14 12:12 greg0ire

Just what my dev is currently on. But no reason why you could not do https to overwrite the default.

E.g.

Behat\WebApiExtension:
        base_url: https://localhost

eddiejaoude avatar Dec 17 '14 12:12 eddiejaoude

This what I did, and then I got errors because guzzle was checking the server certificate.

greg0ire avatar Dec 17 '14 12:12 greg0ire

Oh I see. I will try to investigate later on. Otherwise hopefully someone else has the answer?

eddiejaoude avatar Dec 17 '14 12:12 eddiejaoude

According to Guzzle default code, it will check the Cert...

protected function getDefaultOptions()
    {
        $settings = [
            'allow_redirects' => true,
            'exceptions'      => true,
            'decode_content'  => true,
            'verify'          => __DIR__ . '/cacert.pem'
        ];

        // Use the bundled cacert if it is a regular file, or set to true if
        // using a phar file (because curL and the stream wrapper can't read
        // cacerts from the phar stream wrapper). Favor the ini setting over
        // the system's cacert.
        if (substr(__FILE__, 0, 7) == 'phar://') {
            $settings['verify'] = ini_get('openssl.cafile') ?: true;
        }

        // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set
        if ($proxy = getenv('HTTP_PROXY')) {
            $settings['proxy']['http'] = $proxy;
        }

        if ($proxy = getenv('HTTPS_PROXY')) {
            $settings['proxy']['https'] = $proxy;
        }

        return $settings;
    }

eddiejaoude avatar Dec 17 '14 12:12 eddiejaoude

When the Guzzle Client is created in the Extension, you can pass it config....

public function load(ContainerBuilder $container, array $config)
    {
        $this->loadClient($container, $config);
        $this->loadContextInitializer($container, $config);
    }

    private function loadClient(ContainerBuilder $container, $config)
    {
        $definition = new Definition('GuzzleHttp\Client', array($config));
        $container->setDefinition(self::CLIENT_ID, $definition);
    }

Hope that helps a bit

eddiejaoude avatar Dec 17 '14 12:12 eddiejaoude

@eddiejaoude : I saw that, but it doesn't help because $config is being validated in the method above load(), and as you can see, for the moment, only base_url is allowed in config. Nothing else. This is the point of this issue, I think maybe more things should be allowed.

greg0ire avatar Dec 17 '14 13:12 greg0ire

Here is my work around (to set the allow_redirects setting):

<?php

namespace Escapee\CoreBundle\Behat;

use Behat\WebApiExtension\ServiceContainer\WebApiExtension as BaseWebApiExtension;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

class WebApiExtension extends BaseWebApiExtension
{
    /**
     * {@inheritdoc}
     */
    public function configure(ArrayNodeDefinition $builder)
    {
        parent::configure($builder);

        $builder
            ->children()
                ->arrayNode('defaults')
                    ->children()
                        ->scalarNode('allow_redirects')
                            ->defaultTrue()
                        ->end()
                    ->end()
                ->end()
            ->end();
    }
}

behat.yml:

default:
    formatters:
        progress: ~
    extensions:
        Escapee\CoreBundle\Behat\WebApiExtension:
            defaults:
                allow_redirects: false

noetix avatar May 06 '15 04:05 noetix

Note, I now use https://github.com/teaandcode/behat-guzzle-extension

eddiejaoude avatar Jun 13 '15 12:06 eddiejaoude

@stof : I made #32 to solve the particular ssl problem, that must be quite common.

greg0ire avatar Jun 26 '15 10:06 greg0ire