Skip to content

Commit a0e6559

Browse files
committed
replace LWP usage with HTTP::Tiny and HTTP::CookieJar (for tests)
1 parent 2a6fd34 commit a0e6559

28 files changed

+185
-239
lines changed

bin/dancer

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use File::Spec::Functions;
1212
use Getopt::Long;
1313
use Pod::Usage;
1414
use Dancer::Renderer;
15-
use LWP::UserAgent;
15+
use HTTP::Tiny;
1616
use constant FILE => 1;
1717

1818
# options
@@ -269,14 +269,13 @@ sub write_data_to_file {
269269

270270
sub send_http_request {
271271
my $url = shift;
272-
my $ua = LWP::UserAgent->new;
272+
my $ua = HTTP::Tiny->new;
273273
$ua->timeout(5);
274-
$ua->env_proxy();
275274

276275
my $response = $ua->get($url);
277276

278-
if ($response->is_success) {
279-
return $response->content;
277+
if ($response->{success}) {
278+
return $response->{content};
280279
}
281280
else {
282281
return;

dist.ini

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ upstream=origin
1919

2020
authority=cpan:SUKRIA
2121

22+
[Prereqs / RuntimeRequires]
23+
HTTP::Tiny = 0.014 ; for get/post/post_form
2224

2325
[Prereqs / RuntimeRecommends ]
2426
YAML = 0

lib/Dancer.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@ The following modules are mandatory (Dancer cannot run without them):
19621962
19631963
=item L<HTTP::Body>
19641964
1965-
=item L<LWP>
1965+
=item L<HTTP::Tiny>
19661966
19671967
=item L<MIME::Types>
19681968

t/00_base/12_utf8_charset.t

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use utf8;
55
use Encode;
66
use Test::More import => ['!pass'];
77
use Dancer::ModuleLoader;
8-
use LWP::UserAgent;
8+
use HTTP::Tiny;
99

1010
plan skip_all => "skip test with Test::TCP in win32/cygwin" if ($^O eq 'MSWin32' or $^O eq 'cygwin');
1111
plan skip_all => "Test::TCP is needed for this test"
@@ -19,16 +19,16 @@ Test::TCP::test_tcp(
1919
my $res;
2020

2121
$res = _get_http_response(GET => '/string', $port);
22-
is d($res->content), "\x{1A9}", "utf8 static response";
22+
is d($res->{content}), "\x{1A9}", "utf8 static response";
2323

2424
$res = _get_http_response(GET => '/other/string', $port);
25-
is d($res->content), "\x{1A9}", "utf8 response through forward";
25+
is d($res->{content}), "\x{1A9}", "utf8 response through forward";
2626

2727
$res = _get_http_response(GET => "/param/".u("\x{1A9}"), $port);
28-
is d($res->content), "\x{1A9}", "utf8 route param";
28+
is d($res->{content}), "\x{1A9}", "utf8 route param";
2929

3030
$res = _get_http_response(GET => "/view?string1=".u("\x{E9}"), $port);
31-
is d($res->content), "sigma: 'Ʃ'\npure_token: 'Ʃ'\nparam_token: '\x{E9}'\n",
31+
is d($res->{content}), "sigma: 'Ʃ'\npure_token: 'Ʃ'\nparam_token: '\x{E9}'\n",
3232
"params and tokens are valid unicode";
3333
},
3434
server => sub {
@@ -60,8 +60,7 @@ sub d {
6060
sub _get_http_response {
6161
my ($method, $path, $port) = @_;
6262

63-
my $ua = LWP::UserAgent->new;
64-
my $req = HTTP::Request->new($method => "http://127.0.0.1:$port${path}");
65-
return $ua->request($req);
63+
my $ua = HTTP::Tiny->new;
64+
return $ua->request($method => "http://127.0.0.1:$port${path}");
6665
}
6766

t/02_request/07_raw_data.t

+5-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plan skip_all => "skip test with Test::TCP in win32/cygwin" if ($^O eq 'MSWin32'
1010
plan skip_all => "Test::TCP is needed for this test"
1111
unless Dancer::ModuleLoader->load("Test::TCP" => "1.30");
1212

13-
use LWP::UserAgent;
13+
use HTTP::Tiny;
1414

1515
use constant RAW_DATA => "var: 2; foo: 42; bar: 57\nHey I'm here.\r\n\r\n";
1616

@@ -19,15 +19,12 @@ Test::TCP::test_tcp(
1919
client => sub {
2020
my $port = shift;
2121
my $rawdata = RAW_DATA;
22-
my $ua = LWP::UserAgent->new;
23-
my $req = HTTP::Request->new(PUT => "http://127.0.0.1:$port/jsondata");
22+
my $ua = HTTP::Tiny->new;
2423
my $headers = { 'Content-Length' => length($rawdata) };
25-
$req->push_header($_, $headers->{$_}) foreach keys %$headers;
26-
$req->content($rawdata);
27-
my $res = $ua->request($req);
24+
my $res = $ua->put("http://127.0.0.1:$port/jsondata", { headers => $headers, content => $rawdata });
2825

29-
ok $res->is_success, 'req is success';
30-
is $res->content, $rawdata, "raw_data is OK";
26+
ok $res->{success}, 'req is success';
27+
is $res->{content}, $rawdata, "raw_data is OK";
3128
},
3229
server => sub {
3330
my $port = shift;

t/02_request/10_mixed_params.t

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ plan skip_all => "skip test with Test::TCP in win32" if ($^O eq 'MSWin32' or $^O
1111
plan skip_all => "Test::TCP is needed for this test"
1212
unless Dancer::ModuleLoader->load("Test::TCP" => "1.30");
1313

14-
use LWP::UserAgent;
14+
use HTTP::Tiny;
1515

1616
plan tests => 2;
1717
Test::TCP::test_tcp(
1818
client => sub {
1919
my $port = shift;
20-
my $ua = LWP::UserAgent->new;
21-
my $res = $ua->post("http://127.0.0.1:$port/params/route?a=1&var=query",
20+
my $ua = HTTP::Tiny->new;
21+
my $res = $ua->post_form("http://127.0.0.1:$port/params/route?a=1&var=query",
2222
{var => 'post', b => 2});
2323

24-
ok $res->is_success, 'req is success';
25-
my $content = $res->content;
24+
ok $res->{success}, 'req is success';
25+
my $content = $res->{content};
2626
my $VAR1;
2727
eval ("$content");
2828

t/02_request/13_ajax.t

+11-13
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@ plan skip_all => 'Test::TCP is needed to run this test'
77
unless Dancer::ModuleLoader->load('Test::TCP' => "1.30");
88
plan tests => 8;
99

10-
use LWP::UserAgent;
10+
use HTTP::Tiny;
1111
use HTTP::Headers;
1212

1313
Test::TCP::test_tcp(
1414
client => sub {
1515
my $port = shift;
16-
my $ua = LWP::UserAgent->new;
17-
18-
my $request = HTTP::Request->new(GET => "http://127.0.0.1:$port/req");
19-
$request->header('X-Requested-With' => 'XMLHttpRequest');
20-
my $res = $ua->request($request);
21-
ok($res->is_success, "server responded");
22-
is($res->content, 1, "content ok");
23-
24-
$request = HTTP::Request->new(GET => "http://127.0.0.1:$port/req");
25-
$res = $ua->request($request);
26-
ok($res->is_success, "server responded");
27-
is($res->content, 0, "content ok");
16+
my $ua = HTTP::Tiny->new;
17+
18+
my $headers = { 'X-Requested-With' => 'XMLHttpRequest' };
19+
my $res = $ua->get("http://127.0.0.1:$port/req", { headers => $headers });
20+
ok($res->{success}, "server responded");
21+
is($res->{content}, 1, "content ok");
22+
23+
$res = $ua->get("http://127.0.0.1:$port/req");
24+
ok($res->{success}, "server responded");
25+
is($res->{content}, 0, "content ok");
2826
},
2927
server => sub {
3028
my $port = shift;

t/02_request/15_headers.t

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plan skip_all => "skip test with Test::TCP in win32" if $^O eq 'MSWin32';
66
plan skip_all => 'Test::TCP is needed to run this test'
77
unless Dancer::ModuleLoader->load('Test::TCP' => "1.30");
88

9-
use LWP::UserAgent;
9+
use HTTP::Tiny;
1010

1111
my $plack_available = Dancer::ModuleLoader->load('Plack::Request');
1212
Dancer::ModuleLoader->load('Plack::Loader') if $plack_available;
@@ -20,17 +20,15 @@ for my $handler (@handlers) {
2020
Test::TCP::test_tcp(
2121
client => sub {
2222
my $port = shift;
23-
my $ua = LWP::UserAgent->new;
23+
my $ua = HTTP::Tiny->new;
2424

25-
my $request = HTTP::Request->new(GET => "http://127.0.0.1:$port/req");
26-
$request->header('X-User-Head1' => 42);
27-
$request->header('X-User-Head2' => 43);
25+
my $headers = { 'X-User-Head1' => 42, 'X-User-Head2' => 43 };
2826

29-
my $res = $ua->request($request);
30-
ok($res->is_success, "$handler server responded");
31-
is($res->header('X-Foo'), 2);
32-
is($res->header('X-Bar'), 3);
33-
is($res->header('Content-Type'), 'text/plain');
27+
my $res = $ua->get("http://127.0.0.1:$port/req", { headers => $headers });
28+
ok($res->{success}, "$handler server responded");
29+
is($res->{headers}{'x-foo'}, 2);
30+
is($res->{headers}{'x-bar'}, 3);
31+
is($res->{headers}{'content-type'}, 'text/plain');
3432
},
3533
server => sub {
3634
my $port = shift;

t/03_route_handler/21_ajax.t

+13-18
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ plan skip_all => "skip test with Test::TCP in win32" if $^O eq 'MSWin32';
88
plan skip_all => 'Test::TCP is needed to run this test'
99
unless Dancer::ModuleLoader->load('Test::TCP' => "1.30");
1010

11-
use LWP::UserAgent;
11+
use HTTP::Tiny;
1212

13-
plan tests => 43;
13+
plan tests => 33;
1414

1515
ok(Dancer::App->current->registry->is_empty,
1616
"registry is empty");
@@ -21,7 +21,7 @@ ok(!Dancer::App->current->registry->is_empty,
2121
Test::TCP::test_tcp(
2222
client => sub {
2323
my $port = shift;
24-
my $ua = LWP::UserAgent->new;
24+
my $ua = HTTP::Tiny->new;
2525

2626
my @queries = (
2727
{ path => 'req', ajax => 1, success => 1, content => 1 },
@@ -36,31 +36,26 @@ Test::TCP::test_tcp(
3636
);
3737

3838
foreach my $query (@queries) {
39-
ok my $request =
40-
HTTP::Request->new(
41-
GET => "http://127.0.0.1:$port/" . $query->{path} );
42-
43-
$request->header( 'X-Requested-With' => 'XMLHttpRequest' )
39+
my %headers;
40+
$headers{'X-Requested-With'} = 'XMLHttpRequest'
4441
if ( $query->{ajax} == 1);
4542

46-
ok my $res = $ua->request($request);
43+
ok my $res = $ua->get("http://127.0.0.1:$port/" . $query->{path}, { headers => \%headers });
4744

4845
if ( $query->{success} == 1) {
49-
ok $res->is_success;
50-
is $res->content, $query->{content};
51-
like $res->header('Content-Type'), qr/text\/xml/ if $query->{ajax} == 1;
46+
ok $res->{success};
47+
is $res->{content}, $query->{content};
48+
like $res->{headers}{'content-type'}, qr/text\/xml/ if $query->{ajax} == 1;
5249
}
5350
else {
54-
ok !$res->is_success;
51+
ok !$res->{success};
5552
}
5653
}
5754

5855
# test ajax with content_type to json
59-
ok my $request =
60-
HTTP::Request->new( GET => "http://127.0.0.1:$port/ajax.json" );
61-
$request->header( 'X-Requested-With' => 'XMLHttpRequest' );
62-
ok my $res = $ua->request($request);
63-
like $res->header('Content-Type'), qr/json/;
56+
my %headers = ( 'X-Requested-With' => 'XMLHttpRequest' );
57+
ok my $res = $ua->get("http://127.0.0.1:$port/ajax.json", { headers => \%headers });
58+
like $res->{headers}{'content-type'}, qr/json/;
6459
},
6560
server => sub {
6661
my $port = shift;

t/03_route_handler/28_plack_mount.t

+5-7
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ BEGIN {
1111
unless Dancer::ModuleLoader->load('Plack::Builder');
1212
}
1313

14-
use HTTP::Request;
15-
use LWP::UserAgent;
14+
use HTTP::Tiny;
1615

1716
use Plack::Builder; # should be loaded in BEGIN block, but it seems that it's not the case ...
1817
use HTTP::Server::Simple::PSGI;
@@ -24,11 +23,10 @@ Test::TCP::test_tcp(
2423
my $port = shift;
2524
my $url = "http://127.0.0.1:$port/mount/test/foo";
2625

27-
my $req = HTTP::Request->new(GET => $url);
28-
my $ua = LWP::UserAgent->new();
29-
ok my $res = $ua->request($req);
30-
ok $res->is_success;
31-
is $res->content, '/foo';
26+
my $ua = HTTP::Tiny->new();
27+
ok my $res = $ua->get($url);
28+
ok $res->{success};
29+
is $res->{content}, '/foo';
3230
},
3331
server => sub {
3432
my $port = shift;

t/03_route_handler/33_vars.t

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use warnings;
55

66
use Test::More;
77

8-
use LWP::UserAgent;
8+
use HTTP::Tiny;
99

1010
plan skip_all => "skip test with Test::TCP in win32" if $^O eq 'MSWin32';
1111
plan skip_all => "Test::TCP is needed for this test"
@@ -15,13 +15,10 @@ plan tests => 10;
1515
Test::TCP::test_tcp(
1616
client => sub {
1717
my $port = shift;
18-
my $ua = LWP::UserAgent->new;
18+
my $ua = HTTP::Tiny->new;
1919
for (1..10) {
20-
my $req = HTTP::Request->new(
21-
GET => "http://127.0.0.1:$port/getvarfoo"
22-
);
23-
my $res = $ua->request($req);
24-
is $res->content, 1;
20+
my $res = $ua->get("http://127.0.0.1:$port/getvarfoo");
21+
is $res->{content}, 1;
2522
}
2623
},
2724
server => sub {

t/03_route_handler/34_forward_body_post.t

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@ BEGIN {
1515
}
1616

1717
use Dancer;
18-
use LWP::UserAgent;
19-
use HTTP::Request;
18+
use HTTP::Tiny;
2019

2120
plan tests => 2;
2221

2322
Test::TCP::test_tcp(
2423
client => sub {
2524
my $port = shift;
2625
my $url_base = "http://127.0.0.1:$port";
27-
my $ua = LWP::UserAgent->new;
28-
my $res = $ua->post($url_base . "/foo", { data => 'foo'});
29-
is($res->decoded_content, "data:foo");
26+
my $ua = HTTP::Tiny->new;
27+
my $res = $ua->post_form($url_base . "/foo", { data => 'foo'});
28+
is($res->{content}, "data:foo");
3029

31-
$res = $ua->post($url_base . "/foz", { data => 'foo'});
32-
is($res->decoded_content, "data:foo");
30+
$res = $ua->post_form($url_base . "/foz", { data => 'foo'});
31+
is($res->{content}, "data:foo");
3332
},
3433
server => sub {
3534
my $port = shift;

t/04_static_file/001_base.t

+10-16
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,15 @@ my $r = Dancer::SharedData->response();
4343
is $r->status, 400;
4444
is $r->content, 'Bad Request';
4545

46-
require HTTP::Request;
47-
require LWP::UserAgent;
46+
require HTTP::Tiny;
4847

4948
Test::TCP::test_tcp(
5049
client => sub {
5150
my $port = shift;
52-
my $req =
53-
HTTP::Request->new(
54-
GET => "http://127.0.0.1:$port/hello%00.txt" );
55-
my $ua = LWP::UserAgent->new();
56-
my $res = $ua->request($req);
57-
ok !$res->is_success;
58-
is $res->code, 400;
51+
my $ua = HTTP::Tiny->new();
52+
my $res = $ua->get("http://127.0.0.1:$port/hello%00.txt");
53+
ok !$res->{success};
54+
is $res->{status}, 400;
5955
},
6056
server => sub {
6157
my $port = shift;
@@ -70,13 +66,11 @@ Test::TCP::test_tcp(
7066
Test::TCP::test_tcp(
7167
client => sub {
7268
my $port = shift;
73-
my $req =
74-
HTTP::Request->new(
75-
GET => "http://127.0.0.1:$port/hello.txt", [ 'If-Modified-Since' => $date ] );
76-
my $ua = LWP::UserAgent->new();
77-
my $res = $ua->request($req);
78-
ok !$res->is_success;
79-
is $res->code, 304;
69+
my $headers = { 'If-Modified-Since' => $date };
70+
my $ua = HTTP::Tiny->new();
71+
my $res = $ua->get("http://127.0.0.1:$port/hello.txt", { headers => $headers });
72+
ok !$res->{success};
73+
is $res->{status}, 304;
8074
},
8175
server => sub {
8276
my $port = shift;

0 commit comments

Comments
 (0)