Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeburo committed Mar 11, 2011
1 parent cb4b846 commit 0917f6d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 14 deletions.
6 changes: 4 additions & 2 deletions Makefile.PL
Expand Up @@ -6,14 +6,16 @@ requires 'Furl', 0.31;
requires 'URI';
requires 'List::Util';
requires 'Net::DNS::Lite';
requires 'Digest::DM5';
requires 'Digest::SHA';
requires 'Plack', 0.9963;
requires 'parent';

tests 't/*.t';
author_tests 'xt';

test_requires 'Test::More';
test_requires 'Test::TCP', 1.11;

auto_set_repository;
auto_include;
#auto_include;
WriteAll;
4 changes: 2 additions & 2 deletions lib/WWW/GoogleAnalytics/Mobile.pm
Expand Up @@ -3,7 +3,7 @@ package WWW::GoogleAnalytics::Mobile;
use strict;
use warnings;
use Carp;
use Digest::MD5;
use Digest::SHA qw/hmac_sha1_hex/;
use URI;
use URI::QueryParam;

Expand Down Expand Up @@ -50,7 +50,7 @@ sub image_url {
$referer = $r_uri->as_string;
}

my $digest = substr( Digest::MD5::md5_hex($self->secret . $utmn . $domain . $path), 16, 6 );
my $digest = substr( hmac_sha1_hex($utmn . $domain . $path, $self->secret), 16, 6 );

