]> Dogcows Code - chaz/graphql-client/blob - t/client.t
rename class attribute to transport_class
[chaz/graphql-client] / t / client.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use FindBin '$Bin';
7 use lib "$Bin/lib";
8
9 use Test::Exception;
10 use Test::More;
11
12 use Future;
13 use GraphQL::Client;
14 use MockTransport;
15
16 subtest 'transport' => sub {
17 my $client = GraphQL::Client->new(transport_class => 'http');
18 isa_ok($client->transport, 'GraphQL::Client::http', 'decide transport from transport_class');
19
20 $client = GraphQL::Client->new(url => 'https://localhost:4000/graphql');
21 isa_ok($client->transport, 'GraphQL::Client::http', 'decide transport from url');
22
23 $client = GraphQL::Client->new(transport_class => 'not a real class');
24 is($client->transport_class, 'not a real class', 'transport_class constructor works');
25 throws_ok { $client->transport } qr/^Failed to load transport/, 'throws if invalid transport';
26 };
27
28 subtest 'request to transport' => sub {
29 my $mock = MockTransport->new;
30 my $client = GraphQL::Client->new(transport => $mock);
31
32 $client->execute('{hello}');
33 my $req = ($mock->requests)[-1];
34 is_deeply($req->[0], {
35 query => '{hello}',
36 }, 'query is passed to transport');
37
38 $client->execute('{hello}', {foo => 'bar'});
39 $req = ($mock->requests)[-1];
40 is_deeply($req->[0], {
41 query => '{hello}',
42 variables => {foo => 'bar'},
43 }, 'vars passed to transport');
44
45 $client->execute('{hello}', {foo => 'bar'}, 'opname');
46 $req = ($mock->requests)[-1];
47 is_deeply($req->[0], {
48 query => '{hello}',
49 variables => {foo => 'bar'},
50 operationName => 'opname',
51 }, 'operationName passed to transport');
52
53 $client->execute('{hello}', {foo => 'bar'}, 'opname', {baz => 'qux'});
54 $req = ($mock->requests)[-1];
55 is_deeply($req->[1], {
56 baz => 'qux',
57 }, 'transport options passed to transport');
58
59 $client->execute('{hello}', {foo => 'bar'}, {baz => 'qux'});
60 $req = ($mock->requests)[-1];
61 is_deeply($req->[1], {
62 baz => 'qux',
63 }, 'operation name can be ommitted with transport options');
64 };
65
66 subtest 'success response' => sub {
67 my $mock = MockTransport->new;
68 my $client = GraphQL::Client->new(transport => $mock);
69
70 $mock->response({
71 response => {
72 data => {
73 hello => 'Hello world!',
74 },
75 },
76 });
77 my $resp = $client->execute('{hello}');
78 is_deeply($resp, {
79 data => {hello => 'Hello world!'},
80 }, 'response is packed') or diag explain $resp;
81 {
82 local $client->{unpack} = 1;
83 my $resp = $client->execute('{hello}');
84 is_deeply($resp, {
85 hello => 'Hello world!',
86 }, 'success response is unpacked') or diag explain $resp;
87 };
88
89 $mock->response(Future->done({
90 response => {
91 data => {
92 hello => 'Hello world!',
93 },
94 },
95 }));
96 my $f = $client->execute('{hello}');
97 is_deeply($f->get, {
98 data => {hello => 'Hello world!'},
99 }, 'future response is packed') or diag explain $f->get;
100 {
101 local $client->{unpack} = 1;
102 my $f = $client->execute('{hello}');
103 is_deeply($f->get, {
104 hello => 'Hello world!',
105 }, 'future success response is unpacked') or diag explain $f->get;
106 };
107 };
108
109 subtest 'response with errors' => sub {
110 my $mock = MockTransport->new;
111 my $client = GraphQL::Client->new(transport => $mock);
112
113 $mock->response({
114 response => {
115 data => {
116 hello => 'Hello world!',
117 },
118 errors => [
119 {
120 message => 'Uh oh',
121 },
122 ],
123 },
124 });
125 my $resp = $client->execute('{hello}');
126 is_deeply($resp, {
127 data => {hello => 'Hello world!'},
128 errors => [{message => 'Uh oh'}],
129 }, 'response is packed') or diag explain $resp;
130 {
131 local $client->{unpack} = 1;
132 throws_ok { $client->execute('{hello}') } qr/^Uh oh$/, 'error response thrown';
133 my $err = $@;
134 is($err->error, 'Uh oh', 'error message is from first error');
135 is($err->type, 'graphql', 'error type is "graphql"');
136 my $resp = $err->{response};
137 is_deeply($resp, {
138 data => {hello => 'Hello world!'},
139 errors => [{message => 'Uh oh'}],
140 }, 'error includes the response') or diag explain $resp;
141 };
142
143 $mock->response({
144 response => undef,
145 error => 'Transport error',
146 details => {
147 foo => 'bar',
148 },
149 });
150 $resp = $client->execute('{hello}');
151 is_deeply($resp, {
152 errors => [{message => 'Transport error'}],
153 }, 'unpacked response munges error into response') or diag explain $resp;
154 {
155 local $client->{unpack} = 1;
156 throws_ok { $client->execute('{hello}') } qr/^Transport error$/, 'error response thrown';
157 my $err = $@;
158 my $resp = $err->{response};
159 is_deeply($resp, {
160 errors => [{message => 'Transport error'}],
161 }, 'error includes the constructed response') or diag explain $resp;
162 };
163 };
164
165 done_testing;
This page took 0.053185 seconds and 5 git commands to generate.