<?php

namespace NoahVet\Reef\Test\A_Unit\Domain\Http\Multipart;

use NoahVet\Reef\Domain\Http\Multipart\HeaderValueParser;
use PHPUnit\Framework\TestCase;

class HeaderValueParserTest extends TestCase
{
    private HeaderValueParser $parser;

    protected function setUp(): void
    {
        $this->parser = new HeaderValueParser();
    }

    public function testGetValueSimple(): void
    {
        self::assertEquals('form-data', $this->parser->getValue('form-data; name="foo"'));
    }

    public function testGetValueNoOptions(): void
    {
        self::assertEquals('text/plain', $this->parser->getValue('text/plain'));
    }

    public function testGetValueNull(): void
    {
        self::assertEquals('', $this->parser->getValue(null));
    }

    public function testGetOptions(): void
    {
        $options = $this->parser->getOptions('form-data; name="foo"; filename="bar.txt"');

        self::assertEquals('foo', $options['name']);
        self::assertEquals('bar.txt', $options['filename']);
    }

    public function testGetOptionExists(): void
    {
        self::assertEquals('foo', $this->parser->getOption('form-data; name="foo"', 'name'));
    }

    public function testGetOptionMissing(): void
    {
        self::assertNull($this->parser->getOption('form-data', 'name'));
    }

    public function testGetOptionDefault(): void
    {
        self::assertEquals('default', $this->parser->getOption('form-data', 'name', 'default'));
    }

    public function testRfc5987Iso88591(): void
    {
        $header = "form-data; text1*=iso-8859-1'en'%A3%20rates";
        self::assertEquals('£ rates', $this->parser->getOption($header, 'text1'));
    }

    public function testRfc5987Utf8(): void
    {
        $header = "form-data; text2*=UTF-8''%c2%a3%20and%20%e2%82%ac%20rates";
        self::assertEquals('£ and € rates', $this->parser->getOption($header, 'text2'));
    }

    public function testBoundaryOption(): void
    {
        $header = 'multipart/form-data; boundary=----boundary123';
        self::assertEquals('----boundary123', $this->parser->getOption($header, 'boundary'));
    }

    public function testCharsetOption(): void
    {
        $header = 'text/html; charset=iso-8859-1';
        self::assertEquals('iso-8859-1', $this->parser->getOption($header, 'charset'));
    }
}
