<commit_before><?php

namespace Bugsnag\Tests;

use Bugsnag\Configuration;
use Bugsnag\Error;
use Bugsnag\HttpClient;
use Exception;
use GuzzleHttp\Client;
use phpmock\phpunit\PHPMock;
use PHPUnit_Framework_TestCase as TestCase;

class HttpClientTest extends TestCase
{
    use PHPMock;

    protected $config;
    protected $guzzle;
    protected $http;

    protected function setUp()
    {
        $this->config = new Configuration('<KEY>');

        $this->guzzle = $this->getMockBuilder(Client::class)
                             ->setMethods(['request'])
                             ->getMock();

        $this->http = new HttpClient($this->config, $this->guzzle);
    }

    public function testHttpClient()
    {
        // Expect request to be called
        $this->guzzle->expects($spy = $this->any())->method('request');

        // Add an error to the http and deliver it
        $this->http->queue(Error::fromNamedError($this->config, 'Name')->setMetaData(['foo' => 'bar']));
        $this->http->send();

        $this->assertCount(1, $invocations = $spy->getInvocations());
        $params = $invocations[0]->parameters;
        $this->assertCount(3, $params);
        $this->assertSame('POST', $params[0]);
        $this->assertSame('/', $params[1]);
        $this->assertInternalType('array', $params[2]);
        $this->assertSame('<KEY>', $params[2]['json']['apiKey']);
        $this->assertInternalType('array', $params[2]['json']['notifier']);
        $this->assertInternalType('array', $params[2]['json']['events']);
        $this->assertSame([], $params[2]['json']['events'][0]['user']);
        $this->assertSame(['foo' => 'bar'], $params[2]['json']['events'][0]['metaData']);
    }

    public function testMassiveMetaDataHttpClient()
    {
        // Expect request to be called
        $this->guzzle->expects($spy = $this->any())->method('request');

        // Add an error to the http and deliver it
        $this->http->queue(Error::fromNamedError($this->config, 'Name')->setMetaData(['foo' => str_repeat('A', 1000000)]));
        $this->http->send();

        $this->assertCount(1, $invocations = $spy->getInvocations());
        $params = $invocations[0]->parameters;
        $this->assertCount(3, $params);
        $this->assertSame('POST', $params[0]);
        $this->assertSame('/', $params[1]);
        $this->assertInternalType('array', $params[2]);
        $this->assertSame('<KEY>', $params[2]['json']['apiKey']);<commit_msg>Test multiple sends don't break (#261)<commit_after><?php

namespace Bugsnag\Tests;

use Bugsnag\Configuration;
use Bugsnag\Error;
use Bugsnag\HttpClient;
use Exception;
use GuzzleHttp\Client;
use phpmock\phpunit\PHPMock;
use PHPUnit_Framework_TestCase as TestCase;

class HttpClientTest extends TestCase
{
    use PHPMock;

    protected $config;
    protected $guzzle;
    protected $http;

    protected function setUp()
    {
        $this->config = new Configuration('6015a72ff14038114c3d12623dfb018f');

        $this->guzzle = $this->getMockBuilder(Client::class)
                             ->setMethods(['request'])
                             ->getMock();

        $this->http = new HttpClient($this->config, $this->guzzle);
    }

    public function testHttpClient()
    {
        // Expect request to be called
        $this->guzzle->expects($spy = $this->any())->method('request');

        // Add an error to the http and deliver it
        $this->http->queue(Error::fromNamedError($this->config, 'Name')->setMetaData(['foo' => 'bar']));
        $this->http->send();

        $this->assertCount(1, $invocations = $spy->getInvocations());
        $params = $invocations[0]->parameters;
        $this->assertCount(3, $params);
        $this->assertSame('POST', $params[0]);
        $this->assertSame('/', $params[1]);
        $this->assertInternalType('array', $params[2]);
        $this->assertSame('<KEY>', $params[2]['json']['apiKey']);
        $this->assertInternalType('array', $params[2]['json']['notifier']);
        $this->assertInternalType('array', $params[2]['json']['events']);
        $this->assertSame([], $params[2]['json']['events'][0]['user']);
        $this->assertSame(['foo' => 'bar'], $params[2]['json']['events'][0]['metaData']);
    }

    public function testHttpClientMultipleSend()
    {
        // Expect request to be called
        $this->guzzle->expects($spy = $this->any())->method('request');

        // Add an error to the http and deliver it
        $this->http->queue(Error::fromNamedError($this->config, 'Name')->setMetaData(['foo' => 'bar']));
        $this->http->send();

        // Make sure these do nothing
        $this->http->send();
        $this->http->send();

        // Check we only sent once
        $this->assertCount(1, $invocations = $spy->getInvocations());
    }

    public function testMassiveMetaDataHttpClient()
    {
        // Expect request to be called
        $this->guzzle->expects($spy = $this->any())->method('request');

        // Add an error to the http and deliver it
        $this->http->queue(Error::fromNamedError($this->config, 'Name')->setMetaData(['foo' => str_repeat('A', 1000000)]));
        $this->http->send();

        $this->assertCount(1, $invocations = $spy->getInvocations());
        $params = $invocations[0]->parameters;
        $this->assertCount(3, $params);
        $this->assertSame('POST', $params[0]);
        $this->assertSame('/', $params[1]);
        $this->assertInternalType('array', $params[2]);
        $this->assertSame('<KEY>', $params[2]['json']['apiKey']);
{'fraction_non_alphanumeric': 0.1963577353047241, 'fraction_numerical': 0.014605120807789397, 'mean_word_length': 3.1364653243847873, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6685615', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1636, 'n_words': 304}