File tree 4 files changed +74
-22
lines changed
4 files changed +74
-22
lines changed Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require "rack"
4
+
5
+ module Hanami
6
+ module Lambda
7
+ class Env
8
+ def initialize ( event :, headers :, context :)
9
+ @event = event
10
+ @headers = headers
11
+ @content_type = extract_content_type
12
+ @http_headers = transform_http_headers
13
+ @context = context
14
+ end
15
+
16
+ def to_h
17
+ {
18
+ ::Rack ::REQUEST_METHOD => @event [ "httpMethod" ] ,
19
+ ::Rack ::PATH_INFO => @event [ "path" ] || "" ,
20
+ ::Rack ::RACK_VERSION => ::Rack . release ,
21
+ ::Rack ::RACK_INPUT => StringIO . new ( @event [ "body" ] || "" ) ,
22
+ ::Hanami ::Lambda ::LAMBDA_EVENT => @event ,
23
+ ::Hanami ::Lambda ::LAMBDA_CONTEXT => @context
24
+ } . tap do |env |
25
+ env [ "CONTENT_TYPE" ] = @content_type if @content_type
26
+ end . merge ( @http_headers )
27
+ end
28
+
29
+ private
30
+
31
+ def extract_content_type
32
+ @headers . delete ( "Content-Type" ) ||
33
+ @headers . delete ( "content-type" ) ||
34
+ @headers . delete ( "CONTENT_TYPE" )
35
+ end
36
+
37
+ def transform_http_headers
38
+ @headers . transform_keys { |k | "HTTP_#{ k . upcase . tr ( '-' , '_' ) } " }
39
+ end
40
+ end
41
+ end
42
+ end
Original file line number Diff line number Diff line change @@ -41,19 +41,7 @@ def call(event:, context:)
41
41
#
42
42
# @since 0.1.0
43
43
def build_env ( event , headers , context )
44
- {
45
- ::Rack ::REQUEST_METHOD => event [ "httpMethod" ] ,
46
- ::Rack ::PATH_INFO => event [ "path" ] || "" ,
47
- ::Rack ::RACK_VERSION => ::Rack . release ,
48
- ::Rack ::RACK_INPUT => StringIO . new ( event [ "body" ] || "" ) ,
49
- ::Hanami ::Lambda ::LAMBDA_EVENT => event ,
50
- ::Hanami ::Lambda ::LAMBDA_CONTEXT => context
51
- } . tap do |env |
52
- content_type = headers . delete ( "Content-Type" ) ||
53
- headers . delete ( "content-type" ) ||
54
- headers . delete ( "CONTENT_TYPE" )
55
- env [ "CONTENT_TYPE" ] = content_type if content_type
56
- end . merge ( headers . transform_keys { |k | "HTTP_#{ k . upcase . tr ( '-' , '_' ) } " } )
44
+ Env . new ( event : event , headers : headers , context : context ) . to_h
57
45
end
58
46
end
59
47
end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ RSpec . describe Hanami ::Lambda ::Env do
4
+ subject ( :env ) { described_class . new ( event : event , headers : headers , context : context ) }
5
+
6
+ let ( :event ) do
7
+ {
8
+ "httpMethod" => "GET" ,
9
+ "path" => "/" ,
10
+ "body" => ""
11
+ }
12
+ end
13
+
14
+ let ( :headers ) do
15
+ {
16
+ "Content-Type" => "text/plain" ,
17
+ "X-Custom-Header" => "Custom Value"
18
+ }
19
+ end
20
+
21
+ let ( :context ) { double ( :context ) }
22
+
23
+ describe "#to_h" do
24
+ subject ( :hash ) { env . to_h }
25
+
26
+ it { is_expected . to include ( ::Hanami ::Lambda ::LAMBDA_EVENT ) }
27
+ it { is_expected . to include ( ::Hanami ::Lambda ::LAMBDA_CONTEXT ) }
28
+ it { is_expected . to include ( "CONTENT_TYPE" => "text/plain" ) }
29
+ it { is_expected . to include ( "HTTP_X_CUSTOM_HEADER" => "Custom Value" ) }
30
+ end
31
+ end
Original file line number Diff line number Diff line change 33
33
it { is_expected . to include ( body : "Hello from Rack" ) }
34
34
it { is_expected . to include ( headers : { } ) }
35
35
end
36
-
37
- describe "#env" do
38
- subject ( :env ) { rack . build_env ( event , headers , context ) }
39
-
40
- it { is_expected . to include ( ::Hanami ::Lambda ::LAMBDA_EVENT ) }
41
- it { is_expected . to include ( ::Hanami ::Lambda ::LAMBDA_CONTEXT ) }
42
- it { is_expected . to include ( "CONTENT_TYPE" => "text/plain" ) }
43
- it { is_expected . to include ( "HTTP_X_CUSTOM_HEADER" => "Custom Value" ) }
44
- end
45
36
end
You can’t perform that action at this time.
0 commit comments