Skip to content

Commit acb25d6

Browse files
committed
Defined PRODUCT_CHECH_VALUE and PRODUCT_CHECK_HEADER
1 parent c077725 commit acb25d6

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

lib/Search/Elasticsearch/Role/Cxn.pm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package Search::Elasticsearch::Role::Cxn;
1919

20+
our $PRODUCT_CHECK_HEADER = 'X-elastic-product';
21+
our $PRODUCT_CHECK_VALUE = 'Elasticsearch';
22+
2023
use Moo::Role;
2124
use Search::Elasticsearch::Util qw(parse_params throw to_list);
2225
use List::Util qw(min);
@@ -29,6 +32,7 @@ use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
2932
use Search::Elasticsearch::Util qw(to_list);
3033
use namespace::clean;
3134
use Net::IP;
35+
use Data::Dumper;
3236

3337
requires qw(perform_request error_from_text handle);
3438

@@ -343,7 +347,8 @@ sub process_response {
343347

344348
# Product check
345349
if ( $code >= 200 and $code < 300 ) {
346-
if (!defined $headers->{'X-Elastic-Product'} || $headers->{'X-Elastic-Product'} ne 'Elasticsearch') {
350+
my $product = $headers->{$PRODUCT_CHECK_HEADER} // '';
351+
if ($product ne $PRODUCT_CHECK_VALUE) {
347352
throw( "ProductCheck", "The client noticed that the server is not Elasticsearch and we do not support this unknown product" );
348353
}
349354
}

t/60_Cxn/20_process_response.t

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use Test::More;
1919
use Test::Exception;
2020
use Test::Deep;
2121
use Search::Elasticsearch;
22+
use Search::Elasticsearch::Role::Cxn qw(PRODUCT_CHECK_HEADER PRODUCT_CHECK_VALUE);
23+
24+
our $PRODUCT_CHECK_VALUE = $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_VALUE;
25+
our $PRODUCT_CHECK_HEADER = $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_HEADER;
2226

2327
my $c = Search::Elasticsearch->new->transport->cxn_pool->cxns->[0];
2428
ok $c->does('Search::Elasticsearch::Role::Cxn'),
@@ -29,30 +33,30 @@ my ( $code, $response );
2933
### OK GET
3034
( $code, $response )
3135
= $c->process_response( { method => 'GET', ignore => [] },
32-
200, "OK", '{"ok":1}', { 'content-type' => 'application/json', 'X-Elastic-Product' => 'Elasticsearch' } );
36+
200, "OK", '{"ok":1}', { 'content-type' => 'application/json', $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE } );
3337

3438
is $code, 200, "OK GET - code";
3539
cmp_deeply $response, { ok => 1 }, "OK GET - body";
3640

3741
### OK GET - Text body
3842
( $code, $response )
3943
= $c->process_response( { method => 'GET', ignore => [] },
40-
200, "OK", 'Foo', { 'content-type' => 'text/plain', 'X-Elastic-Product' => 'Elasticsearch' } );
44+
200, "OK", 'Foo', { 'content-type' => 'text/plain', $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE } );
4145

4246
is $code, 200, "OK GET Text body - code";
4347
cmp_deeply $response, 'Foo', "OK GET Text body - body";
4448

4549
### OK GET - Empty body
4650
( $code, $response )
4751
= $c->process_response( { method => 'GET', ignore => [] },
48-
200, "OK", '', { 'X-Elastic-Product' => 'Elasticsearch' } );
52+
200, "OK", '', { $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE } );
4953

5054
is $code, 200, "OK GET Empty body - code";
5155
cmp_deeply $response, '', "OK GET Empty body - body";
5256

5357
### OK HEAD
5458
( $code, $response )
55-
= $c->process_response( { method => 'HEAD', ignore => [] }, 200, "OK", '', { 'X-Elastic-Product' => 'Elasticsearch' } );
59+
= $c->process_response( { method => 'HEAD', ignore => [] }, 200, "OK", '', { $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE } );
5660

5761
is $code, 200, "OK HEAD - code";
5862
is $response, 1, "OK HEAD - body";
@@ -82,7 +86,7 @@ is $response, undef, "Missing GET - body";
8286
### Missing HEAD
8387
( $code, $response )
8488
= $c->process_response( { method => 'HEAD', ignore => [] },
85-
404, "Missing", '', { 'X-Elastic-Product' => 'Elasticsearch' });
89+
404, "Missing", '', { $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE });
8690
is $code, 404, "Missing HEAD - code";
8791
is $response, undef, "Missing HEAD - body";
8892

@@ -92,7 +96,7 @@ throws_ok {
9296
{ method => 'GET', ignore => [] },
9397
400, "Request",
9498
'{"error":"error in body"}',
95-
{ 'content-type' => 'application/json', 'X-Elastic-Product' => 'Elasticsearch' }
99+
{ 'content-type' => 'application/json', $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE }
96100
);
97101
}
98102
qr/\[400\] error in body/, "Request error";

t/60_Cxn_Async/20_process_response.t

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use Test::More;
1919
use Test::Exception;
2020
use Test::Deep;
2121
use Search::Elasticsearch::Async;
22+
use Search::Elasticsearch::Role::Cxn qw(PRODUCT_CHECK_HEADER PRODUCT_CHECK_VALUE);
23+
24+
our $PRODUCT_CHECK_VALUE = $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_VALUE;
25+
our $PRODUCT_CHECK_HEADER = $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_HEADER;
2226