my $url = URI->new($self->base_url);
$url->query_form_hash({
Expand Down
16 changes: 10 additions & 6 deletions lib/WWW/GoogleAnalytics/Mobile/PSGI.pm
Expand Up @@ -9,9 +9,9 @@ use URI::QueryParam;
use List::Util qw/first/;
use Furl;
use Net::DNS::Lite;
use Digest::MD5;
use Plack::Request;
use Digest::SHA qw/hmac_sha1_hex sha1_hex/;
use Plack::Request;
use Plack::Response;

use Plack::Util::Accessor qw/secret timeout/;

Expand All @@ -31,6 +31,8 @@ my $GIF_DATA = pack "C43", (
0x00, 0x02, 0x02, 0x44, 0x01, 0x00,
0x3b );

our $DEBUG = 0;

sub prepare_app {
my $self = shift;
Carp::croak "secret key must be defined" if ! $self->secret;
Expand Down Expand Up @@ -58,7 +60,7 @@ sub call {
return ['403', ['Content-Type' => 'text/plain'], ['no checksum'] ];
}
if ( $req->param('cs') ne
substr( Digest::MD5::md5_hex($self->secret . $utmn . $domain_name . $document_path), 16, 6 ) ) {
substr( hmac_sha1_hex($utmn . $domain_name . $document_path, $self->secret), 16, 6 ) ) {
return ['403', ['Content-Type' => 'text/plain'], ['checksum not match'] ];
}

Expand Down Expand Up @@ -121,7 +123,10 @@ sub call {
expires => $GAM_COOKIE_USER_PERSISTENCE,
};
$res->body($GIF_DATA);

if ( $DEBUG ) {
$res->header('X-GAM-Code', $status);
$res->header('X-GAM-URI', $utm_url);
}
return $res->finalize;
}

Expand Down Expand Up @@ -150,8 +155,7 @@ sub get_visitor_id {
$message = $user_agent . int(rand 0x7fffffff );
}

my $md5_string = Digest::MD5::md5_hex($message);
return "0x" . substr($md5_string, 0, 16);
return "0x" . substr(sha1_hex($message), 0, 16);
}


Expand Down
7 changes: 5 additions & 2 deletions t/00_compile.t
@@ -1,4 +1,7 @@
use strict;
use Test::More tests => 1;
use Test::More tests => 2;

BEGIN { use_ok 'WWW::GoogleAnalytics::Mobile' }
BEGIN {
use_ok 'WWW::GoogleAnalytics::Mobile';
use_ok 'WWW::GoogleAnalytics::Mobile::PSGI'
}
32 changes: 30 additions & 2 deletions t/01_client.t
@@ -1,5 +1,33 @@
use strict;
use Test::More tests => 1;
use Test::More;
use HTTP::Request;
use HTTP::Message::PSGI;
use URI::QueryParam;

BEGIN { use_ok 'WWW::GoogleAnalytics::Mobile::PSGI' }
BEGIN { use_ok 'WWW::GoogleAnalytics::Mobile' }

my $gam = WWW::GoogleAnalytics::Mobile->new(
base_url => '/t',
account => 'UA-99999-9',
secret => 'very secret '
);

my $req = HTTP::Request->new(GET => 'http://example.com/foo/bar?baz=987');
$req->referer("http://example.com/ref/page?guid=XXXXX&category=321");

my $url = $gam->image_url($req->to_psgi);
ok($url);
isa_ok($url, 'URI');

is( $url->query_param('utmac'), 'UA-99999-9');
like( $url->query_param('utmn'), qr/^\d+$/);
is( $url->query_param('utmhn'), 'example.com');
like( $url->query_param('utmr'), qr!http://example\.com/ref/page! );
unlike( $url->query_param('utmr'), qr!guid=! );
like( $url->query_param('utmr'), qr!category=! );
is( $url->query_param('utmp'), '/foo/bar?baz=987');
like( $url->query_param('cs'), qr/\w{6}/);
is( $url->query_param('guid'), 'ON');

done_testing();

86 changes: 86 additions & 0 deletions t/02_server.t
@@ -0,0 +1,86 @@
use strict;
use Test::More;
use Test::TCP;

use Plack::Builder;
use Plack::Loader;
use Plack::Test;
use WWW::GoogleAnalytics::Mobile::PSGI;
use WWW::GoogleAnalytics::Mobile;
use HTTP::Request;
use HTTP::Message::PSGI;
use URI;
use URI::QueryParam;

my $server = Test::TCP->new(
code => sub {
my $port = shift;
my $loader = Plack::Loader->load(
'Standalone',
host => '127.0.0.1',
port => $port,
);
$loader->run(sub{ [200, [ 'Content-Type' => 'text/plain' ], [ shift->{REQUEST_URI} ]] });
}
);

# For test
$WWW::GoogleAnalytics::Mobile::PSGI::GAM_UTM_GIF_LOCATION =
"http://127.0.0.1:".$server->port."/__utm.gif";
$WWW::GoogleAnalytics::Mobile::PSGI::DEBUG = 1;

my $beacon = WWW::GoogleAnalytics::Mobile::PSGI->new(
secret => 'very secret',
);

ok($beacon);

my $app = builder {
mount "/t" => $beacon;
};

my $gam = WWW::GoogleAnalytics::Mobile->new(
base_url => '/t',
account => 'UA-99999-9',
secret => 'very secret'
);

my $req = HTTP::Request->new(GET => 'http://localhost/foo/bar?baz=987');
$req->referer("http://localhost/ref/page?guid=XXXXX&category=321");
my $gam_url = $gam->image_url($req->to_psgi);

test_psgi
app => $app,
client => sub {
my $cb = shift;
my $req = HTTP::Request->new(GET => $gam_url);
my $res = $cb->($req);
ok($res->is_success);
is($res->header('X-GAM-Code'), '200');
ok($res->header('X-GAM-URI'));

my $url = eval {
URI->new($res->header('X-GAM-URI'))
};
ok($url);
is($url->host, '127.0.0.1');
is($url->port, $server->port);

is( $url->query_param('utmac'), 'UA-99999-9');
like( $url->query_param('utmvid'), qr/^\w+$/);
is( $url->query_param('utmhn'), 'localhost');
like( $url->query_param('utmr'), qr!http://localhost/ref/page! );
unlike( $url->query_param('utmr'), qr!guid=! );
like( $url->query_param('utmr'), qr!category=! );
is( $url->query_param('utmp'), '/foo/bar?baz=987');
is( $url->query_param('utmip'), '127.0.0.0');
ok( $url->query_param('utmwv'));
ok( $url->query_param('utmcc'));

like( $res->header('Set-Cookie'), qr/__utmmobile=\w+/ );;

};

done_testing();


0 comments on commit 0917f6d

Please sign in to comment.