From 2ec7daa7e18fc277a792010793cf8de7a5683e90 Mon Sep 17 00:00:00 2001 From: Dyslexic Degenerate Date: Fri, 4 Jun 2021 18:58:51 -0400 Subject: [PATCH] Experiment converting JSON::Aany to string --- spec/lambda_builder/http_request_spec.cr | 8 ++++ spec/lambda_builder/runtime_spec.cr | 8 ++-- spec/spec_helper.cr | 55 ++++++++++++++++++++++++ src/lambda_builder/runtime.cr | 13 +++--- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/spec/lambda_builder/http_request_spec.cr b/spec/lambda_builder/http_request_spec.cr index 48c3a5e..e706b6e 100644 --- a/spec/lambda_builder/http_request_spec.cr +++ b/spec/lambda_builder/http_request_spec.cr @@ -16,6 +16,14 @@ describe Lambda::Builder::HTTPRequest do req.query_params["test"].should eq "bar" end + it "parses api gateway v2 (HTTP gateway)" do + ENV["_HANDLER"] = "foo" + input = JSON.parse(request_body_v2) + req = Lambda::Builder::HTTPRequest.new(input) + req.method.should eq "OPTIONS" + req.path.should eq "/hi" + end + it "works with empty body and query string" do body = %q({ "path" : "/test", "httpMethod" : "GET", "headers" : { "key": "value" }, "requestContext" : {} }) response = HTTP::Client::Response.new(200, body, HTTP::Headers.new) diff --git a/spec/lambda_builder/runtime_spec.cr b/spec/lambda_builder/runtime_spec.cr index 271b8bb..fe3d56c 100644 --- a/spec/lambda_builder/runtime_spec.cr +++ b/spec/lambda_builder/runtime_spec.cr @@ -27,7 +27,7 @@ describe Lambda::Builder::Runtime do runtime = Lambda::Builder::Runtime.new # handler = do |_input| JSON.parse Lambda::Builder::HTTPResponse.new(200).to_json end runtime.register_handler("my_handler") do |_input| - JSON.parse(%q({ "foo" : "bar"})) + %q({ "foo" : "bar"}) end runtime.handlers["my_handler"].should_not be_nil end @@ -44,7 +44,7 @@ describe Lambda::Builder::Runtime do runtime = Lambda::Builder::Runtime.new runtime.register_handler("my_handler") do - JSON.parse(%q({ "foo" : "bar" })) + %q({ "foo" : "bar" }) end runtime.process_handler end @@ -67,7 +67,7 @@ describe Lambda::Builder::Runtime do runtime.register_handler("my_handler") do response = Lambda::Builder::HTTPResponse.new(200, "text body") response.headers["Content-Type"] = "application/text" - JSON.parse response.to_json + response.to_json end runtime.process_handler end @@ -102,7 +102,7 @@ describe Lambda::Builder::Runtime do runtime = Lambda::Builder::Runtime.new runtime.register_handler("my_handler") do - JSON.parse "{}" + "{}" end runtime.process_handler diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 448c4bb..8e58983 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -116,3 +116,58 @@ def request_body } END end + +def request_body_v2 + <<-END + { + "version": "2.0", + "routeKey": "OPTIONS /{proxy+}", + "rawPath": "/hi", + "rawQueryString": "", + "headers": { + "accept": "*/*", + "accept-encoding": "gzip, deflate, br", + "accept-language": "en-US,en;q=0.9", + "access-control-request-headers": "authorization,content-type", + "access-control-request-method": "POST", + "content-length": "0", + "content-type": "application/x-www-form-urlencoded", + "forwarded": "by=0.0.0.0;for=0.0.0.0;host=local.dev;proto=https", + "host": "local.dev", + "origin": "https://local.dev", + "referer": "https://local.dev/", + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "same-site", + "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/0.0.0.0 Safari/537.36", + "via": "HTTP/1.1 AmazonAPIGateway", + "x-amzn-trace-id": "Self=1-60ad1af6-6041ce5335bd44ad26b42c70;Root=1-60ad2af6-7bbc041f64b16ac44ad97952", + "x-forwarded-for": "0.0.0.0", + "x-forwarded-port": "443", + "x-forwarded-proto": "https" + }, + "pathParameters": { + "proxy": "score" + }, + "requestContext": { + "routeKey": "OPTIONS /{proxy+}", + "accountId": "5555555555", + "stage": "$default", + "requestId": "f5Emhi3LIAMEM7A=", + "apiId": "xxxxx", + "domainName": "local.dev", + "domainPrefix": "local", + "time": "25/May/2021:15:42:46 +0000", + "timeEpoch": 1621957366418, + "http": { + "method": "OPTIONS", + "path": "/hi", + "protocol": "HTTP/1.1", + "sourceIp": "0.0.0.0", + "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/0.0.0.0 Safari/537.36" + } + }, + "isBase64Encoded": false + } +END +end diff --git a/src/lambda_builder/runtime.cr b/src/lambda_builder/runtime.cr index 15a0470..6427ee0 100644 --- a/src/lambda_builder/runtime.cr +++ b/src/lambda_builder/runtime.cr @@ -7,7 +7,7 @@ module Lambda::Builder class Runtime getter host : String getter port : Int16 - getter handlers : Hash(String, (JSON::Any -> JSON::Any)) = Hash(String, (JSON::Any -> JSON::Any)).new + getter handlers : Hash(String, (String -> String)) = Hash(String, (String -> String)).new Log = ::Log.for(self) def initialize @@ -18,7 +18,7 @@ module Lambda::Builder end # Associate the block/proc to the function name - def register_handler(name : String, &handler : JSON::Any -> JSON::Any) + def register_handler(name : String, &handler : String -> String) self.handlers[name] = handler end @@ -38,7 +38,7 @@ module Lambda::Builder end end - def _process_request(proc : Proc(JSON::Any, JSON::Any)) + def _process_request(proc : Proc(String, String)) client = HTTP::Client.new(host: @host, port: @port) begin @@ -48,11 +48,12 @@ module Lambda::Builder aws_request_id = response.headers["Lambda-Runtime-Aws-Request-Id"] base_url = "/2018-06-01/runtime/invocation/#{aws_request_id}" - input = JSON.parse response.body - body = proc.call input + # input = JSON.parse response.body + # body = proc.call input + body = proc.call response.body Log.info { "preparing body #{body}" } - response = client.post("#{base_url}/response", body: body.to_json) + response = client.post("#{base_url}/response", body: body) Log.debug { "response invocation response #{response.status_code} #{response.body}" } rescue ex body = %Q({ "statusCode": 500, "body" : "#{ex.message}" })