]> Dogcows Code - chaz/graphql-client/commitdiff
fix encoding issues
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 22 Mar 2020 07:58:09 +0000 (01:58 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Sun, 22 Mar 2020 07:58:09 +0000 (01:58 -0600)
lib/GraphQL/Client/CLI.pm
lib/GraphQL/Client/http.pm
t/http.t

index 81fb47a67f8824ae3fe5f378e4bf8f3b8051af42..66b391cb723b30a4bbf11addb19990d1fc7c7a3b 100644 (file)
@@ -4,14 +4,17 @@ package GraphQL::Client::CLI;
 use warnings;
 use strict;
 
-use Text::ParseWords;
+use Encode qw(decode);
 use Getopt::Long 2.39 qw(GetOptionsFromArray);
 use GraphQL::Client;
-use JSON::MaybeXS qw(encode_json);
+use JSON::MaybeXS;
+use Text::ParseWords;
 use namespace::clean;
 
 our $VERSION = '999.999'; # VERSION
 
+my $JSON = JSON::MaybeXS->new(canonical => 1);
+
 sub _croak { require Carp; goto &Carp::croak }
 
 sub new {
@@ -67,6 +70,7 @@ sub main {
     if ($query eq '-') {
         print STDERR "Interactive mode engaged! Waiting for a query on <STDIN>...\n"
             if -t STDIN; ## no critic (InputOutput::ProhibitInteractiveTest)
+        binmode(STDIN, 'encoding(UTF-8)');
         $query = do { local $/; <STDIN> };
     }
 
@@ -80,6 +84,7 @@ sub main {
         *STDOUT = $out;
     }
 
+    binmode(STDOUT, 'encoding(UTF-8)');
     _print_data($data, $format);
 
     exit($unpack && $err ? 1 : 0);
@@ -91,6 +96,9 @@ sub _get_options {
 
     unshift @args, shellwords($ENV{GRAPHQL_CLIENT_OPTIONS} || '');
 
+    # assume UTF-8 args if non-ASCII
+    @args = map { decode('UTF-8', $_) } @args if grep { /\P{ASCII}/ } @args;
+
     my %options = (
         format  => 'json:pretty',
         unpack  => 0,
@@ -124,7 +132,7 @@ sub _get_options {
         die "Two or more --variable keys are incompatible.\n" if $@;
     }
     elsif ($options{variables}) {
-        $options{variables} = eval { JSON::MaybeXS->new->decode($options{variables}) };
+        $options{variables} = eval { $JSON->decode($options{variables}) };
         die "The --variables JSON does not parse.\n" if $@;
     }
 
@@ -136,9 +144,9 @@ sub _stringify {
     if (ref($item) eq 'ARRAY') {
         my $first = @$item && $item->[0];
         return join(',', @$item) if !ref($first);
-        return join(',', map { encode_json($_) } @$item);
+        return join(',', map { $JSON->encode($_) } @$item);
     }
-    return encode_json($item) if ref($item) eq 'HASH';
+    return $JSON->encode($item) if ref($item) eq 'HASH';
     return $item;
 }
 
@@ -146,7 +154,7 @@ sub _print_data {
     my ($data, $format) = @_;
     $format = lc($format || 'json:pretty');
     if ($format eq 'json' || $format eq 'json:pretty') {
-        my %opts = (allow_nonref => 1, canonical => 1, utf8 => 1);
+        my %opts = (allow_nonref => 1, canonical => 1);
         $opts{pretty} = 1 if $format eq 'json:pretty';
         print JSON::MaybeXS->new(%opts)->encode($data);
     }
index 77a089f2e319b5f3627ac31c1a9ceac3407fede2..795f2e7ee9e97c72ffc5065e5f09ae2d8c5b9dcf 100644 (file)
@@ -42,7 +42,7 @@ sub execute {
         my $encoded_data = $self->json->encode($data);
         $options->{content} = $encoded_data;
         $options->{headers}{'content-length'} = length $encoded_data;
-        $options->{headers}{'content-type'}   = 'application/json';
+        $options->{headers}{'content-type'}   = 'application/json;charset=UTF-8';
     }
 
     return $self->_handle_response($self->any_ua->request($method, $url, $options));
index 61a9ccf63cceac74932990dc642e35393d359302..cad84fdaa800119b37f8ae331ce779abb36c41c8 100755 (executable)
--- a/t/http.t
+++ b/t/http.t
@@ -57,7 +57,7 @@ subtest 'POST request' => sub {
 
     is($req->[0], 'POST', 'method is POST');
     is($req->[2]{content}, '{"query":"{hello}"}', 'encoded body as JSON');
-    is($req->[2]{headers}{'content-type'}, 'application/json', 'set content-type to json');
+    is($req->[2]{headers}{'content-type'}, 'application/json;charset=UTF-8', 'set content-type to json');
 };
 
 subtest 'GET request' => sub {
This page took 0.029266 seconds and 4 git commands to generate.