POE-Component-Client-HTTP

 view release on metacpan or  search on metacpan

t/07_proxy.t  view on Meta::CPAN

#! /usr/bin/perl
# -*- perl -*-
# vim: filetype=perl sw=2 ts=2 expandtab

# Contributed by Yuri Karaban.  Thank you!

use strict;
use warnings;

use Test::More tests => 9;

$SIG{PIPE} = 'IGNORE';

use Socket;

use POE;
use POE::Session;
use POE::Component::Server::TCP;
use POE::Component::Client::HTTP;
use POE::Filter::HTTPD;
use HTTP::Request;
use HTTP::Request::Common qw(GET PUT);

use HTTP::Response;

# We need some control over proxying here.
BEGIN {
  delete $ENV{HTTP_PROXY};
  for (qw /HTTP_PROXY http_proxy NO_PROXY no_proxy/) {
    delete $ENV{$_};
  }
}

POE::Session->create(
   inline_states => {
    _child => sub { undef },
    _stop => sub { undef },

    _start => sub {
      my $kernel = $_[KERNEL];
      $kernel->alias_set('main');

      spawn_http('proxy1');
      spawn_http('proxy2');
      spawn_http('host');
      spawn_rproxy();
    },
    set_port => sub {
      my ($kernel, $heap, $name, $port) = @_[KERNEL, HEAP, ARG0, ARG1];

      $heap->{$name} = "http://127.0.0.1:$port/";

      if (++ $_[HEAP]->{ready_cnt} == 4) {
        $_[KERNEL]->yield('begin_tests');
      }
    },
    begin_tests => sub {
      my ($kernel, $heap) = @_[KERNEL, HEAP];

      POE::Component::Client::HTTP->spawn(Alias => 'DefProxy', Proxy => $heap->{proxy1});
      POE::Component::Client::HTTP->spawn(Alias => 'NoProxy', FollowRedirects => 3);

      # Test is default proxy working
      $kernel->post(DefProxy => request => test1_resp => GET $heap->{host});
    },
    test1_resp => sub {
      my ($kernel, $heap, $resp_pack) = @_[KERNEL, HEAP, ARG1];
      my $resp = $resp_pack->[0];

      ok($resp->is_success && $resp->content eq 'proxy1');

      # Test is default proxy override working
      $kernel->post(DefProxy => request => test2_resp => (GET $heap->{host}), undef, undef, $heap->{proxy2});
    },
    test2_resp => sub {
      my ($kernel, $heap, $resp_pack) = @_[KERNEL, HEAP, ARG1];
      my $resp = $resp_pack->[0];

      ok($resp->is_success && $resp->content eq 'proxy2');

      # Test per request proxy setting (override with no default proxy)
      $kernel->post(NoProxy => request => test3_resp => (GET $heap->{host}), undef, undef, $heap->{proxy1});
    },
    test3_resp => sub {
      my ($kernel, $heap, $resp_pack) = @_[KERNEL, HEAP, ARG1];
      my $resp = $resp_pack->[0];

      ok($resp->is_success && $resp->content eq 'proxy1');



( run in 0.574 second using v1.01-cache-2.11-cpan-71847e10f99 )