2327
my $c = Search::Elasticsearch::Async->new->transport->cxn_pool->cxns->[0];
2428
ok $c->does('Search::Elasticsearch::Role::Cxn::Async'),
@@ -29,30 +33,30 @@ my ( $code, $response );
2933
### OK GET
3034
( $code, $response )
3135
= $c->process_response( { method => 'GET', ignore => [] },
32-
200, "OK", '{"ok":1}', { 'content-type' => 'application/json', 'X-Elastic-Product' => 'Elasticsearch' } );
36+
200, "OK", '{"ok":1}', { 'content-type' => 'application/json', $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE } );
3337

3438
is $code, 200, "OK GET - code";
3539
cmp_deeply $response, { ok => 1 }, "OK GET - body";
3640

3741
### OK GET - Text body
3842
( $code, $response )
3943
= $c->process_response( { method => 'GET', ignore => [] },
40-
200, "OK", 'Foo', { 'content-type' => 'text/plain', 'X-Elastic-Product' => 'Elasticsearch' } );
44+
200, "OK", 'Foo', { 'content-type' => 'text/plain', $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE } );
4145

4246
is $code, 200, "OK GET Text body - code";
4347
cmp_deeply $response, 'Foo', "OK GET Text body - body";
4448

4549
### OK GET - Empty body
4650
( $code, $response )
4751
= $c->process_response( { method => 'GET', ignore => [] }, 200, "OK",
48-
'', { 'X-Elastic-Product' => 'Elasticsearch' } );
52+
'', { $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE } );
4953

5054
is $code, 200, "OK GET Empty body - code";
5155
cmp_deeply $response, '', "OK GET Empty body - body";
5256

5357
### OK HEAD
5458
( $code, $response )
55-
= $c->process_response( { method => 'HEAD', ignore => [] }, 200, "OK", '', { 'X-Elastic-Product' => 'Elasticsearch' });
59+
= $c->process_response( { method => 'HEAD', ignore => [] }, 200, "OK", '', { $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE });
5660

5761
is $code, 200, "OK HEAD - code";
5862
is $response, 1, "OK HEAD - body";
@@ -63,7 +67,7 @@ throws_ok {
6367
{ method => 'GET', ignore => [] },
6468
404, "Missing",
6569
'{"error": "Something is missing"}',
66-
{ 'content-type' => 'application/json', 'X-Elastic-Product' => 'Elasticsearch' }
70+
{ 'content-type' => 'application/json', $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE }
6771
);
6872
}
6973
qr/Missing/, "Missing GET";
@@ -73,7 +77,7 @@ qr/Missing/, "Missing GET";
7377
{ method => 'GET', ignore => [404] },
7478
404, "Missing",
7579
'{"error": "Something is missing"}',
76-
{ 'content-type' => 'application/json', 'X-Elastic-Product' => 'Elasticsearch' }
80+
{ 'content-type' => 'application/json', $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE }
7781
);
7882

7983
is $code, 404, "Missing GET - code";
@@ -92,7 +96,7 @@ throws_ok {
9296
{ method => 'GET', ignore => [] },
9397
400, "Request",
9498
'{"error":"error in body"}',
95-
{ 'content-type' => 'application/json', 'X-Elastic-Product' => 'Elasticsearch' }
99+
{ 'content-type' => 'application/json', $PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE }
96100
);
97101
}
98102
qr/\[400\] error in body/, "Request error";

t/lib/MockAsyncCxn.pm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ package MockAsyncCxn;
1919

2020
use strict;
2121
use warnings;
22+
use Search::Elasticsearch::Role::Cxn qw(PRODUCT_CHECK_HEADER PRODUCT_CHECK_VALUE);
2223

24+
our $PRODUCT_CHECK_VALUE = $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_VALUE;
25+
our $PRODUCT_CHECK_HEADER = $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_HEADER;
2326
our $VERSION = $Search::Elasticsearch::VERSION;
2427

2528
use Promises qw(deferred);
@@ -118,7 +121,10 @@ sub perform_request {
118121
$response->{code}, # code
119122
$response->{error}, # msg
120123
$response->{content}, # body
121-
{ 'content-type' => 'application/json', 'X-Elastic-Product' => 'Elasticsearch' }
124+
{
125+
'content-type' => 'application/json',
126+
$PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE
127+
}
122128
)
123129
);
124130
1;

t/lib/MockCxn.pm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ package MockCxn;
1919

2020
use strict;
2121
use warnings;
22+
use Search::Elasticsearch::Role::Cxn qw(PRODUCT_CHECK_HEADER PRODUCT_CHECK_VALUE);
2223

24+
our $PRODUCT_CHECK_VALUE = $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_VALUE;
25+
our $PRODUCT_CHECK_HEADER = $Search::Elasticsearch::Role::Cxn::PRODUCT_CHECK_HEADER;
2326
our $VERSION = $Search::Elasticsearch::VERSION;
2427

2528
use Data::Dumper;
@@ -110,7 +113,10 @@ sub perform_request {
110113
$response->{code}, # code
111114
$response->{error}, # msg
112115
$response->{content}, # body
113-
{ 'content-type' => 'application/json', 'X-Elastic-Product' => 'Elasticsearch' }
116+
{
117+
'content-type' => 'application/json',
118+
$PRODUCT_CHECK_HEADER => $PRODUCT_CHECK_VALUE
119+
}
114120
);
115121
}
116122

0 commit comments

Comments
 (0)