<commit_before>use v6.c;

use Test;
use URI;

use Bailador;
use Bailador::Request;


unit module Bailador::Test;

my class ErrorBuffer does Stringy {
    has @.buf;
    method Str()  { @.buf.join: '' }
    method gist() { self.Str }
    method wipe() { @.buf.splice(0, @.buf.elems).join: '' }
    method add(Str:D $data) { @.buf.push($data) }
}

my class IO::Null is IO::Handle {
    has ErrorBuffer $.error-buf;

    multi method print(Str:D \string ) { self!add-string( string ) }
    multi method print(**@args is raw) { self!add-string( join '', @args.map: *.Str ) }

    method print-nl { self.add-string( $.nl-out ) }

    multi method put(Str:D \string)  { self!add-string( string ~ $.nl-out ) }
    multi method put(**@args is raw) {
        self!add-string( join '', @args.map(*.Str), $.nl-out )
    }

    multi method say(Str:D \string)  { self!add-string( string ~ $.nl-out ) }
    multi method say(**@args is raw) {
        self!add-string( join '', @args.map(*.gist), $.nl-out )
    }

    method !add-string(Str:D $str) {
        if $.error-buf {
            $.error-buf.add($str);
        } else {
            diag $str;
        }
    }
}

sub run-psgi-request(Callable $app, $meth, $url, $data = '', :%headers ) is export {
    my $error-buf = ErrorBuffer.new;
    my $response  = get-psgi-response($app, $meth, $url, $data, :%headers, :$error-buf),
<commit_msg>Ticket #185 - Let run-psgi-request accept %Hash<commit_after>use v6.c;

use Test;
use URI;

use Bailador;
use Bailador::Request;


unit module Bailador::Test;

my class ErrorBuffer does Stringy {
    has @.buf;
    method Str()  { @.buf.join: '' }
    method gist() { self.Str }
    method wipe() { @.buf.splice(0, @.buf.elems).join: '' }
    method add(Str:D $data) { @.buf.push($data) }
}

my class IO::Null is IO::Handle {
    has ErrorBuffer $.error-buf;

    multi method print(Str:D \string ) { self!add-string( string ) }
    multi method print(**@args is raw) { self!add-string( join '', @args.map: *.Str ) }

    method print-nl { self.add-string( $.nl-out ) }

    multi method put(Str:D \string)  { self!add-string( string ~ $.nl-out ) }
    multi method put(**@args is raw) {
        self!add-string( join '', @args.map(*.Str), $.nl-out )
    }

    multi method say(Str:D \string)  { self!add-string( string ~ $.nl-out ) }
    multi method say(**@args is raw) {
        self!add-string( join '', @args.map(*.gist), $.nl-out )
    }

    method !add-string(Str:D $str) {
        if $.error-buf {
            $.error-buf.add($str);
        } else {
            diag $str;
        }
    }
}

multi sub run-psgi-request(Callable $app, $meth, $url, $data = '', :%headers ) is export {
    my $error-buf = ErrorBuffer.new;
    my $response  = get-psgi-response($app, $meth, $url, $data, :%headers, :$error-buf),

    return {
        err      => $error-buf.Str,
        response => $response;
    };
}

multi sub run-psgi-request(Callable $app, $meth, $url, %data, :%headers ) is export {
    my Str $data = '';
    my Int $n = 0;
    my Int $elems = %data.elems;
    for %data.kv -> $key, $value {
        $n++;
        $data ~= $key ~ '=' ~ $value ~ '&' if $n < $elems;
        $data ~= $key ~ '=' ~ $value if $n == $elems;
    }
    $data .= substr(/\h/, '%20');

    my $error-buf = ErrorBuffer.new;
    my $response  = get-psgi-response($app, $meth, $url, $data, :%headers, :$error-buf),

{'fraction_non_alphanumeric': 0.19118082272861794, 'fraction_numerical': 0.002959455460195324, 'mean_word_length': 2.470225872689938, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '6303125', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1235, 'n_words': 307}