File: 021-post-w-bypass-n11.t

package info (click to toggle)
libweb-machine-perl 0.17-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 940 kB
  • sloc: perl: 5,481; makefile: 2
file content (170 lines) | stat: -rw-r--r-- 5,196 bytes parent folder | download | duplicates (5)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl

use strict;
use warnings;
use FindBin;

use Test::More;
use Test::Fatal;

BEGIN {
    if (!eval { require JSON::XS; 1 }) {
        plan skip_all => "JSON::XS is required for this test";
    }
}

use Plack::Test;
use Plack::Util;

use HTTP::Request::Common;

use Web::Machine;

my %DB;
{
    package My::Resource::Test021;
    use strict;
    use warnings;

    use Web::Machine::Util qw[ bind_path ];
    use JSON::XS qw[ encode_json decode_json ];

    use parent 'Web::Machine::Resource';

    sub current_id {
        my $self = shift;
        $self->{'current_id'} = shift if @_;
        $self->{'current_id'}
    }

    sub allowed_methods        { [qw[ GET POST ]] }
    sub content_types_provided { [ { 'application/json' => 'to_json'   } ] }
    sub content_types_accepted { [ { 'application/json' => 'from_json' } ] }

    sub create_path_after_handler { 1 }

    sub post_is_create { 1 }
    sub base_uri       { '/' }
    sub create_path    { (shift)->current_id }

    sub to_json {
        my $self = shift;
        if ( my $id = bind_path( '/:id', $self->request->path_info ) ) {
            encode_json( $DB{ $id } )
        } else {
            encode_json([ map { $DB{ $_ } } sort keys %DB ])
        }
    }

    sub from_json {
        my $self = shift;
        my $data = decode_json( $self->request->content );
        $DB{ $data->{'id'} } = $data;
        $self->current_id( $data->{'id'} );
        return;
    }
}

test_psgi(
    Web::Machine->new( resource => 'My::Resource::Test021' )->to_app,
    sub {
        my $cb  = shift;

        {
            my $res = $cb->(GET "/");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->header('Content-Length'), 2, '... got the expected Content-Length header');
            is(
                $res->content,
                '[]',
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(
                POST "/", (
                    'Content-Type' => 'application/json',
                    'Content'      => '{"id":"bar"}'
                )
            );
            is($res->code, 201, '... got the expected status');
            is($res->header('Location'), '/bar', '... got the expected Location header');
        }

        {
            my $res = $cb->(GET "/");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->header('Content-Length'), 14, '... got the expected Content-Length header');
            is(
                $res->content,
                '[{"id":"bar"}]',
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(GET "/bar");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->header('Content-Length'), 12, '... got the expected Content-Length header');
            is(
                $res->content,
                '{"id":"bar"}',
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(
                POST "/", (
                    'Content-Type' => 'application/json',
                    'Content'      => '{"id":"baz"}'
                )
            );
            is($res->code, 201, '... got the expected status');
            is($res->header('Location'), '/baz', '... got the expected Location header');
        }

        {
            my $res = $cb->(GET "/");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->header('Content-Length'), 27, '... got the expected Content-Length header');
            is(
                $res->content,
                '[{"id":"bar"},{"id":"baz"}]',
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(GET "/baz");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->header('Content-Length'), 12, '... got the expected Content-Length header');
            is(
                $res->content,
                '{"id":"baz"}',
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(GET "/bar");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->header('Content-Length'), 12, '... got the expected Content-Length header');
            is(
                $res->content,
                '{"id":"bar"}',
                '... got the expected content'
            );
        }

    }
);

done_testing;