Skip to content

Instantly share code, notes, and snippets.

@matsubo
Created March 25, 2023 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsubo/ba0db8ba1386119ff0ffff1142a038ba to your computer and use it in GitHub Desktop.
Save matsubo/ba0db8ba1386119ff0ffff1142a038ba to your computer and use it in GitHub Desktop.
diff --git a/Gemfile b/Gemfile
index ddc9c44..d824388 100644
--- a/Gemfile
+++ b/Gemfile
@@ -96,4 +96,6 @@ group :test do
gem 'capybara-screenshot'
gem 'rspec-rails'
+ gem "timecop"
end
+
diff --git a/Gemfile.lock b/Gemfile.lock
index 93812e6..68ebe2c 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -275,6 +275,7 @@ GEM
tailwindcss-rails (2.0.21-x86_64-linux)
railties (>= 6.0.0)
thor (1.2.1)
+ timecop (0.9.6)
timeout (0.3.1)
turbo-rails (1.3.2)
actionpack (>= 6.0.0)
@@ -301,6 +302,7 @@ GEM
zeitwerk (2.6.6)
PLATFORMS
+ aarch64-linux
x86_64-linux
DEPENDENCIES
@@ -336,6 +338,7 @@ DEPENDENCIES
sprockets-rails
stimulus-rails
tailwindcss-rails
+ timecop
turbo-rails
tzinfo-data
web-console
diff --git a/app/controllers/healthcheck_controller.rb b/app/controllers/healthcheck_controller.rb
new file mode 100644
index 0000000..584b1f5
--- /dev/null
+++ b/app/controllers/healthcheck_controller.rb
@@ -0,0 +1,6 @@
+class HealthcheckController < ApplicationController
+ def index
+ current_time = Time.now.to_i
+ render json: { time: current_time }
+ end
+end
diff --git a/app/helpers/healthcheck_helper.rb b/app/helpers/healthcheck_helper.rb
new file mode 100644
index 0000000..6c52fff
--- /dev/null
+++ b/app/helpers/healthcheck_helper.rb
@@ -0,0 +1,2 @@
+module HealthcheckHelper
+end
diff --git a/config/routes.rb b/config/routes.rb
index fcccb90..ad1e9d4 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -18,4 +18,5 @@ Rails.application.routes.draw do
root to: 'home#index'
get '/doc' => 'home#doc', :format => false
get '/doc/redoc' => 'home#redoc', :format => false
+ get '/healthcheck', to: 'healthcheck#index', defaults: { format: 'json' }
end
diff --git a/spec/controllers/healthcheck_controller_spec.rb b/spec/controllers/healthcheck_controller_spec.rb
new file mode 100644
index 0000000..0093de0
--- /dev/null
+++ b/spec/controllers/healthcheck_controller_spec.rb
@@ -0,0 +1,28 @@
+require 'rails_helper'
+
+RSpec.describe HealthcheckController, type: :controller do
+ describe 'GET #index' do
+ before do
+ Timecop.freeze(Time.zone.local(2023, 1, 1, 12, 0, 0))
+ end
+
+ after do
+ Timecop.return
+ end
+
+ it 'returns http success' do
+ get :index
+ expect(response).to have_http_status(:success)
+ end
+
+ it 'returns current time in unix timestamp format' do
+ get :index
+ json_response = JSON.parse(response.body)
+ current_time = Time.now.to_i
+ returned_time = json_response['time']
+
+ expect(returned_time).to eq(current_time)
+ end
+ end
+end
+
diff --git a/test/controllers/healthcheck_controller_test.rb b/test/controllers/healthcheck_controller_test.rb
new file mode 100644
index 0000000..e2a212a
--- /dev/null
+++ b/test/controllers/healthcheck_controller_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class HealthcheckControllerTest < ActionDispatch::IntegrationTest
+ # test "the truth" do
+ # assert true
+ # end
+end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment