| Socialtext-Resting-Utils documentation | Contained in the Socialtext-Resting-Utils distribution. |
Socialtext::Resting::Mock - Fake rester
my $rester = Socialtext::Resting::Mock->(file => 'foo');
# returns content of 'foo'
$rester->get_page('bar');
Create a new fake rester object. Options:
File to return the contents of.
Get or set the server.
Get or set the username.
Get or set the password.
Get or set the workspace.
Returns the content of the specified file or the page stored locally in the object.
Retrieve a list of pages in the current workspace.
Stores the page content in the object.
Stores the page tags in the object.
Tells the next put_page() to die with the supplied return code.
Stores the requested mime type.
Stores the requested order.
Retrieves the taggedpages stored in the object.
Store the taggedpages return value in the object.
This is not a real function, but it can make testing easier.
Set the json_verbose flag.
Retrieve a fake response object.
Luke Closs, <luke.closs at socialtext.com>
Copyright 2006 Luke Closs, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Socialtext-Resting-Utils documentation | Contained in the Socialtext-Resting-Utils distribution. |
package Socialtext::Resting::Mock; use strict; use warnings; use HTTP::Response;
our $VERSION = '0.04';
sub new { my ($class, %opts) = @_; if ($opts{file}) { die "not a file: $opts{file}" unless -f $opts{file}; } my $self = \%opts; bless $self, $class; return $self; }
sub server { my $self = shift; my $server = shift; $self->{server} = $server if $server; return $self->{server}; }
sub username { my $self = shift; my $username = shift; $self->{username} = $username if $username; return $self->{username}; }
sub password { my $self = shift; my $password = shift; $self->{password} = $password if $password; return $self->{password}; }
sub workspace { my $self = shift; my $workspace = shift; $self->{workspace} = $workspace if $workspace; return $self->{workspace}; }
sub get_page { my $self = shift; my $page_name = shift; if ($self->{file}) { warn "Mock rester: returning content of $self->{file} for page ($page_name)\n"; open(my $fh, $self->{file}) or die "Can't open $self->{file}: $!"; local $/; my $page = <$fh>; close $fh; return $page; } my $text = shift @{ $self->{page}{$page_name} }; unless (defined $text) { $text = "$page_name not found"; } return $text; }
sub get_pages { my ($self) = @_; return $self->{_get_pages} if $self->{_get_pages}; # testing shortcut return keys %{ $self->{page} }; }
sub put_page { my ($self, $page, $content) = @_; die delete $self->{die_on_put} if $self->{die_on_put}; push @{ $self->{page}{$page} }, $content; }
sub put_pagetag { my ($self, $page, $tag) = @_; push @{$self->{page_tags}{$page}}, $tag; }
sub get_pagetags { my ($self, $page) = @_; my $tags = $self->{page_tags}{$page} || []; return @$tags if wantarray; return join ' ', @$tags; }
sub die_on_put { my $self = shift; my $rc = shift; $self->{die_on_put} = $rc; }
sub accept { my $self = shift; $self->{accept} = shift; }
sub order { my $self = shift; $self->{order} = shift; }
sub get_taggedpages { my $self = shift; my $tag = shift; # makes testing easier my $mock_return = $self->{taggedpages}{$tag}; return $mock_return if defined $mock_return; my @taggedpages; for my $page (keys %{$self->{page_tags}}) { my $tags = $self->{page_tags}{$page}; next unless grep { $_ eq $tag } @$tags; push @taggedpages, $page; } return @taggedpages if wantarray; return join ' ', @taggedpages; }
sub set_taggedpages { my $self = shift; my $tag = shift; $self->{taggedpages}{$tag} = shift; }
sub json_verbose { $_[0]->{json_verbose} = $_[1] }
sub response { my $self = shift; $self->{response} = shift if @_; $self->{response} ||= HTTP::Response->new(200); return $self->{response}; }
1;