diff options
author | franck cuny <franck@lumberjaph.net> | 2010-09-15 15:21:44 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-09-15 15:21:44 +0200 |
commit | 43ed9c96e4b1c27d0c5919e6d31bac9fd16a540c (patch) | |
tree | e35130c40d311311f78eabbc16dba5e8bd6039bc | |
parent | mocker for request (diff) | |
download | net-http-spore-43ed9c96e4b1c27d0c5919e6d31bac9fd16a540c.tar.gz |
rewrite test using mocker
-rw-r--r-- | t/spore-middleware/auth-basic.t | 60 |
1 files changed, 41 insertions, 19 deletions
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'; +} |