about summary refs log tree commit diff
path: root/scripts/post_to_hook.pl
blob: 55d3d735dfdac1935a949ba9245676562c1fb2de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use strict;
use warnings;

use jitterbug;
use jitterbug::Schema;

use JSON;
use YAML qw/LoadFile Dump/;

use File::Temp qw/tempdir/;

use Dancer::Test;
use Dancer::Config qw/setting/;
use File::Spec::Functions;

my $content = LoadFile(shift || catfile(qw/t data hook_data.yml/));

my $db_file = catfile( qw/t data jitterbug.db/ );
my $dsn     = 'dbi:SQLite:dbname=' . $db_file;
my $schema  = jitterbug::Schema->connect($dsn);
# assume we have a deployed schema
# $schema->deploy;

setting plugins => {
    DBIC => {
        schema => {
            skip_automake => 1,
            pckg          => "jitterbug::Schema",
            connect_info  => [$dsn]
        }
    }
};

{
    my $response = dancer_response(
        POST => '/hook/',
        {
            headers =>
              [ 'Content-Type' => 'application/x-www-form-urlencoded' ],
            body => _generate_post_request($content),
        }
    );

    printf "Response was: %s\n",  $response->{status};
}

sub _generate_post_request {
    my $content = shift;
    my $payload = "payload=" . JSON::encode_json($content);
    open my $in, '<', \$payload;

    $ENV{'CONTENT_LENGTH'} = length($payload);
    $ENV{'CONTENT_TYPE'}   = 'application/x-www-form-urlencoded';
    $ENV{'psgi.input'}     = $in;
    return $payload;
}