File Coverage

blib/lib/Web/Solid/Test/Basic.pm
Criterion Covered Total %
statement 38 104 36.5
branch 0 4 0.0
condition n/a
subroutine 13 20 65.0
pod 4 5 80.0
total 55 133 41.3


line stmt bran cond sub pod time code
1             package Web::Solid::Test::Basic;
2              
3 1     1   648 use 5.010001;
  1         3  
4 1     1   5 use strict;
  1         1  
  1         52  
5 1     1   5 use warnings;
  1         2  
  1         34  
6 1     1   388 use parent 'Test::FITesque::Fixture';
  1         252  
  1         4  
7 1     1   1615 use Test::More ;
  1         2  
  1         6  
8 1     1   1531 use LWP::UserAgent;
  1         40025  
  1         64  
9 1     1   1123 use Test::Deep;
  1         8247  
  1         6  
10 1     1   632 use Test::RDF;
  1         1198162  
  1         10  
11              
12             our $AUTHORITY = 'cpan:KJETILK';
13             our $VERSION = '0.011_01';
14              
15             sub http_read_unauthenticated : Test : Plan(4) {
16 0     0 1 0 my ($self, $args) = @_;
17 0         0 my $ua = LWP::UserAgent->new;
18 0         0 my $url = $args->{url};
19 0         0 my $reshead = $ua->head( $url );
20 0         0 note($args->{description});
21 0         0 ok($reshead->is_success, "Successful HEAD request for $url");
22              
23 0         0 my $resget = $ua->get( $url );
24 0         0 ok($resget->is_success, "Successful GET request for $url");
25              
26 0         0 my @head_headers_fields = $reshead->headers->header_field_names;
27 0         0 my @get_headers_fields = $resget->headers->header_field_names;
28 0         0 cmp_bag(\@head_headers_fields, \@get_headers_fields, "HEAD and GET request has the same header fields");
29              
30 0         0 @get_headers_fields = grep { !/Date/ } @get_headers_fields; # Do not test date-fields since they may change between HEAD and GET
  0         0  
31 0         0 @get_headers_fields = grep { !/^Client/ } @get_headers_fields; # Do not test client-side added fields
  0         0  
32              
33             subtest 'Testing all headers' => sub {
34 0     0   0 plan tests => scalar @get_headers_fields;
35 0         0 foreach my $get_header_field (@get_headers_fields) {
36 0         0 is($resget->header($get_header_field), $reshead->header($get_header_field), "$get_header_field is the same for both");
37             }
38 0         0 };
39              
40 1     1   395 }
  1         23  
  1         8  
41              
42              
43             sub http_check_header_unauthenticated : Test : Plan(2) {
44 0     0 0 0 my ($self, $args) = @_;
45 0         0 my $ua = LWP::UserAgent->new;
46 0         0 my $url = $args->{url};
47 0         0 note($args->{description});
48 0         0 delete $args->{url};
49 0         0 delete $args->{description};
50 0         0 my $reshead = $ua->head( $url );
51 0         0 ok($reshead->is_success, "Successful HEAD request for $url");
52             subtest 'Testing HTTP header content' => sub {
53 0     0   0 plan tests => scalar keys(%{$args});
  0         0  
54 0         0 while (my ($predicate, $value) = each(%{$args})) {
  0         0  
55 0         0 my ($key) = $predicate =~ m/\#(.*)$/; # TODO: Use URI::NamespaceMap for this
56 0         0 $key =~ s/_/-/g; # Some heuristics for creating HTTP headers
57 0         0 $key =~ s/\b(\w)/\u$1/g;
58 0         0 is($reshead->header($key), $value, "$key has correct value");
59             }
60             }
61 1     1   486 }
  1         2  
  1         3  
  0         0  
