<commit_before>use strict;
use warnings;
use Test::More;
use Test::Exception;
use t::Util;
use JSON;

plan skip_all => 'curl not found'
    unless prog_exists('curl');

subtest 'minimum' => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    http2-debug-state: minimum
    paths:
      /:
        mruby.handler: proc {|env| [399, {}, [] ] }
        file.dir: @{[ DOC_ROOT ]}
EOT

    run_with_curl($server, sub {
        my ($proto, $port, $curl_cmd) = @_;
        $curl_cmd .= " --silent --show-error";
        my $url = "$proto://127.0.0.1:$port/.well-known/h2/state";

        if ($curl_cmd =~ /--http2/) {
            subtest "single stream itself" => sub {
                my ($headers, $body) = run_prog("$curl_cmd --dump-header /dev/stderr $url");
                like($headers, qr!^HTTP/2 200!);
                my $data;
                lives_ok { $data = decode_json($body) };
                is($data->{streams}->{1}->{state}, 'HALF_CLOSED_REMOTE');
            };
        } else {
            subtest "return_404_when_http1" => sub {
                my ($headers, $body) = run_prog("$curl_cmd --dump-header /dev/stderr $url");
                like($headers, qr!^HTTP/1.1 404!);
            };
        }
    });
};

subtest 'hpack' => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:<commit_msg>fix test error in case curl is built without H2 support<commit_after>use strict;
use warnings;
use Test::More;
use Test::Exception;
use t::Util;
use JSON;

plan skip_all => 'curl not found'
    unless prog_exists('curl');

subtest 'minimum' => sub {
    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
    http2-debug-state: minimum
    paths:
      /:
        mruby.handler: proc {|env| [399, {}, [] ] }
        file.dir: @{[ DOC_ROOT ]}
EOT

    run_with_curl($server, sub {
        my ($proto, $port, $curl_cmd) = @_;
        $curl_cmd .= " --silent --show-error";
        my $url = "$proto://127.0.0.1:$port/.well-known/h2/state";

        if ($curl_cmd =~ /--http2/) {
            subtest "single stream itself" => sub {
                my ($headers, $body) = run_prog("$curl_cmd --dump-header /dev/stderr $url");
                like($headers, qr!^HTTP/2 200!);
                my $data;
                lives_ok { $data = decode_json($body) };
                is($data->{streams}->{1}->{state}, 'HALF_CLOSED_REMOTE');
            };
        } else {
            subtest "return_404_when_http1" => sub {
                my ($headers, $body) = run_prog("$curl_cmd --dump-header /dev/stderr $url");
                like($headers, qr!^HTTP/1.1 404!);
            };
        }
    });
};

subtest 'hpack' => sub {
    plan skip_all => "curl does not support HTTP/2"
        unless curl_supports_http2();

    my $server = spawn_h2o(<< "EOT");
hosts:
  default:
{'fraction_non_alphanumeric': 0.17649160414433726, 'fraction_numerical': 0.021078956770275097, 'mean_word_length': 2.040173724212812, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4788558', 'n_tokens_mistral': 1039, 'n_tokens_neox': 958, 'n_words': 214}