From 61ec79aca5aecbb71ce13195e0cb9c22ce5e784b Mon Sep 17 00:00:00 2001 From: Yuki Nishijima Date: Mon, 14 Apr 2014 19:22:02 -0700 Subject: [PATCH] Add support for json output for statuses --- Gemfile | 1 + Gemfile.lock | 4 +++ app/views/status/show.json.jbuilder | 13 +++++++++ test/integration/status_json_test.rb | 43 ++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 app/views/status/show.json.jbuilder create mode 100644 test/integration/status_json_test.rb diff --git a/Gemfile b/Gemfile index 101513b..1633ad9 100644 --- a/Gemfile +++ b/Gemfile @@ -12,6 +12,7 @@ gem 'sass-rails' gem 'coffee-rails' gem 'uglifier' gem 'rails_12factor' +gem 'jbuilder' gem 'rake' gem 'mocha', :require => false diff --git a/Gemfile.lock b/Gemfile.lock index c4009a6..85bf2dd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -38,6 +38,9 @@ GEM execjs (2.2.1) hike (1.2.3) i18n (0.6.11) + jbuilder (2.0.6) + activesupport (>= 3.0.0, < 5) + multi_json (~> 1.2) jquery-rails (3.1.1) railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) @@ -109,6 +112,7 @@ PLATFORMS DEPENDENCIES coffee-rails + jbuilder jquery-rails json mocha diff --git a/app/views/status/show.json.jbuilder b/app/views/status/show.json.jbuilder new file mode 100644 index 0000000..5ab68a1 --- /dev/null +++ b/app/views/status/show.json.jbuilder @@ -0,0 +1,13 @@ +json.status @status + +json.services @pings do |ping| + json.name ping.service + json.status ping.state + json.description ping.description + + if !ping.unknown? + json.last_seen ping.last_seen.iso8601 + else + json.last_seen "unknown" + end +end diff --git a/test/integration/status_json_test.rb b/test/integration/status_json_test.rb new file mode 100644 index 0000000..4c38f60 --- /dev/null +++ b/test/integration/status_json_test.rb @@ -0,0 +1,43 @@ +require 'test_helper' + +class StatusJsonTest < ActionDispatch::IntegrationTest + def json_response + JSON.parse(response.body) + end + + test "Status API: GET / when all services are up" do + Ping.create! status: "up", service: "Application", description: "This service is up", last_seen: Time.new(2014, 3, 14, 1, 59, 26).utc + Ping.create! status: "up", service: "Core API", description: "This service is up", last_seen: Time.new(2014, 2, 7, 1, 8, 28).utc + + get "/", {}, {"Accept" => "application/json"} + + service = json_response["services"].find { |s| s["name"] == "Core API" } + + assert_match "up", json_response["status"] + assert_match "up", json_response["services"][0]["status"] + assert_match "This service is up", json_response["services"][0]["descrption"] + assert_match "2014-03-14T08:59:26Z", json_response["services"][0]["last_seen"] + end + + test "Status API: GET / when some of the services are down" do + Ping.create! status: "down", service: "Application", last_seen: Time.new(2014, 3, 14, 1, 59, 26).utc + Ping.create! status: "up", service: "Core API", last_seen: Time.new(2014, 2, 7, 1, 8, 28).utc + + get "/", {}, {"Accept" => "application/json"} + + assert_match "partial", json_response["status"] + assert_match "down", json_response["services"][0]["status"] + assert_match "up", json_response["services"][1]["status"] + end + + test "Status API: GET / when all of the services are down" do + Ping.create! status: "down", service: "Application", last_seen: Time.new(2014, 3, 14, 1, 59, 26).utc + Ping.create! status: "down", service: "Core API", last_seen: Time.new(2014, 2, 7, 1, 8, 28).utc + + get "/", {}, {"Accept" => "application/json"} + + assert_match "down", json_response["status"] + assert_match "down", json_response["services"][0]["status"] + assert_match "down", json_response["services"][1]["status"] + end +end