1use Test::More;
2use Test::Exception;
3use Search::Elasticsearch;
4
5do './t/lib/LogCallback.pl' or die( $@ || $! );
6
7ok my $e
8    = Search::Elasticsearch->new( nodes => 'https://foo.bar:444/some/path' ),
9    'Client';
10
11isa_ok my $l = $e->logger, 'Search::Elasticsearch::Logger::LogAny', 'Logger';
12my $c = $e->transport->cxn_pool->cxns->[0];
13ok $c->does('Search::Elasticsearch::Role::Cxn'),
14    'Does Search::Elasticsearch::Role::Cxn';
15
16# No body
17
18ok $l->trace_request(
19    $c,
20    {   method    => 'POST',
21        qs        => { foo => 'bar' },
22        serialize => 'std',
23        path      => '/xyz'
24    }
25    ),
26    'No body';
27
28is $format, <<'REQUEST', 'No body - format';
29# Request to: https://foo.bar:444/some/path
30curl -XPOST 'http://localhost:9200/xyz?foo=bar&pretty=1'
31REQUEST
32
33# Std body
34
35ok $l->trace_request(
36    $c,
37    {   method    => 'POST',
38        qs        => { foo => 'bar' },
39        serialize => 'std',
40        path      => '/xyz',
41        body      => { foo => qq(bar\n'baz) },
42        data      => qq({"foo":"bar\n'baz"}),
43        mime_type => 'application/json',
44    }
45    ),
46    'Body';
47
48is $format, <<'REQUEST', 'Body - format';
49# Request to: https://foo.bar:444/some/path
50curl -H "Content-type: application/json" -XPOST 'http://localhost:9200/xyz?foo=bar&pretty=1' -d '
51{
52   "foo" : "bar\n\u0027baz"
53}
54'
55REQUEST
56
57# Bulk body
58
59ok $l->trace_request(
60    $c,
61    {   method    => 'POST',
62        qs        => { foo => 'bar' },
63        serialize => 'bulk',
64        path      => '/xyz',
65        body      => [ { foo => qq(bar\n'baz) }, { foo => qq(bar\n'baz) } ],
66        data => qq({"foo":"bar\\n\\u0027baz"}\n{"foo":"bar\\n\\u0027baz"}\n),
67        mime_type => 'application/json',
68    }
69    ),
70    'Bulk';
71
72is $format, <<'REQUEST', 'Bulk - format';
73# Request to: https://foo.bar:444/some/path
74curl -H "Content-type: application/json" -XPOST 'http://localhost:9200/xyz?foo=bar&pretty=1' -d '
75{"foo":"bar\n\u0027baz"}
76{"foo":"bar\n\u0027baz"}
77'
78REQUEST
79
80# String body
81
82ok $l->trace_request(
83    $c,
84    {   method    => 'POST',
85        qs        => { foo => 'bar' },
86        serialize => 'std',
87        path      => '/xyz',
88        body => qq(The quick brown fox\njumped over the lazy dog's basket),
89        mime_type => 'application/json',
90    }
91    ),
92    'Body string';
93
94is $format, <<'REQUEST', 'Body string - format';
95# Request to: https://foo.bar:444/some/path
96curl -H "Content-type: application/json" -XPOST 'http://localhost:9200/xyz?foo=bar&pretty=1' -d '
97The quick brown fox
98jumped over the lazy dog\u0027s basket'
99REQUEST
100
101done_testing;
102
103