From 43ed9c96e4b1c27d0c5919e6d31bac9fd16a540c Mon Sep 17 00:00:00 2001 From: franck cuny Date: Wed, 15 Sep 2010 15:21:44 +0200 Subject: rewrite test using mocker --- t/spore-middleware/auth-basic.t | 60 ++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 19 deletions(-) (limited to 't') diff --git a/t/spore-middleware/auth-basic.t b/t/spore-middleware/auth-basic.t index 92776ba..e0e1f05 100644 --- a/t/spore-middleware/auth-basic.t +++ b/t/spore-middleware/auth-basic.t @@ -6,27 +6,49 @@ use MIME::Base64; use Net::HTTP::Spore; -ok my $client = - Net::HTTP::Spore->new_from_spec( 't/specs/couchdb.json', - api_base_url => 'http://localhost:5984' ); - my $username = 'franck'; my $password = 's3kr3t'; -$client->enable( 'Auth::Basic', username => $username, password => $password ); -$client->enable( - 'Test::Response', - body => 'result is ok', - headers => [ 'Content-Type' => 'text/html' ] +my $mock_server = { + '/test_spore/_all_docs' => sub { + my $req = shift; + my $auth = $req->header('Authorization'); + if ($auth) { + $req->new_response( 200, [ 'Content-Type' => 'text/plain' ], 'ok' ); + } + else { + $req->new_response( 403, [ 'Content-Type' => 'text/plain' ], + 'not ok' ); + } + }, +}; + +my @tests = ( + { + middlewares => [ [ 'Mock', tests => $mock_server ] ], + expected => { status => 403, body => 'not ok' } + }, + { + middlewares => [ + [ 'Auth::Basic', username => $username, password => $password ], + [ 'Mock', tests => $mock_server ], + ], + expected => { status => 200, body => 'ok' } + }, ); -my $res = $client->get_all_documents( database => 'test_spore' ); -is $res->[0], 200; - -my $req = $res->request; - -is $req->header('Authorization'), - 'Basic ' . encode_base64( $username . ':' . $password, '' ); - -done_testing; - +plan tests => 3 * @tests; + +foreach my $test (@tests) { + ok my $client = Net::HTTP::Spore->new_from_spec( + 't/specs/couchdb.json', api_base_url => 'http://localhost:5984' + ), + 'client created'; + foreach ( @{ $test->{middlewares} } ) { + $client->enable(@$_); + } + + my $res = $client->get_all_documents( database => 'test_spore' ); + is $res->[0], $test->{expected}->{status}, 'valid HTTP status'; + is $res->[2], $test->{expected}->{body}, 'valid HTTP body'; +} -- cgit 1.4.1