106 lines
5.5 KiB
Ruby
106 lines
5.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# spec/app_spec.rb
|
|
require_relative 'spec_helper' # Load common setup
|
|
|
|
RSpec.describe 'No as a Service' do
|
|
include Rack::Test::Methods # Rack::Test methods included via spec_helper.rb
|
|
|
|
# The `app` method is defined in spec_helper.rb, required by Rack::Test
|
|
|
|
context 'GET /' do
|
|
it 'returns a default English "no" in HTML' do
|
|
get '/'
|
|
# Debugging: puts last_response.status, last_response.body
|
|
expect(last_response.status).to eq(200), "Expected status 200, got #{last_response.status}"
|
|
expect(last_response.body).to include('No'), "Expected body to include 'No', got:\n#{last_response.body}"
|
|
expect(last_response.content_type).to include('text/html'),
|
|
"Expected HTML content type, got #{last_response.content_type}"
|
|
end
|
|
|
|
it 'returns JSON when Accept header is application/json' do
|
|
header 'Accept', 'application/json'
|
|
get '/'
|
|
expect(last_response.status).to eq(200), "Expected status 200 with Accept: JSON, got #{last_response.status}"
|
|
expect(last_response.content_type).to include('application/json'),
|
|
"Expected JSON content type with Accept: JSON, got #{last_response.content_type}"
|
|
|
|
json_response = JSON.parse(last_response.body)
|
|
expect(json_response).to be_a(Hash), "Expected JSON response to be a Hash, got #{json_response.class}"
|
|
expect(json_response['response']).to be_a(String),
|
|
"Expected 'response' key with string value, got #{json_response['response'].class}"
|
|
expect(json_response['response']).not_to be_empty, "Expected 'response' value to be non-empty"
|
|
end
|
|
|
|
it 'returns JSON when format parameter is json' do
|
|
get '/?format=json'
|
|
expect(last_response.status).to eq(200), "Expected status 200 with ?format=json, got #{last_response.status}"
|
|
expect(last_response.content_type).to include('application/json'),
|
|
"Expected JSON content type with ?format=json, got #{last_response.content_type}"
|
|
|
|
json_response = JSON.parse(last_response.body)
|
|
expect(json_response).to be_a(Hash), "Expected JSON response to be a Hash, got #{json_response.class}"
|
|
expect(json_response['response']).to be_a(String),
|
|
"Expected 'response' key with string value, got #{json_response['response'].class}"
|
|
expect(json_response['response']).not_to be_empty, "Expected 'response' value to be non-empty"
|
|
end
|
|
end
|
|
|
|
context 'GET /:lang_code' do
|
|
it 'returns "no" in Norwegian (no)' do
|
|
get '/no'
|
|
expect(last_response.status).to eq(200), "Expected status 200 for /no, got #{last_response.status}"
|
|
expect(last_response.body).to include('Nei'),
|
|
"Expected body to include 'Nei' for /no, got:\n#{last_response.body}"
|
|
expect(last_response.content_type).to include('text/html'),
|
|
"Expected HTML content type for /no, got #{last_response.content_type}"
|
|
end
|
|
|
|
it 'returns "no" in Swedish (sv)' do
|
|
get '/sv'
|
|
expect(last_response.status).to eq(200), "Expected status 200 for /sv, got #{last_response.status}"
|
|
expect(last_response.body).to include('Nej'),
|
|
"Expected body to include 'Nej' for /sv, got:\n#{last_response.body}"
|
|
expect(last_response.content_type).to include('text/html'),
|
|
"Expected HTML content type for /sv, got #{last_response.content_type}"
|
|
end
|
|
|
|
it 'returns 404 for an unsupported language code' do
|
|
get '/xyz'
|
|
expect(last_response.status).to eq(404),
|
|
"Expected status 404 for unsupported language, got #{last_response.status}"
|
|
expect(last_response.body).to include('Not Found'),
|
|
"Expected body to include 'Not Found', got:\n#{last_response.body}"
|
|
end
|
|
end
|
|
|
|
context 'GET /languages' do
|
|
it 'lists all supported languages in JSON' do
|
|
get '/languages'
|
|
expect(last_response.status).to eq(200), "Expected status 200 for /languages, got #{last_response.status}"
|
|
expect(last_response.content_type).to include('application/json'),
|
|
"Expected JSON content type for /languages, got #{last_response.content_type}"
|
|
|
|
json_response = JSON.parse(last_response.body)
|
|
expect(json_response).to be_an(Array), "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
|
|
actual_languages = json_response.sort
|
|
|
|
expect(actual_languages.size).to eq(expected_languages.size),
|
|
"Expected #{expected_languages.size} languages, got #{actual_languages.size}"
|
|
expect(actual_languages).to eq(expected_languages), 'Language list mismatch.'
|
|
end
|
|
end
|
|
|
|
context 'GET /health' do
|
|
it 'returns OK' do
|
|
get '/health'
|
|
expect(last_response.status).to eq(200), "Expected status 200 for /health, got #{last_response.status}"
|
|
expect(last_response.body).to eq('OK'), "Expected body 'OK' for /health, got '#{last_response.body}'"
|
|
expect(last_response.content_type).to include('text/plain'),
|
|
"Expected text/plain content type for /health, got #{last_response.content_type}"
|
|
end
|
|
end
|
|
end
|