-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package Plack::Middleware::MetaCPAN::RequestID; | ||
use strict; | ||
use warnings; | ||
|
||
our $VERSION = 'v1.0.1'; | ||
|
||
use Moo; | ||
|
||
use Digest::SHA; | ||
|
||
use namespace::clean; | ||
|
||
with qw(MetaCPAN::Role::Middleware); | ||
|
||
has env_key => ( | ||
is => 'ro', | ||
required => 1, | ||
); | ||
|
||
sub call { | ||
my ( $self, $env ) = @_; | ||
|
||
$env->{ $self->env_key } = Digest::SHA::sha1_hex( join( | ||
"\0", $env->{REMOTE_ADDR}, $env->{REQUEST_URI}, time, $$, rand, ) ); | ||
|
||
$self->app->($env); | ||
} | ||
|
||
1; | ||
__END__ | ||
=pod | ||
=encoding UTF-8 | ||
=for stopwords MDC | ||
=head1 NAME | ||
Plack::Middleware::MetaCPAN::RequestID - Generate a Request ID | ||
=head1 ATTRIBUTES | ||
=head2 env_key | ||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use Data::Dumper (); | ||
use Plack::Middleware::MetaCPAN::RequestID; | ||
use HTTP::Request::Common qw(GET); | ||
use Plack::Test qw(test_psgi); | ||
|
||
my $app = sub { | ||
my $env = shift; | ||
[ 200, [], [ $env->{'request-id'} ] ]; | ||
}; | ||
$app = Plack::Middleware::MetaCPAN::RequestID->wrap( | ||
$app, | ||
{ | ||
env_key => 'request-id', | ||
} | ||
); | ||
|
||
test_psgi $app, sub { | ||
my $cb = shift; | ||
my $res = $cb->( GET "/" ); | ||
like $res->content, qr/\A[a-zA-Z0-9]{40}\z/, 'has a request id'; | ||
}; | ||
|
||
done_testing; |