62              
63              
64             sub http_put_readback_unauthenticated : Test : Plan(4) {
65 0     0 1 0 my ($self, $args) = @_;
66 0         0 my $ua = LWP::UserAgent->new;
67 0         0 my $url = $args->{url};
68 0         0 my $content = '<https://example.org/foo> a <https://example.org/Dahut> .';
69 0         0 my $resput = $ua->put( $url, Content => $content );
70 0         0 note($args->{description});
71 0         0 ok($resput->is_success, "Successful PUT request for $url");
72 0         0 my $resget = $ua->get( $url );
73 0         0 ok($resget->is_success, "Successful GET request for $url");
74 0         0 my $rescontent = $resget->content;
75 0         0 is_valid_rdf($rescontent, "Returned content is valid RDF");
76 0         0 is_rdf($rescontent, $content, "Same content returned");
77 1     1   292 }
  1         1  
  1         5  
78              
79              
80             sub http_write_with_bearer : Test : Plan(1) {
81 0     0 1 0 my ($self, $args) = @_;
82             SKIP: {
83 0 0       0 skip 'SOLID_BEARER_TOKEN needs to set for this test', 1 unless ($ENV{SOLID_BEARER_TOKEN});
  0         0  
84 0         0 my $ua = LWP::UserAgent->new;
85             $ua->default_header('Authorization' => 'Bearer ' . $ENV{SOLID_BEARER_TOKEN},
86 0         0 'Content-Type' => 'text/turtle'
87             );
88 0         0 my $url = $args->{url};
89 0         0 my $res = $ua->put( $url, Content => '<https://example.org/foo> a <https://example.org/Dahut> .' );
90 0         0 ok($res->is_success, "Successful PUT request for $url");
91             }
92 1     1   264 };
  1         2  
  1         24  
93              
94              
95             sub http_methods_with_bearer : Test : Plan(1) {
96 0     0 1   my ($self, $args) = @_;
97             SKIP: {
98 0 0         skip 'SOLID_BEARER_TOKEN needs to set for this test', 1 unless ($ENV{SOLID_BEARER_TOKEN});
  0            
99 0           note($args->{description});
100 0           my $ua = LWP::UserAgent->new;
101             $ua->default_header('Authorization' => 'Bearer ' . $ENV{SOLID_BEARER_TOKEN},
102 0           'Content-Type' => 'text/turtle',
103             'Accept' => 'text/turtle',
104             );
105 0           my $url = $args->{url};
106 0           my $method = $args->{method};
107 0           my $res = $ua->request( HTTP::Request->new($method => $url), Content => $args->{body} );
108 0           is($res->code, $args->{code}, "Using $method for request for $url");
109             }
110 1     1   353 };
  1         2  
  1         4  
