Tester/Manual.pm untested

filter: Statementstmt 0.0% 0 / 1 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 1 SCAR -
1 package JMAP::Tester::Manual 0.112;
2 # ABSTRACT: how to use JMAP::Tester
3  
4 #pod =head1 OVERVIEW
5 #pod
6 #pod JMAP::Tester is a simple JMAP client designed for testing, although it can
7 #pod certainly be used outside of tests. It's meant to be easy to use without a lot
8 #pod of training, but it's not so simple that you don't need a little bit of
9 #pod up-front knowledge. This document is meant to give a high-level overview of
10 #pod how to use it, with links to more detailed information as needed. Quite
11 #pod possibly, you won't need to click even one of them.
12 #pod
13 #pod =head2 Return types and asynchrony
14 #pod
15 #pod When using a JMAP::Tester, you'll need to know whether it was configured to run
16 #pod synchronously or asynchronously. You can check the result of calling
17 #pod C<< L<JMAP::Tester/should_return_futures> >>, but really you want to know this
18 #pod while writing your code, so just make sure you pay attention to how the object
19 #pod was constructed. If your tester returns futures, then its async methods return
20 #pod Future objects and can be called with C<await>. Otherwise, they will block and
21 #pod return whatever the future would have resolved to.
22 #pod
23 #pod The default behavior is to I<not> return futures, as most test suites are
24 #pod synchronous.
25 #pod
26 #pod Methods generally return an object that does the
27 #pod L<HTTPResult|JMAP::Tester::Role::HTTPResult> role, which has just a few
28 #pod important methods:
29 #pod
30 #pod =for :list
31 #pod * C<is_success>, which returns true for a successful HTTP response and false otherwise
32 #pod * C<http_response>, which returns the L<HTTP::Response> object for the result
33 #pod * C<response_payload>, which returns the HTTP response as a string
34 #pod
35 #pod =head2 Authentication and session
36 #pod
37 #pod JMAP does not specify authentication, so authenticating your tester is up to
38 #pod you. Most often, you do that by setting a default header on the tester's user
39 #pod agent, like this:
40 #pod
41 #pod my $tester = JMAP::Tester->new( ... );
42 #pod $tester->ua->set_default_header(Authorization => 'Bearer 1234');
43 #pod
44 #pod This is a pretty broad brush, but for testing your local test system, generally
45 #pod fine.
46 #pod
47 #pod The other thing associated with authentication is your JMAP session, which
48 #pod describes your account's capabilities and HTTP endpoints. The session object
49 #pod is generally represented as a L<JMAP::Tester::Result::Auth> object, which has
50 #pod one useful method: C<client_session>, which returns the decoded JSON structure
51 #pod of the session.
52 #pod
53 #pod There are a few methods for dealing with the session:
54 #pod
55 #pod =for :list
56 #pod * C<< L<JMAP::Tester/get_client_session> >>, an async method that fetches and returns the session object; on success, it returns an Auth object
57 #pod * C<< L<JMAP::Tester/update_client_session> >>, an async method that gets the client session, uses it to reconfigure the tester (if necessary), and returns the Auth object
58 #pod
59 #pod =head2 API requests
60 #pod
61 #pod The most common thing you'll do with a tester is probably make API requests.
62 #pod Those are the ones defined in L<RFC 8620
63 #pod §3.1|https://datatracker.ietf.org/doc/html/rfc8620#section-3.1> -- the ones
64 #pod with C<methodCalls>, where you do most of the work of JMAP.
65 #pod
66 #pod You call it like this:
67 #pod
68 #pod my $res = $tester->request({
69 #pod using => [ 'urn:ietf:params:jmap:core', 'urn:ietf:params:jmap:mail' ],
70 #pod methodCalls => [ [ 'Some/method', { ... }, 'a' ], ... ],
71 #pod });
72 #pod
73 #pod If you trust the client's C<default_using>, provided during creation, you can
74 #pod skip the outer hashref and just provide an arrayref, which will be used as the
75 #pod C<methodCalls> value. Each entry in that arrayref is itself an arrayref that
76 #pod becomes the C<Invocation> (in RFC 8620 terms) that's passed to the remote API.
77 #pod If you don't include a method call id, the third element in the array, one will
78 #pod be generated for you.
79 #pod
80 #pod C<request> is an async method, and on success returns a
81 #pod L<Response|JMAP::Tester::Response> object. Since a I<lot> of testing your JMAP
82 #pod server involves inspecting API responses, it's important to know the interface!
83 #pod First, some jargon: a I<sentence> is one entry in the C<methodResponses>
84 #pod array; a I<paragraph> is a group of sequential sentences all sharing the same
85 #pod method call id. The first element in a sentence is its I<name>. Almost
86 #pod always, you'll be working with the sentences in a response, usually looked up
87 #pod by name.
88 #pod
89 #pod Here are the most important methods on a Response:
90 #pod
91 #pod =for :list
92 #pod * C<sentence($n)> returns the I<n>th sentence in the response (or dies if out of bounds)
93 #pod * C<sentence_named($name)> returns the sentence with this name (or dies if there isn't exactly one with that name)
94 #pod * C<single_sentence($name)> dies unless there is exactly one sentence in the response; if C<$name> is given, the method dies unless the sentence has that name; if it doesn't die, it returns that sentence.
95 #pod * C<as_pairs> and C<as_triples> return arrayrefs where each element is a 2- or 3-element arrayref of the name, arguments, and (maybe) client id of each sentence -- in other words, a plain structure representing the method response
96 #pod * C<as_stripped_pairs> and C<as_stripped_triples> return the same, but with L<JSON::Typist> data tripped from the arguments
97 #pod
98 #pod L<Sentence|JMAP::Tester::Response::Sentence> objects have these useful methods:
99 #pod
100 #pod =for :list
101 #pod * C<name> returns the sentence name
102 #pod * C<arguments> returns the sentence arguments
103 #pod * C<client_id> returns the method call id
104 #pod * C<as_pair>, C<as_triple>, C<as_stripped_pair>, and C<as_stripped_triple> behave like the similarly-named methods on a Response, but just return the arrayref representing this sentence
105 #pod * C<as_set> returns a new L<Set|JMAP::Tester::Response::Sentence::Set> object,
106 #pod with extra methods for testing the response to C</set>-style methods
107 #pod
108 #pod A "Set" sentence has all the methods of a normal sentence as well as:
109 #pod
110 #pod =for :list
111 #pod * C<new_state> and C<old_state>: return the new and old state
112 #pod * C<created>: returns the C<created> argument, or an empty hashref if null
113 #pod * C<created_id($creation_id)>: returns the C<id> for the object created for that creation id
114 #pod * C<updated>: returns the C<updated> argument, or an empty hashref if null
115 #pod * C<created_ids>, C<updated_ids>, C<destroyed_ids>: return the ids of objects created, updated, or destroyed
116 #pod * C<create_errors>, C<update_errors>, C<destroy_errors>: return the errors with their respective operations, or an empty hashref if none
117 #pod * C<not_created_ids>, C<not_updated_ids>, C<not_destroyed_ids>: return the ids of objects not created, not updated, or not destroyed; in other words, the keys of the hashrefs returned by the error methods above
118 #pod
119 #pod There are also a few useful assertion-making methods to know. These will throw
120 #pod aborts (L<see below|/Diagnostics and logging>) if the condition they assert
121 #pod doesn't hold true:
122 #pod
123 #pod =for :list
124 #pod * C<< $result->assert_successful >>: the result must be a success (C<is_success> is true)
125 #pod * C<< $result->assert_successful_set($name) >>: the result must be an API request result with a sentence named C<$name>, which must be a C</set> method, and it must be reporting zero errors (like C<notCreated> etc.)
126 #pod * C<< $result->assert_single_successful_set($name) >>: just like the above, but there must be only one sentence in the response; C<$name> can be omitted to allow any C</set>
127 #pod * C<< $set->assert_no_errors >>: on a Set sentence, this asserts that there were no errors in any of its operations
128 #pod
129 #pod =head2 Uploads and downloads
130 #pod
131 #pod Testing blobs might require that you perform JMAP upload or download requests.
132 #pod For these, the C<< L<JMAP::Tester/upload> >> and C<< L<JMAP::Tester/download>
133 #pod >> methods exist.
134 #pod
135 #pod C<upload> takes as its argument a hashref of upload properties and on success
136 #pod returns an L<Upload|JMAP::Tester::Result::Upload> object. The argument hashref
137 #pod must contain:
138 #pod
139 #pod accountId - the account for which we're uploading (no default)
140 #pod type - the content-type we want to provide to the server
141 #pod blob - the data to upload. Must be a reference to a string
142 #pod
143 #pod C<download> takes as its argument a hashref of download properties and on
144 #pod success returns a L<Download|JMAP::Tester::Result::Download> object. The
145 #pod argument hashref must contain:
146 #pod
147 #pod blobId - the blob to download (no default)
148 #pod accountId - the account for which we're downloading (no default)
149 #pod type - the content-type we want the server to provide back (no default)
150 #pod name - the name we want the server to provide back (default: "download")
151 #pod
152 #pod =head2 Other HTTP requests
153 #pod
154 #pod Sometimes, you may need to make a custom HTTP request with the same underlying
155 #pod user agent as your tester uses. This might be to interact with a custom
156 #pod authentication mechanism, to access custom endpoints, or just to make very,
157 #pod very specifically crafted requests. For this reason, C<http_request> exists.
158 #pod It's an async method that takes an L<HTTP::Request> object as its argument and
159 #pod returns an L<HTTP::Response>. Remember when you make this call that the
160 #pod default headers you may have set up for auth will be applied!
161 #pod
162 #pod =head2 Diagnostics and logging
163 #pod
164 #pod Many of JMAP::Tester's failures are L<Test::Abortable> exceptions ("aborts")
165 #pod that can terminate subtests without terminating the whole test program. They
166 #pod provide useful diagnostics on failure, so learning and using the JMAP::Tester
167 #pod methods that die on unexpected results can save you time later.
168 #pod
169 #pod Further, JMAP::Tester has a generic request logging system. It's subject to
170 #pod significant change, but the important thing to know is that if you set the
171 #pod C<JMAP_TESTER_LOGGER> environment variable to C<HTTP>, you'll end up with a
172 #pod file in the current working directory with a name like F<jmap-tester-….log>
173 #pod that contains the requests and responses made.
174 #pod
175 #pod =cut
176  
177 1;
178  
179 __END__