]>
Dogcows Code - chaz/graphql-client/blob - t/http.t
14 use GraphQL
::Client
::http
;
16 HTTP
::AnyUA-
>register_backend(MockUA
=> '+MockUA');
18 my $URL = 'http://localhost:4000/graphql';
20 subtest
'attributes' => sub {
21 my $http = GraphQL
::Client
::http-
>new;
23 is($http->method, 'POST', 'default method is POST');
24 is($http->url, undef, 'default url is undefined');
26 $http = GraphQL
::Client
::http-
>new(method => 'HEAD', url
=> $URL);
28 is($http->method, 'HEAD', 'method getter returns correctly');
29 is($http->url, $URL, 'url getter returns correctly');
32 subtest
'bad arguments to execute' => sub {
33 my $http = GraphQL
::Client
::http-
>new(ua
=> 'MockUA');
34 my $mock = $http->any_ua->backend;
37 $http->execute('blah');
38 } qr/^Usage:/, 'first argument must be a hashref';
42 } qr/^Request must have a query/, 'request must have a query';
45 $http->execute({query
=> '{hello}'});
46 } qr/^URL must be provided/, 'request must have a URL';
49 subtest
'POST request' => sub {
50 my $http = GraphQL
::Client
::http-
>new(ua
=> 'MockUA', url
=> $URL);
51 my $mock = $http->any_ua->backend;
53 my $resp = $http->execute({
56 my $req = ($mock->requests)[-1];
58 is($req->[0], 'POST', 'method is POST');
59 is($req->[2]{content
}, '{"query":"{hello}"}', 'encoded body as JSON');
60 is($req->[2]{headers
}{'content-type'}, 'application/json;charset=UTF-8', 'set content-type to json');
63 subtest
'GET request' => sub {
64 my $http = GraphQL
::Client
::http-
>new(ua
=> 'MockUA', url
=> $URL);
65 my $mock = $http->any_ua->backend;
72 my $req = ($mock->requests)[-1];
74 is($req->[0], 'GET', 'method is GET');
75 is($req->[1], "$URL?query=%7Bhello%7D", 'encoded query in params');
76 is($req->[2]{content
}, undef, 'no content');
82 url
=> "$URL?foo=bar",
84 $req = ($mock->requests)[-1];
86 is($req->[1], "$URL?foo=bar&query=%7Bhello%7D", 'encoded query in params with existing param');
89 subtest
'plain response' => sub {
90 my $http = GraphQL
::Client
::http-
>new(ua
=> 'MockUA', url
=> $URL);
91 my $mock = $http->any_ua->backend;
94 content
=> '{"data":{"foo":"bar"}}',
99 my $r = $http->execute({query
=> '{hello}'});
102 data
=> {foo
=> 'bar'},
105 http_response
=> $mock->response,
108 is_deeply
($r, $expected, 'success response') or diag explain
$r;
111 content
=> '{"data":{"foo":"bar"},"errors":[{"message":"uh oh"}]}',
116 $r = $http->execute({query
=> '{hello}'});
119 data
=> {foo
=> 'bar'},
120 errors
=> [{message
=> 'uh oh'}],
123 http_response
=> $mock->response,
126 is_deeply
($r, $expected, 'response with graphql errors') or diag explain
$r;
129 content
=> 'The agent failed',
130 reason
=> 'Internal Exception',
134 my $resp = $http->execute({query
=> '{hello}'});
136 error
=> 'HTTP transport returned 599 (Internal Exception): The agent failed',
139 http_response
=> $mock->response,
142 is_deeply
($resp, $expected, 'response with http error') or diag explain
$resp;
145 content
=> 'not json',
150 $r = $http->execute({query
=> '{hello}'});
152 error
=> re
('^HTTP transport failed to decode response:'),
155 http_response
=> $mock->response,
158 cmp_deeply
($r, $expected, 'response with invalid response') or diag explain
$r;
161 subtest
'future response' => sub {
162 my $http = GraphQL
::Client
::http-
>new(ua
=> 'MockUA', url
=> $URL);
163 my $mock = $http->any_ua->backend;
165 $mock->response(Future-
>done({
166 content
=> '{"data":{"foo":"bar"}}',
171 my $f = $http->execute({query
=> '{hello}'});
174 data
=> {foo
=> 'bar'},
177 http_response
=> $mock->response->get,
180 is_deeply
($f->get, $expected, 'success response') or diag explain
$f->get;
182 $mock->response(Future-
>done({
183 content
=> '{"data":{"foo":"bar"},"errors":[{"message":"uh oh"}]}',
188 $f = $http->execute({query
=> '{hello}'});
191 data
=> {foo
=> 'bar'},
192 errors
=> [{message
=> 'uh oh'}],
195 http_response
=> $mock->response->get,
198 is_deeply
($f->get, $expected, 'response with graphql errors') or diag explain
$f->get;
200 $mock->response(Future-
>fail({
201 content
=> 'The agent failed',
202 reason
=> 'Internal Exception',
207 error
=> 'HTTP transport returned 599 (Internal Exception): The agent failed',
210 http_response
=> ($mock->response->failure)[0],
213 $f = $http->execute({query
=> '{hello}'});
214 is_deeply
($f->get, $expected, 'response with http error') or diag explain
$f->get;
This page took 0.054619 seconds and 4 git commands to generate.