Behat / WebApiExtension

Behat extension to test web APIs
http://extensions.behat.org
MIT License
111 stars 114 forks source link

Is it possible to configure the guzzle client ? #18

Open greg0ire opened 9 years ago

greg0ire commented 9 years ago

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 commented 9 years ago

How to configure the url from localhost:80?

eddiejaoude commented 9 years ago

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

Behat\WebApiExtension:
        base_url: http://localhost:8000
greg0ire commented 9 years ago

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

eddiejaoude commented 9 years ago

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 commented 9 years ago

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

eddiejaoude commented 9 years ago

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

eddiejaoude commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

@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.

noetix commented 9 years ago

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 commented 9 years ago

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

greg0ire commented 9 years ago

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