API-Client

 view release on metacpan or  search on metacpan

t/API_Client.t  view on Meta::CPAN


=cut

=method resource

The resource method returns a new instance of the object for the API resource
endpoint specified.

=signature resource

resource(Str @segments) : Object

=example-1 resource

  # given: synopsis

  $client->resource('status', 200);

=cut

=method serialize

The serialize method serializes and returns the object as a C<hashref>.

=signature serialize

serialize() : HashRef

=example-1 serialize

  # given: synopsis

  $client->serialize;

=cut

=method update

The update method issues a C<PUT> request to the API resource represented by
the object.

=signature update

update(Any %args) : InstanceOf["Mojo::Transaction"]

=example-1 update

  # given: synopsis

  $client->resource('put')->update(
    json => {active => 1}
  );

=cut

package main;

use Mojo::UserAgent;

SKIP: {
  my $skip_tests = do {
    my $tx = Mojo::UserAgent->new->get('https://httpbin.org/anything');

    !eval{$tx->result->is_success};
  };

  unless ($skip_tests) {
    my $test = testauto(__FILE__);

    my $subs = $test->standard;

    $subs->synopsis(fun($tryable) {
      ok my $result = $tryable->result;

      $result
    });

    $subs->scenario('building', fun($tryable) {
      require Scalar::Util;
      ok my $result = $tryable->result;

      my $get = $result->[0];
      my $head = $result->[1];
      my $patch = $result->[2];

      isnt Scalar::Util::refaddr($get), Scalar::Util::refaddr($head);
      isnt Scalar::Util::refaddr($get), Scalar::Util::refaddr($patch);
      isnt Scalar::Util::refaddr($head), Scalar::Util::refaddr($patch);

      is $get->req->method, 'get';
      is $head->req->method, 'head';
      is $patch->req->method, 'patch';
    });

    $subs->scenario('chaining', fun($tryable) {
      require Scalar::Util;
      ok my $result = $tryable->result;

      my $users = $result->[0];
      my $user = $result->[1];
      my $new_user = $result->[2];

      isnt Scalar::Util::refaddr($users), Scalar::Util::refaddr($user);
      isnt Scalar::Util::refaddr($users), Scalar::Util::refaddr($new_user);
      isnt Scalar::Util::refaddr($user), Scalar::Util::refaddr($new_user);

      is $users->url->to_string, 'https://httpbin.org/users';
      is $user->url->to_string, 'https://httpbin.org/users/c09e91a';
      is $new_user->url->to_string, 'https://httpbin.org/users/c09e91a';
    });

    $subs->scenario('fetching', fun($tryable) {
      ok my $result = $tryable->result;

      ;
    });

    $subs->scenario('creating', fun($tryable) {
      ok my $result = $tryable->result;

      ;
    });

    $subs->scenario('updating', fun($tryable) {
      ok my $result = $tryable->result;

      ;
    });

    $subs->scenario('deleting', fun($tryable) {

t/API_Client.t  view on Meta::CPAN

      is $json->{headers}{'Content-Type'}, 'application/json';
      is_deeply $json->{json}, {active => 1};

      $result
    });

    $subs->example(-1, 'prepare', 'method', fun($tryable) {
      ok my $result = $tryable->result;

      $result
    });

    $subs->example(-1, 'process', 'method', fun($tryable) {
      ok my $result = $tryable->result;

      $result
    });

    $subs->example(-1, 'resource', 'method', fun($tryable) {
      ok my $result = $tryable->result;
      is $result->debug, 0;
      is $result->fatal, 0;
      like $result->name, qr/API::Client \(\d.\d\d\)/;
      is $result->retries, 0;
      is $result->timeout, 10;
      is $result->url->to_string, 'https://httpbin.org/status/200';

      $result
    });

    $subs->example(-1, 'serialize', 'method', fun($tryable) {
      ok my $result = $tryable->result;
      is $result->{debug}, 0;
      is $result->{fatal}, 0;
      like $result->{name}, qr/API::Client \(\d.\d\d\)/;
      is $result->{retries}, 0;
      is $result->{timeout}, 10;
      is $result->{url}, 'https://httpbin.org';

      $result
    });

    $subs->example(-1, 'update', 'method', fun($tryable) {
      ok my $result = $tryable->result;

      my $req = $result->req;
      is lc($req->method), 'put';

      my $res = $result->res;
      is $res->code, 200;

      my $json = $res->json;
      is $json->{headers}{'Host'}, 'httpbin.org';
      is $json->{headers}{'Content-Type'}, 'application/json';
      is_deeply $json->{json}, {active => 1};

      $result
    });
  }

  skip 'Unable to connect to HTTPBin' if $skip_tests;
}

ok 1 and done_testing;



( run in 1.526 second using v1.01-cache-2.11-cpan-6b5c3043376 )