#!/usr/bin/perl

use strict;
use warnings;

BEGIN { delete $ENV{http_proxy} }

use Plack::Test;
use HTTP::Request;
use Test::More;
use Test::TCP;

if ($^O eq 'MSWin32' and $] >= 5.016 and $] < 5.019005 and not $ENV{PERL_TEST_BROKEN}) {
    plan skip_all => 'Perl with bug RT#119003 on MSWin32';
    exit 0;
}

if ($^O eq 'cygwin' and not eval { require Win32::Process; }) {
    plan skip_all => 'Win32::Process required';
    exit 0;
}

$Plack::Test::Impl = "Server";
$ENV{PLACK_SERVER} = 'Starlight';
$ENV{PLACK_QUIET} = 1;

my $app = sub {
    my $env = shift;
    return sub {
        my $response = shift;
        my $writer = $response->([200, ['Content-Type', 'text/plain']]);
        $writer->write("Content");
        $writer->write("");
        $writer->write("Again");
        $writer->close;
    }
};

test_psgi $app, sub {
    my $cb = shift;

    sleep 1;

    my $req = HTTP::Request->new(GET => "http://127.0.0.1/");
    my $res = $cb->($req);

    is $res->content, "ContentAgain", 'content';

    sleep 1;
};

done_testing;