111              
112              
113              
114              
115              
116             1;
117              
118             __END__
119              
120             =pod
121              
122             =encoding utf-8
123              
124             =head1 NAME
125              
126             Web::Solid::Test::Basic - Basic Solid Tests
127              
128             =head1 SYNOPSIS
129              
130             use Test::FITesque::RDF;
131             my $suite = Test::FITesque::RDF->new(source => $file, base_uri => $ENV{SOLID_REMOTE_BASE})->suite;
132             $suite->run_tests;
133             done_testing;
134              
135             See C<tests/basic.t> for a full example.
136              
137             =head1 DESCRIPTION
138              
139             See L<Web::Solid::Test> for an introduction to the idea behind these
140             test modules.
141              
142             =head1 IMPLEMENTED TESTS
143              
144             =head2 Test scripts
145              
146             This package provides C<tests/basic.t> which runs tests over the
147             fixture table in C<tests/data/basic.ttl>. The test script requires the
148             environment variable C<SOLID_REMOTE_BASE> to be set to the base URL
149             that any relative URLs in the fixture tables will be resolved
150             against. Thus, the fixture tables themselves are independent of the
151             host that will run them.
152              
153             To run the test script in the clone of this package, invoke it like this:
154              
155             SOLID_REMOTE_BASE="https://kjetiltest4.dev.inrupt.net/" prove -l tests/basic.t
156              
157              
158              
159              
160             =head2 C<< http_read_unauthenticated >>
161              
162             Some basic tests for HTTP reads.
163              
164             =head3 Parameters
165              
166             =over
167              
168             =item * C<url>
169              
170             The URL to request.
171              
172             =back
173              
174             =head3 Environment
175              
176             None
177              
178             =head3 Implements
179              
180             =over
181              
182             =item 1. That an HTTP HEAD request to the given URL succeeds.
183              
184             =item 2. That an HTTP GET request to the given URL succeeds.
185              
186             =item 3. That the HEAD and GET requests had the same header fields.
187              
188             =item 4. That the values of the header fields are the same.
189              
190             =back
191              
192             =head2 C<< http_put_readback_unauthenticated >>
193              
194             First writes some content with a PUT request, then reads it back with a GET and checks RDF validity.
195              
196             =head3 Parameters
197              
198             =over
199              
200             =item * C<url>
201              
202             The URL to request.
203              
204             =back
205              
206             =head3 Environment
207              
208             None
209              
210             =head3 Implements
211              
212             =over
213              
214             =item 1. That an HTTP PUT with content request to the given URL succeeds.
215              
216             =item 2. That an HTTP GET request to the given URL succeeds.
217              
218             =item 3. That the content is valid RDF.
219              
220             =item 4. That the triples in the written and read RDF is the same.
221              
222             =back
223              
224              
225             =head2 C<< http_write_with_bearer >>
226              
227             Test for successful HTTP PUT authenticated with a Bearer token
228              
229             =head3 Parameters
230              
231             =over
232              
233             =item * C<url>
234              
235             The URL to request.
236              
237             =back
238              
239             =head3 Environment
240              
241             Set C<SOLID_BEARER_TOKEN> to the bearer token to be used in the authorization header.
242              
243             =head3 Implements
244              
245             =over
246              
247             =item 1. That an HTTP PUT request to the given URL with a short Turtle payload succeeds.
248              
249             =back
250              
251              
252             =head2 C<< http_methods_with_bearer >>
253              
254             Tests for whether a certain HTTP request, authenticated with a Bearer token, returns a certain status code.
255              
256             =head3 Parameters
257              
258             =over
259              
260             =item * C<url>
261              
262             The URL to request.
263              
264             =item * C<method>
265              
266             The HTTP method to use in the request.
267              
268             =item * C<body>
269              
270             The body of the request (optional).
271              
272             =item * C<code>
273              
274             The expected status code to tests for.
275              
276             =back
277              
278             =head3 Environment
279              
280             Set C<SOLID_BEARER_TOKEN> to the bearer token to be used in the authorization header.
281              
282             =head3 Implements
283              
284             =over
285              
286             =item 1. That an HTTP request with the given method and body to the
287             given URL with an payload returns the given status code.
288              
289             =back
290              
291              
292              
293              
294              
295             =head1 NOTE
296              
297             The parameters above are in the RDF formulated as actual full URIs,
298             but where the local part is used here and resolved by the
299             L<Test::FITesque::RDF> framework, see its documentation for details.
300              
301             =head1 TODO
302              
303             The namespaces used in the current fixture tables are examples, and
304             will be changed before an 1.0 release of the system.
305              
306              
307             =head1 BUGS
308              
309             Please report any bugs to
310             L<https://github.com/kjetilk/p5-web-solid-test-basic/issues>.
311              
312             =head1 SEE ALSO
313              
314             =head1 AUTHOR
315              
316             Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
317              
318             =head1 COPYRIGHT AND LICENCE
319              
320             This software is Copyright (c) 2019 by Inrupt Inc.
321              
322             This is free software, licensed under:
323              
324             The MIT (X11) License
325              
326              
327             =head1 DISCLAIMER OF WARRANTIES
328              
329             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
330             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
331             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
332