96 lines
4.9 KiB
Ruby
96 lines
4.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# test/app_test.rb
|
|
require_relative 'test_helper' # Load common setup
|
|
|
|
class AppTest < Minitest::Test
|
|
include Rack::Test::Methods # Include Rack::Test methods
|
|
|
|
# The `app` method is defined in test_helper.rb, required by Rack::Test
|
|
|
|
def test_root_path_returns_default_english_no
|
|
get '/'
|
|
# Debugging: puts last_response.status, last_response.body
|
|
assert last_response.ok?, "Expected status 200, got #{last_response.status}"
|
|
assert_includes last_response.body, 'No', "Expected body to include 'No', got:\n#{last_response.body}"
|
|
assert_includes last_response.content_type, 'text/html',
|
|
"Expected HTML content type, got #{last_response.content_type}"
|
|
end
|
|
|
|
def test_language_path_returns_specific_language_no
|
|
# Test Norwegian
|
|
get '/no'
|
|
assert last_response.ok?, "Expected status 200 for /no, got #{last_response.status}"
|
|
assert_includes last_response.body, 'Nei', "Expected body to include 'Nei' for /no, got:\n#{last_response.body}"
|
|
assert_includes last_response.content_type, 'text/html',
|
|
"Expected HTML content type for /no, got #{last_response.content_type}"
|
|
|
|
# Test Swedish
|
|
get '/sv'
|
|
assert last_response.ok?, "Expected status 200 for /sv, got #{last_response.status}"
|
|
assert_includes last_response.body, 'Nej', "Expected body to include 'Nej' for /sv, got:\n#{last_response.body}"
|
|
assert_includes last_response.content_type, 'text/html',
|
|
"Expected HTML content type for /sv, got #{last_response.content_type}"
|
|
end
|
|
|
|
def test_unsupported_language_returns_not_found
|
|
get '/xyz' # Non-existent language
|
|
assert_equal 404, last_response.status, "Expected status 404 for unsupported language, got #{last_response.status}"
|
|
# Asserting a general 'Not Found' message. Adjust if your 404 page has a specific string.
|
|
assert_includes last_response.body, 'Not Found', "Expected body to include 'Not Found', got:\n#{last_response.body}"
|
|
end
|
|
|
|
def test_lists_all_supported_languages_in_json
|
|
get '/languages'
|
|
assert last_response.ok?, "Expected status 200 for /languages, got #{last_response.status}"
|
|
assert_includes last_response.content_type, 'application/json',
|
|
"Expected JSON content type for /languages, got #{last_response.content_type}"
|
|
|
|
# Ensure JSON parsing is successful
|
|
json_response = JSON.parse(last_response.body)
|
|
assert_kind_of Array, json_response, "Expected JSON response to be an Array, got #{json_response.class}"
|
|
|
|
# Check for a subset of expected languages. Add all languages from your responses.yml.
|
|
expected_languages = %w[en no sv fi is da fo].sort # Sort for consistent comparison if order isn't guaranteed
|
|
actual_languages = json_response.sort
|
|
|
|
assert_equal expected_languages.size, actual_languages.size,
|
|
"Expected #{expected_languages.size} languages, got #{actual_languages.size}"
|
|
assert_equal expected_languages, actual_languages, 'Language list mismatch.'
|
|
end
|
|
|
|
def test_returns_json_when_accept_header_is_json
|
|
header 'Accept', 'application/json' # Set the Accept header
|
|
get '/'
|
|
assert last_response.ok?, "Expected status 200 with Accept: JSON, got #{last_response.status}"
|
|
assert_includes last_response.content_type, 'application/json',
|
|
"Expected JSON content type with Accept: JSON, got #{last_response.content_type}"
|
|
|
|
json_response = JSON.parse(last_response.body)
|
|
assert_kind_of Hash, json_response, "Expected JSON response to be a Hash, got #{json_response.class}"
|
|
assert json_response['response'].is_a?(String),
|
|
"Expected 'response' key with string value, got #{json_response['response'].class}"
|
|
assert json_response['response'].length.positive?, "Expected 'response' value to be non-empty"
|
|
end
|
|
|
|
def test_returns_json_when_format_param_is_json
|
|
get '/?format=json' # Use the format parameter
|
|
assert last_response.ok?, "Expected status 200 with ?format=json, got #{last_response.status}"
|
|
assert_includes last_response.content_type, 'application/json',
|
|
"Expected JSON content type with ?format=json, got #{last_response.content_type}"
|
|
|
|
json_response = JSON.parse(last_response.body)
|
|
assert_kind_of Hash, json_response, "Expected JSON response to be a Hash, got #{json_response.class}"
|
|
assert json_response['response'].is_a?(String),
|
|
"Expected 'response' key with string value, got #{json_response['response'].class}"
|
|
assert json_response['response'].length.positive?, "Expected 'response' value to be non-empty"
|
|
end
|
|
|
|
def test_health_check_returns_ok
|
|
get '/health'
|
|
assert last_response.ok?, "Expected status 200 for /health, got #{last_response.status}"
|
|
assert_equal 'OK', last_response.body, "Expected body 'OK' for /health, got '#{last_response.body}'"
|
|
assert_includes last_response.content_type, 'text/plain',
|
|
"Expected text/plain content type for /health, got #{last_response.content_type}"
|
|
end
|
|
end
|