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

Update hello world ruby template #860

Closed
wants to merge 5 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Cookiecutter SAM for Ruby Lambda functions

This is a [Cookiecutter](https://github.com/audreyr/cookiecutter) template to create a Serverless Hello World App based on Serverless Application Model (SAM) and Python.
This is a [Cookiecutter](https://github.com/audreyr/cookiecutter) template to create a Serverless Hello World App based on Serverless Application Model (SAM) and Ruby.

## Recommendations

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
source "https://rubygems.org"

gem "httparty"

group :test do
gem "test-unit"
gem "mocha"
end
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ rvm --default use 2.5.0
```bash
gem install bundler
bundle install
bundle install --deployment --path hello_world/vendor/bundle
bundle install --deployment --path hello_world/vendor/bundle --without test
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lauratpa should bundle install also skip development?

bundle install --deployment --path hello_world/vendor/bundle --without development test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it makes sense that one does not want development gems in production deployment. But I didn't add it because the example Gemfile does not have development group and I don't want to confuse anyone. Makes sense?

```

* Step 1 installs ```bundler```which provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.
Expand Down Expand Up @@ -124,12 +124,10 @@ aws cloudformation describe-stacks \

## Testing

We use [Mocha](http://gofreerange.com/mocha/docs) for testing our code and you can install it using gem: ``gem install mocha``

Next, we run our initial unit tests:
Run our initial unit tests:

```bash
ruby tests/unit/test_hello.rb
bundle exec ruby tests/unit/test_handler.rb
```

**NOTE**: It is recommended to use a Ruby Version Manager to manage, and work with multiple ruby environments from interpreters to sets of gems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def lambda_handler(event:, context:)
puts error.inspect
raise error
end
return {

{
:statusCode => response.code,
:body => {
:message => "Hello World!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Description: >
{{ cookiecutter.project_name }}

Sample SAM Template for {{ cookiecutter.project_name }}

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require 'json'
require 'test/unit'
require 'mocha/test_unit'

require_relative '../../hello_world/app'

class HelloWorldTest < Test::Unit::TestCase

def setup
@event = {
def event
{
body: 'eyJ0ZXN0IjoiYm9keSJ9',
resource: '/{proxy+}',
path: '/path/to/resource',
Expand All @@ -22,44 +22,44 @@ def setup
baz: 'qux'
},
headers: {
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
Accept-Encoding: 'gzip, deflate, sdch',
Accept-Language: 'en-US,en;q=0.8',
Cache-Control: 'max-age=0',
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: 'US',
Host: '1234567890.execute-api.us-east-1.amazonaws.com',
Upgrade-Insecure-Requests: '1',
User-Agent: 'Custom User Agent String',
Via: '1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)',
X-Amz-Cf-Id: 'cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==',
X-Forwarded-For: '127.0.0.1, 127.0.0.2',
X-Forwarded-Port: '443',
X-Forwarded-Proto: 'https'
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding' => 'gzip, deflate, sdch',
'Accept-Language' => 'en-US,en;q=0.8',
'Cache-Control' => 'max-age=0',
'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' => 'US',
'Host' => '1234567890.execute-api.us-east-1.amazonaws.com',
'Upgrade-Insecure-Requests' => '1',
'User-Agent' => 'Custom User Agent String',
'Via' => '1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)',
'X-Amz-Cf-Id' => 'cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==',
'X-Forwarded-For' => '127.0.0.1, 127.0.0.2',
'X-Forwarded-Port' => '443',
'X-Forwarded-Proto' => 'https'
},
requestContext: {
accountId: '123456789012',
resourceId: '123456',
stage: 'prod',
requestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
requestTime: '09/Apr/2015:12:34:56 +0000',
requestTimeEpoch: 1428582896000,
requestTimeEpoch: 1_428_582_896_000,
identity: {
cognitoIdentityPoolId: "null",
accountId: "null",
cognitoIdentityId: "null",
caller: "null",
accessKey: "null",
cognitoIdentityPoolId: 'null',
accountId: 'null',
cognitoIdentityId: 'null',
caller: 'null',
accessKey: 'null',
sourceIp: '127.0.0.1',
cognitoAuthenticationType: "null",
cognitoAuthenticationProvider: "null",
userArn: "null",
cognitoAuthenticationType: 'null',
cognitoAuthenticationProvider: 'null',
userArn: 'null',
userAgent: 'Custom User Agent String',
user: "null"
user: 'null'
},
path: '/prod/path/to/resource',
resourcePath: '/{proxy+}',
Expand All @@ -68,24 +68,28 @@ def setup
protocol: 'HTTP/1.1'
}
}
end

@mock_response = {
:statusCode => 200,
:body => {
message: "Hello World!",
location: "1.1.1.1"
def mock_response
Object.new.tap do |mock|
mock.expects(:code).returns(200)
mock.expects(:body).returns('1.1.1.1')
end
end

def expected_result
{
statusCode: 200,
body: {
message: 'Hello World!',
location: '1.1.1.1'
}.to_json
}

end

def test_lambda_handler
expects(:lambda_handler).with(event:@event, context:"").returns(@mock_response)
response = lambda_handler(event:@event, context:"")
json_body = JSON.parse(response[:body])
HTTParty.expects(:get).with('http://checkip.amazonaws.com/').returns(mock_response)

assert_equal(200, response[:statusCode])
assert_equal("Hello World!", json_body["message"])
assert_equal("1.1.1.1", json_body["location"])
assert_equal(lambda_handler(event: event, context: ''), expected_result)
end
end