-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from rails-lambda/ProxyServer
Local Development Proxy Server
- Loading branch information
Showing
13 changed files
with
190 additions
and
3 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
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
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
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,25 @@ | ||
module Lamby | ||
# This class is used by the `lamby:proxy_server` Rake task to run a | ||
# Rack server for local development proxy. Specifically, this class | ||
# accepts a JSON respresentation of a Lambda context object converted | ||
# to a Hash as the single arugment. | ||
# | ||
class ProxyContext | ||
def initialize(data) | ||
@data = data | ||
end | ||
|
||
def method_missing(method_name, *args, &block) | ||
key = method_name.to_s | ||
if @data.key?(key) | ||
@data[key] | ||
else | ||
super | ||
end | ||
end | ||
|
||
def respond_to_missing?(method_name, include_private = false) | ||
@data.key?(method_name.to_s) || super | ||
end | ||
end | ||
end |
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,36 @@ | ||
module Lamby | ||
class ProxyServer | ||
|
||
METHOD_NOT_ALLOWED = <<-HEREDOC.strip | ||
<h1>Method Not Allowed</h1> | ||
<p>Please POST to this endpoint with an application/json content type and JSON payload of your Lambda's event and context.<p> | ||
<p>Example: <code>{ "event": event, "context": context }</code></p> | ||
HEREDOC | ||
|
||
def call(env) | ||
return method_not_allowed unless method_allowed?(env) | ||
event, context = event_and_context(env) | ||
lambda_to_rack Lamby.cmd(event: event, context: context) | ||
end | ||
|
||
private | ||
|
||
def event_and_context(env) | ||
data = env['rack.input'].dup.read | ||
json = JSON.parse(data) | ||
[ json['event'], Lamby::ProxyContext.new(json['context']) ] | ||
end | ||
|
||
def method_allowed?(env) | ||
env['REQUEST_METHOD'] == 'POST' && env['CONTENT_TYPE'] == 'application/json' | ||
end | ||
|
||
def method_not_allowed | ||
[405, {"Content-Type" => "text/html"}, [ METHOD_NOT_ALLOWED.dup ]] | ||
end | ||
|
||
def lambda_to_rack(response) | ||
[ 200, {"Content-Type" => "application/json"}, [ response.to_json ] ] | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
module Lamby | ||
class Railtie < ::Rails::Railtie | ||
config.lamby = Lamby::Config.config | ||
|
||
rake_tasks do | ||
load 'lamby/tasks.rake' | ||
end | ||
end | ||
end |
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,8 @@ | ||
namespace :lamby do | ||
task :proxy_server => [:environment] do | ||
require 'webrick' | ||
port = ENV['LAMBY_PROXY_PORT'] || 3000 | ||
bind = ENV['LAMBY_PROXY_BIND'] || '0.0.0.0' | ||
Rack::Handler::WEBrick.run Lamby::ProxyServer.new, Port: port, BindAddress: bind | ||
end | ||
end |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Lamby | ||
VERSION = '4.1.1' | ||
VERSION = '4.2.0' | ||
end |
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,29 @@ | ||
require 'test_helper' | ||
|
||
class ProxyContextTest < LambySpec | ||
|
||
let(:context_data) { TestHelpers::LambdaContext.raw_data } | ||
let(:proxy_context) { Lamby::ProxyContext.new(context_data) } | ||
|
||
it 'should respond to all context methods' do | ||
context_data.keys.each do |key| | ||
response = proxy_context.respond_to?(key.to_sym) | ||
expect(response).must_equal true, "Expected context to respond to #{key.inspect}" | ||
end | ||
end | ||
|
||
it 'should return the correct value for each context method' do | ||
expect(proxy_context.clock_diff).must_equal 1681486457423 | ||
expect(proxy_context.deadline_ms).must_equal 1681492072985 | ||
expect(proxy_context.aws_request_id).must_equal "d6f5961b-5034-4db5-b3a9-fa378133b0f0" | ||
end | ||
|
||
it 'should raise an error for unknown methods' do | ||
expect { proxy_context.foo }.must_raise NoMethodError | ||
end | ||
|
||
it 'should return false for respond_to? for a unknown method' do | ||
expect(proxy_context.respond_to?(:foo)).must_equal false | ||
end | ||
|
||
end |
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,52 @@ | ||
require 'net/http' | ||
require 'test_helper' | ||
|
||
class ProxyServerTest < LambySpec | ||
include Rack::Test::Methods | ||
|
||
let(:event) { TestHelpers::Events::HttpV2.create } | ||
let(:context) { TestHelpers::LambdaContext.raw_data } | ||
let(:app) { Rack::Builder.new { run Lamby::ProxyServer.new }.to_app } | ||
let(:json) { {"event": event, "context": context}.to_json } | ||
|
||
it 'should return a 405 helpful message on GET' do | ||
response = get '/' | ||
expect(response.status).must_equal 405 | ||
expect(response.headers).must_equal({"Content-Type" => "text/html"}) | ||
expect(response.body).must_include 'Method Not Allowed' | ||
end | ||
|
||
it 'should call Lamby.cmd on POST and include full response as JSON' do | ||
response = post '/', json, 'CONTENT_TYPE' => 'application/json' | ||
expect(response.status).must_equal 200 | ||
expect(response.headers).must_equal({"Content-Type" => "application/json"}) | ||
response_body = JSON.parse(response.body) | ||
expect(response_body['statusCode']).must_equal 200 | ||
expect(response_body['headers']).must_be_kind_of Hash | ||
expect(response_body['body']).must_match 'Hello Lamby' | ||
end | ||
|
||
it 'will return whatever Lamby.cmd does' do | ||
Lamby.stubs(:cmd).returns({statusCode: 200}) | ||
response = post '/', json, 'CONTENT_TYPE' => 'application/json' | ||
expect(response.status).must_equal 200 | ||
expect(response.headers).must_equal({"Content-Type" => "application/json"}) | ||
response_body = JSON.parse(response.body) | ||
expect(response_body['statusCode']).must_equal 200 | ||
expect(response_body['headers']).must_be_nil | ||
expect(response_body['body']).must_be_nil | ||
end | ||
|
||
it 'will use the configured Lamby rack_app' do | ||
rack_app = Rack::Builder.new { run lambda { |env| [200, {}, StringIO.new('OK')] } }.to_app | ||
Lamby.config.rack_app = rack_app | ||
response = post '/', json, 'CONTENT_TYPE' => 'application/json' | ||
expect(response.status).must_equal 200 | ||
expect(response.headers).must_equal({"Content-Type" => "application/json"}) | ||
response_body = JSON.parse(response.body) | ||
expect(response_body['statusCode']).must_equal 200 | ||
expect(response_body['headers']).must_equal({}) | ||
expect(response_body['body']).must_equal('OK') | ||
end | ||
|
||
end |
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