File: 501-example-env-resource.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 (251 lines) | stat: -rw-r--r-- 10,166 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/perl

use strict;
use warnings;
use FindBin;

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

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

use HTTP::Request::Common qw[ GET HEAD PUT POST DELETE ];

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

test_psgi
    Plack::Util::load_psgi( "$FindBin::Bin/../examples/env-resource/app.psgi" ),
    sub {
        my $cb   = shift;
        my $JSON = JSON::XS->new->allow_nonref;

        # NOTE:
        # we won't test Content-Length in here
        # because that will change based on the
        # contents of ENV, which are not static.
        # - SL

        {
            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_deeply(
                $JSON->decode( $res->content ),
                \%ENV,
                '... got the expected content'
            );
        }

        # test affecting the ENV

        {
            my $res = $cb->(GET "/WEB_MACHINE_TESTING");
            is($res->code, 404, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Not Found', '... got the expected content');
        }

        $ENV{'WEB_MACHINE_TESTING'} = __FILE__;

        {
            my $res = $cb->(GET "/WEB_MACHINE_TESTING");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is_deeply(
                $JSON->decode( $res->content ),
                __FILE__,
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(DELETE "/WEB_MACHINE_TESTING");
            is($res->code, 204, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->content, '', '... got the expected content');
        }

        {
            my $res = $cb->(GET "/WEB_MACHINE_TESTING");
            is($res->code, 404, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Not Found', '... got the expected content');
        }

        # now through the web-service

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING");
            is($res->code, 404, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Not Found', '... got the expected content');
        }

        {
            my $res = $cb->(PUT "/WEB_MACHINE_AUTOMATED_TESTING", (
                'Content-Type' => 'application/json', 'Content' => '"FOOBAR"'
            ));
            is($res->code, 204, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->content, '', '... got the expected content');
        }

        ok(exists $ENV{'WEB_MACHINE_AUTOMATED_TESTING'}, '... the variable exists now');
        is($ENV{'WEB_MACHINE_AUTOMATED_TESTING'}, 'FOOBAR', '... the variable has the value we want');

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is_deeply(
                $JSON->decode( $res->content ),
                "FOOBAR",
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(DELETE "/WEB_MACHINE_AUTOMATED_TESTING");
            is($res->code, 204, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->content, '', '... got the expected content');
        }

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING");
            is($res->code, 404, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Not Found', '... got the expected content');
        }

        # test loading multiples

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING_BULK_FOO");
            is($res->code, 404, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Not Found', '... got the expected content');
        }

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING_BULK_BAR");
            is($res->code, 404, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Not Found', '... got the expected content');
        }

        {
            my $res = $cb->(PUT "/", (
                'Content-Type' => 'application/json',
                'Content' => $JSON->encode({
                    WEB_MACHINE_AUTOMATED_TESTING_BULK_FOO => 'FOO',
                    WEB_MACHINE_AUTOMATED_TESTING_BULK_BAR => 'BAR',
                })
            ));
            is($res->code, 204, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->content, '', '... got the expected content');
        }

        ok(exists $ENV{'WEB_MACHINE_AUTOMATED_TESTING_BULK_FOO'}, '... the variable exists now');
        is($ENV{'WEB_MACHINE_AUTOMATED_TESTING_BULK_FOO'}, 'FOO', '... the variable has the value we want');

        ok(exists $ENV{'WEB_MACHINE_AUTOMATED_TESTING_BULK_BAR'}, '... the variable exists now');
        is($ENV{'WEB_MACHINE_AUTOMATED_TESTING_BULK_BAR'}, 'BAR', '... the variable has the value we want');

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING_BULK_FOO");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is_deeply(
                $JSON->decode( $res->content ),
                "FOO",
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING_BULK_BAR");
            is($res->code, 200, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is_deeply(
                $JSON->decode( $res->content ),
                "BAR",
                '... got the expected content'
            );
        }

        {
            my $res = $cb->(DELETE "/WEB_MACHINE_AUTOMATED_TESTING_BULK_FOO");
            is($res->code, 204, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->content, '', '... got the expected content');
        }

        {
            my $res = $cb->(DELETE "/WEB_MACHINE_AUTOMATED_TESTING_BULK_BAR");
            is($res->code, 204, '... got the expected status');
            is($res->header('Content-Type'), 'application/json', '... got the expected Content-Type header');
            is($res->content, '', '... got the expected content');
        }

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING_BULK_FOO");
            is($res->code, 404, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Not Found', '... got the expected content');
        }

        {
            my $res = $cb->(GET "/WEB_MACHINE_AUTOMATED_TESTING_BULK_BAR");
            is($res->code, 404, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Not Found', '... got the expected content');
        }

        ## check some of the expected errors ...

        {
            my $res = $cb->(POST "/");
            is($res->code, 405, '... got the expected status');
            is($res->header('Allow'), 'GET, HEAD, PUT', '... got the expected Allow header');
            is($res->content, 'Method Not Allowed', '... got the expected content');
        }

        {
            my $res = $cb->(DELETE "/");
            is($res->code, 405, '... got the expected status');
            is($res->header('Allow'), 'GET, HEAD, PUT', '... got the expected Allow header');
            is($res->content, 'Method Not Allowed', '... got the expected content');
        }

        {
            my $res = $cb->(POST "/FOO");
            is($res->code, 405, '... got the expected status');
            is($res->header('Allow'), 'GET, HEAD, PUT, DELETE', '... got the expected Allow header');
            is($res->content, 'Method Not Allowed', '... got the expected content');
        }

        {
            my $res = $cb->(PUT "/WEB_MACHINE_AUTOMATED_TESTING", (
                'Content-Type' => 'application/xml', 'Content' => '<FOOBAR/>'
            ));
            is($res->code, 415, '... got the expected status');
            is($res->header('Content-Type'), 'text/plain', '... got the expected Content-Type header');
            is($res->content, 'Unsupported Media Type', '... got the expected content');
        }

        {
            my $res = $cb->(GET "/", 'Accept' => 'text/html');
            is($res->code, 406, '... got the expected status');
            is($res->content, 'Not Acceptable', '... got the expected content');
        }

    };

done_testing;