Skip to content

Commit

Permalink
Merge branch 'feature/response/etag'
Browse files Browse the repository at this point in the history
  • Loading branch information
oklahomer committed Apr 24, 2014
2 parents 63b588a + 016b381 commit 80cc231
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
43 changes: 37 additions & 6 deletions lib/Facebook/OpenGraph/Response.pm
Expand Up @@ -20,12 +20,32 @@ sub new {
}

# accessors
sub code { shift->{code} }
sub message { shift->{message} }
sub content { shift->{content} }
sub req_headers { shift->{req_headers} }
sub req_content { shift->{req_content} }
sub json { shift->{json} }
sub code { shift->{code} }
sub headers { shift->{headers} }
sub message { shift->{message} }
sub content { shift->{content} }
sub req_headers { shift->{req_headers} }
sub req_content { shift->{req_content} }
sub json { shift->{json} }
sub etag { shift->header('etag') }

sub header {
my ($self, $key) = @_;

croak 'header field name is not given' unless $key;

$self->{header} ||= do {
my $ref = +{};

while (my ($k, $v) = splice @{ $self->headers }, 0, 2) {
$ref->{$k} = $v;
}

$ref;
};

return $self->{header}->{$key};
}

sub is_success {
my $self = shift;
Expand Down Expand Up @@ -191,6 +211,17 @@ Returns request body. This is especially useful for debugging. You must install
later version of Furl to enable this or otherwise empty string will be returned.
Also you have to specify Furl::HTTP->new(capture_request => 1) option.
=head3 C<< $res->etag >>
Returns ETag value that is given as a part of response headers.
=head3 C<< $res->header($field_name) >>
Returns specified header field value.
my $res = $fb->request('GET', 'go.hagiwara');
my $etag = $res->header('etag'); # "a376a57cb3a4bd3a3c6a53fca06b0fd5badee50b"
=head3 C<< $res->is_success >>
Returns if status is 2XX or 304. 304 is added to handle $fb->fetch_with_etag();
Expand Down
Empty file modified t/001_basic/09_js_cookie_name.t 100644 → 100755
Empty file.
Empty file modified t/002_retrieve/15_get_user_token_by_cookie.t 100644 → 100755
Empty file.
64 changes: 64 additions & 0 deletions t/004_response/01_basic.t
@@ -0,0 +1,64 @@
use strict;
use warnings;
use Test::More;
use Facebook::OpenGraph::Response;

subtest 'initialize' => sub {
my $res = Facebook::OpenGraph::Response->new;
isa_ok($res, 'Facebook::OpenGraph::Response');
isa_ok($res->json, 'JSON');
};

subtest 'accessor' => sub {
my $headers = [
'etag',
'"a376a57cb3a4bd3a3c6a53fca06b0fd5badee50b"',
'content-type',
'text/javascript; charset=UTF-8',
'pragma',
'no-cache',
'access-control-allow-origin',
'*',
'x-fb-rev',
'1220390',
'cache-control',
'private, no-cache, no-store, must-revalidate',
'expires',
'Sat, 01 Jan 2000 00:00:00 GMT',
'x-fb-debug',
'oyi19Zu1f4q0fcjowQrrmu8Lby+AgrNcmfLfpMBWcuQ=',
'date',
'Thu, 24 Apr 2014 13:15:31 GMT',
'connection',
'keep-alive',
'content-length',
'185'
];
my $req_headers = qq{GET /go.hagiwara HTTP/1.1\n}
. qq{Connection: keep-alive\n}
. qq{User-Agent: Facebook::OpenGraph/1.13\n}
. qq{Content-Length: 0\n}
. qq{Host: graph.facebook.com\n}
. qq{\n};
my $content = '{"id":"12345"}';

my $res = Facebook::OpenGraph::Response->new(+{
code => 200,
message => 'OK',
headers => $headers,
req_headers => $req_headers,
req_content => '',
content => $content,
});

is($res->code, 200);
is($res->message, 'OK');
is($res->req_headers, $req_headers);
is($res->req_content, '');
is($res->content, $content);
is($res->etag, '"a376a57cb3a4bd3a3c6a53fca06b0fd5badee50b"');
isa_ok($res->json, 'JSON');
is_deeply($res->headers, $headers);
};

done_testing;

0 comments on commit 80cc231

Please sign in to comment.