1use Test::More;
2use Test::Exception;
3use Test::Deep;
4use Search::Elasticsearch;
5
6my $c = Search::Elasticsearch->new->transport->cxn_pool->cxns->[0];
7ok $c->does('Search::Elasticsearch::Role::Cxn'),
8    'Does Search::Elasticsearch::Role::Cxn';
9
10my ( $code, $response );
11
12### OK GET
13( $code, $response )
14    = $c->process_response( { method => 'GET', ignore => [] },
15    200, "OK", '{"ok":1}', { 'content-type' => 'application/json' } );
16
17is $code, 200, "OK GET - code";
18cmp_deeply $response, { ok => 1 }, "OK GET - body";
19
20### OK GET - Text body
21( $code, $response )
22    = $c->process_response( { method => 'GET', ignore => [] },
23    200, "OK", 'Foo', { 'content-type' => 'text/plain' } );
24
25is $code,             200,   "OK GET Text body - code";
26cmp_deeply $response, 'Foo', "OK GET Text body - body";
27
28### OK GET - Empty body
29( $code, $response )
30    = $c->process_response( { method => 'GET', ignore => [] },
31    200, "OK", '' );
32
33is $code,             200, "OK GET Empty body - code";
34cmp_deeply $response, '',  "OK GET Empty body - body";
35
36### OK HEAD
37( $code, $response )
38    = $c->process_response( { method => 'HEAD', ignore => [] }, 200, "OK" );
39
40is $code,     200, "OK HEAD - code";
41is $response, 1,   "OK HEAD - body";
42
43### Missing GET
44throws_ok {
45    $c->process_response(
46        { method => 'GET', ignore => [] },
47        404, "Missing",
48        '{"error": "Something is missing"}',
49        { 'content-type' => 'application/json' }
50    );
51}
52qr/Missing/, "Missing GET";
53
54### Missing GET ignore
55( $code, $response ) = $c->process_response(
56    { method => 'GET', ignore => [404] },
57    404, "Missing",
58    '{"error": "Something is missing"}',
59    { 'content-type' => 'application/json' }
60);
61
62is $code,     404,   "Missing GET - code";
63is $response, undef, "Missing GET - body";
64
65### Missing HEAD
66( $code, $response )
67    = $c->process_response( { method => 'HEAD', ignore => [] },
68    404, "Missing" );
69is $code,     404,   "Missing HEAD - code";
70is $response, undef, "Missing HEAD - body";
71
72### Request error
73throws_ok {
74    $c->process_response(
75        { method => 'GET', ignore => [] },
76        400, "Request",
77        '{"error":"error in body"}',
78        { 'content-type' => 'application/json' }
79    );
80}
81qr/\[400\] error in body/, "Request error";
82
83### Timeout error
84throws_ok {
85    $c->process_response( { method => 'GET', ignore => [] },
86        509, "28: Timed out,read timeout" );
87}
88qr/Timeout/, "Timeout error";
89
90done_testing;
91