Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to configure the guzzle client ? #18

Open
greg0ire opened this issue Dec 16, 2014 · 12 comments
Open

Is it possible to configure the guzzle client ? #18

greg0ire opened this issue Dec 16, 2014 · 12 comments

Comments

@greg0ire
Copy link
Contributor

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.

@eddiejaoude
Copy link

How to configure the url from localhost:80?

@eddiejaoude
Copy link

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

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

@greg0ire
Copy link
Contributor Author

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

@eddiejaoude
Copy link

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

@greg0ire
Copy link
Contributor Author

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

@eddiejaoude
Copy link

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

@eddiejaoude
Copy link

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
Copy link

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

@greg0ire
Copy link
Contributor Author

@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 greg0ire changed the title Is it possible to configure the guzzle client Is it possible to configure the guzzle client ? Jan 19, 2015
@noetix
Copy link

noetix commented May 6, 2015

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

@eddiejaoude
Copy link

@greg0ire
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants