### ----------------------------------------------------
### -- Humming-Bird
### -- Licenses: MIT
### -- Authors: Rawley Fowler
### -- File: t/02-request_encoding.rakutest
### ----------------------------------------------------
use v6;
use strict;
use lib 'lib';

use Test;
use Humming-Bird::Core;
use Humming-Bird::Glue;

plan 22;

my $simple_raw_request = "GET / HTTP/1.1\r\nHost: bob.com\r\n";
my $simple_request = Request.decode($simple_raw_request);

ok $simple_request.method === GET, 'Is method OK?';
is $simple_request.version, 'HTTP/1.1', 'Is version OK?';
is $simple_request.path, '/', 'Is path OK?';

my $simple_header_raw_request = "GET /bob HTTP/1.1\r\nAccepted-Encoding: utf-8\r\nHost: bob.com\r\n\r\n";
my $simple_header_request = Request.decode($simple_header_raw_request);

ok $simple_header_request.method === GET, 'Is method for header request OK?';
is $simple_header_request.header('Accepted-Encoding'), 'utf-8', 'Is header OK?';

my $many_header_raw_request = "GET /bob HTTP/1.1\r\nAccepted-Encoding: utf-8\r\nAccept-Language: en-US\r\nConnection: keep-alive\r\nHost: bob.com\r\n\r\n";
my $many_header_request = Request.decode($many_header_raw_request);

is $many_header_request.header('Accepted-Encoding'), 'utf-8', 'Is header 1 OK?';
is $many_header_request.header('Accept-Language'), 'en-US', 'Is header 2 OK?';
is $many_header_request.header('Connection'), 'keep-alive', 'Is header 3 OK?';

dies-ok({ Request.decode: 'POST / HTTP/1.1\r\nHost: bob.com\r\nContent-Type: application/json\r\nChunked-Encoding: yes\r\n\r\n123' }, 'Does chunked encoding die?');

my $body = 'aaaaaaaaaa';
my $simple_post_raw_request = "POST / HTTP/1.1\r\nHost: bob.com\r\nContent-Type: application/json\r\nContent-Length: { $body.chars }\r\n\r\n$body";
my $simple_post_request = Request.decode($simple_post_raw_request);

is $simple_post_request.header('Host'), 'bob.com';
is $simple_post_request.header('Content-Type'), 'application/json';
is $simple_post_request.header('Content-Length'), $body.chars;
is $simple_post_request.method, POST;

is $simple_post_request.body.decode('latin-1'), $body, 'Is post body OK?';

my $simple_post_empty_raw_request = "POST / HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: 0\r\nHost: bob.com\r\n\r\n";
my $simple_post_empty_request = Request.decode($simple_post_empty_raw_request);

is $simple_post_empty_request.body.decode, '', 'Is empty post body OK?';

my $simple-absolute-uri-raw-request = "POST http://localhost/ HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: { $body.chars }\r\n\r\n$body";
my $simple-absolute-uri-request = Request.decode($simple-absolute-uri-raw-request);
is $simple-absolute-uri-request.body.decode, $body, 'Is absolute URI body OK?';
is $simple-absolute-uri-request.header('Host'), 'localhost', 'Is absolute URI host header OK?';
is $simple-absolute-uri-request.path, '/', 'Is absolute URI path OK?';

my $complex-absolute-uri-raw-request = "POST http://localhost/name/person?bob=123 HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: { $body.chars }\r\n\r\n$body";
my $complex-absolute-uri-request = Request.decode($complex-absolute-uri-raw-request);
is $complex-absolute-uri-request.body.decode, $body, 'Is absolute URI body OK?';
is $complex-absolute-uri-request.header('Host'), 'localhost', 'Is absolute URI host header OK?';
is $complex-absolute-uri-request.path, '/name/person', 'Is absolute URI path OK?';
is $complex-absolute-uri-request.query('bob'), '123', 'Is query param OK?';


