Airlock

 view release on metacpan or  search on metacpan

t/70-client.t  view on Meta::CPAN

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Test::More;
use lib 't/lib';

use HTTP::Server::PSGI;
use Test::TCP;
use JSON::MaybeXS;
use Airlock::Client;
use AirlockTest;

# A stand-in for HTTP::Tiny that talks to a PSGI app in-process: no socket, no
# second process, so the test and the server share one clock and one store.
{
  package LocalUA;
  use Moo;
  extends 'HTTP::Tiny';
  has app  => ( is => 'ro', required => 1 );
  has seen => ( is => 'ro', default => sub { [] } );

  sub request {
    my ( $self, $method, $url, $args ) = @_;
    my $body = $args->{content} // '';
    my ( $path ) = $url =~ m{\Ahttps?://[^/]+(/[^?]*)};
    push @{ $self->seen }, { method => $method, url => $url, body => $body, headers => $args->{headers} };
    CORE::open( my $input, '<', \$body ) or die $!;
    my $psgi = $self->app->( {
      REQUEST_METHOD  => $method,
      PATH_INFO       => $path,
      CONTENT_TYPE    => $args->{headers}{'content-type'},
      CONTENT_LENGTH  => length $body,
      REMOTE_ADDR     => '127.0.0.1',
      HTTP_USER_AGENT => 'local-ua',
      'psgi.input'    => $input
    } );
    return {
      success => $psgi->[0] < 400 ? 1 : '',
      status  => $psgi->[0],
      reason  => 'local',
      content => join( '', @{ $psgi->[2] } ),
      headers => { @{ $psgi->[1] } }
    };
  }
}

sub fixture {
  my ( %arg ) = @_;
  my $t   = AirlockTest->new;
  my $app = delete $arg{app} || $t->airlock->to_app;
  my @slept;
  my $after_sleep = delete $arg{after_sleep} || sub { };
  my $ua     = LocalUA->new( app => $app );
  my $client = Airlock::Client->new(
    client_id       => 'cli',
    scope           => 'read write',
    device_endpoint => 'http://airlock.test/airlock/device',
    token_endpoint  => 'http://airlock.test/airlock/token',
    ua              => $ua,
    now             => sub { $t->clock },
    sleep           => sub { push @slept, $_[0]; $t->advance( $_[0] ); $after_sleep->( $t, scalar @slept ) },
    on_prompt       => sub { },
    %arg



( run in 1.606 second using v1.01-cache-2.11-cpan-234fa220139 )