Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test web frameworks #10

Merged
merged 4 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
dist: xenial
language: perl
sudo: false
perl:
- "5.26"
- "5.28"

before_script:
- cpanm --notest Minilla
# for extra tests
- cpanm --notest Test::CPAN::Meta Test::Pod Test::MinimumVersion::Fast
- cpanm --notest Mojolicious Dancer2
- cpanm --quiet --with-develop --installdeps --notest .

script:
- minil test --all
4 changes: 3 additions & 1 deletion META.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"JSON::Types" : "0.05",
"JSON::XS" : "4.0",
"Plack" : "1.0047",
"Plack::Middleware::ReverseProxy" : "0.16",
"Try::Tiny" : "0.30",
"perl" : "5.026000"
}
Expand Down Expand Up @@ -74,7 +75,8 @@
},
"version" : "0.0.1",
"x_contributors" : [
"Ichinose Shogo <shogo82148@gmail.com>"
"Ichinose Shogo <shogo82148@gmail.com>",
"Jose Luis Martinez <jlmartinez@capside.com>"
],
"x_serialization_backend" : "JSON::PP version 2.97001",
"x_static_install" : 1
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ requires 'HTTP::Tiny', '0.076';
requires 'JSON::XS', '4.0';
requires 'Try::Tiny', '0.30';
requires 'Plack', '1.0047';
requires 'Plack::Middleware::ReverseProxy', '0.16';
requires 'JSON::Types', '0.05';

on 'test' => sub {
Expand Down
18 changes: 14 additions & 4 deletions lib/AWS/Lambda/PSGI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use MIME::Base64;
use JSON::Types;
use Encode;
use Try::Tiny;
use Plack::Middleware::ReverseProxy;

sub new {
my $proto = shift;
Expand Down Expand Up @@ -40,6 +41,11 @@ sub to_app {

sub wrap {
my($self, $app, @args) = @_;

# Lambda function runs as reverse proxy backend.
# So, we always enable ReverseProxy middleware.
$app = Plack::Middleware::ReverseProxy->wrap($app);

if (ref $self) {
$self->{app} = $app;
} else {
Expand Down Expand Up @@ -120,10 +126,14 @@ sub format_input {
}
$env->{PATH_INFO} = URI::Escape::uri_unescape($payload->{path});

if (defined $payload->{ requestContext } and defined $payload->{ requestContext }->{ stage }) {
$env->{SCRIPT_NAME} = "/$payload->{ requestContext }->{ stage }";
} else {
$env->{SCRIPT_NAME} = '';
$env->{SCRIPT_NAME} = '';
my $requestContext = $payload->{requestContext};
if ($requestContext) {
my $path = $requestContext->{path};
my $stage = $requestContext->{stage};
if ($stage && $path && $path ne $payload->{path}) {
$env->{SCRIPT_NAME} = "/$stage";
}
}

return $env;
Expand Down
2 changes: 1 addition & 1 deletion t/21_mojo.t → xt/21_mojo.t
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ my $get_request_for_get_link = <<EOF;
EOF

my $server = Mojo::Server::PSGI->new;
$server->load_app('t/21_mojoapp');
$server->load_app('xt/21_mojoapp.pl');
my $func = AWS::Lambda::PSGI->wrap($server->to_psgi_app);
my $input = decode_json($get_request_for_get_link);
my $ret = $func->($input);
Expand Down
140 changes: 140 additions & 0 deletions xt/21_mojo_custom_domain.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use strict;
use warnings;
use utf8;

use Test::More;
use JSON::XS qw/decode_json/;

use AWS::Lambda::PSGI;
use Mojo::Server::PSGI;

my $get_request_for_get_link = <<EOF;
{
"resource": "/{proxy+}",
"path": "/get-link",
"httpMethod": "GET",
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "ja-JP,ja;q=0.9,en-US;q=0.8,en;q=0.7",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "JP",
"Host": "xxxxxx.example.com",
"upgrade-insecure-requests": "1",
"User-Agent": "curl/7.54.0",
"Via": "2.0 xxxxxxxxxxxx.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "A3DaibJs0GRv3WgyO-5jEtkSlUOG-BfqY1sQJwjFt5RoqHw0uPjo8w==",
"X-Amzn-Trace-Id": "Root=1-5cb40559-7f2aab91110daa905c11c3c9",
"X-Forwarded-For": "192.0.2.1, 192.0.2.2",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"multiValueHeaders": {
"Accept": [
"*/*"
],
"Accept-Encoding": [
"gzip, deflate, br"
],
"Accept-Language": [
"ja-JP,ja;q=0.9,en-US;q=0.8,en;q=0.7"
],
"CloudFront-Forwarded-Proto": [
"https"
],
"CloudFront-Is-Desktop-Viewer": [
"true"
],
"CloudFront-Is-Mobile-Viewer": [
"false"
],
"CloudFront-Is-SmartTV-Viewer": [
"false"
],
"CloudFront-Is-Tablet-Viewer": [
"false"
],
"CloudFront-Viewer-Country": [
"JP"
],
"Host": [
"xxxxxx.example.com"
],
"upgrade-insecure-requests": [
"1"
],
"User-Agent": [
"curl/7.54.0"
],
"Via": [
"2.0 xxxxxxxxxxxx.cloudfront.net (CloudFront)"
],
"X-Amz-Cf-Id": [
"A3DaibJs0GRv3WgyO-5jEtkSlUOG-BfqY1sQJwjFt5RoqHw0uPjo8w=="
],
"X-Amzn-Trace-Id": [
"Root=1-5cb40559-7f2aab91110daa905c11c3c9"
],
"X-Forwarded-For": [
"192.0.2.1, 192.0.2.2"
],
"X-Forwarded-Port": [
"443"
],
"X-Forwarded-Proto": [
"https"
]
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": {
"proxy": "get-link"
},
"stageVariables": null,
"requestContext": {
"resourceId": "eto9na",
"resourcePath": "/{proxy+}",
"httpMethod": "GET",
"extendedRequestId": "YKXGBHSvNjMFQmA=",
"requestTime": "15/Apr/2019:04:15:21 +0000",
"path": "/get-link",
"accountId": "445285296882",
"protocol": "HTTP/1.1",
"stage": "prod",
"domainPrefix": "xxxxxx",
"requestTimeEpoch": 1555301721775,
"requestId": "15f453fc-5f35-11e9-b133-69be6282d969",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"sourceIp": "122.249.124.94",
"accessKey": null,
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "curl/7.54.0",
"user": null
},
"domainName": "xxxxxx.example.com",
"apiId": "eigh7mjsag"
},
"body": null,
"isBase64Encoded": false
}
EOF

my $server = Mojo::Server::PSGI->new;
$server->load_app('xt/21_mojoapp.pl');
my $func = AWS::Lambda::PSGI->wrap($server->to_psgi_app);
my $input = decode_json($get_request_for_get_link);
my $ret = $func->($input);

cmp_ok($ret->{ body }, 'eq', '/controller1');

done_testing;
File renamed without changes.
3 changes: 1 addition & 2 deletions t/22_dancer.t → xt/22_dancer.t
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ my $get_request_for_get_link = <<EOF;
}
EOF

use lib 't';
my $app = require '22_dancerapp';
my $app = require './xt/22_dancerapp.pl';
my $func = AWS::Lambda::PSGI->wrap($app);
my $input = decode_json($get_request_for_get_link);
my $ret = $func->($input);
Expand Down
File renamed without changes.