### ----------------------------------------------------
### -- HTTP::Tinyish
### -- Licenses: Artistic-2.0
### -- Authors: Shoichi Kaji
### -- File: xt/02-binary.rakutest
### ----------------------------------------------------
use HTTP::Tinyish;
use Test;
use File::Temp;
use JSON::Pretty;

my $HTTBIN_HOST = %*ENV<HTTPBIN_HOST> || "httpbin.org";

subtest {
    my $url = "https://raku.org/camelia-logo.png";
    my %res = HTTP::Tinyish.new.get($url, :bin);
    is %res<status>, 200;
    my $buf = %res<content>;
    does-ok $buf, Buf;
    my $file-type = $buf.subbuf(1, 3).decode;
    is $file-type, "PNG";
};

subtest {
    my $url = "http://$HTTBIN_HOST/put";
    my %res = HTTP::Tinyish.new.put: $url,
        headers => { 'Content-Type' => 'text/plain; charset=utf-8' },
        content => "あいうえお".encode,
    ;
    is %res<status>, 200;
    isa-ok %res<content>, Str;
    is from-json(%res<content>)<data>, "あいうえお";
};

subtest {
    my $url = "http://$HTTBIN_HOST/put";
    my %res = HTTP::Tinyish.new.put: $url,
        headers => { 'Content-Type' => 'text/plain; charset=utf-8' },
        content => "あいうえお".encode,
        bin     => True,
    ;
    is %res<status>, 200;
    does-ok %res<content>, Buf;
    is from-json(%res<content>.decode)<data>, "あいうえお";
};

subtest {
    my $url = "http://foo.bar.example.com/does-not-exists";
    my %res = HTTP::Tinyish.new.get($url, :bin);
    is %res<status>, 599;
    does-ok %res<content>, Buf;
};

done-testing;


