Я пытаюсь реализовать базовую HTTP-аутентификацию, но мне нужно возвращать разные коды состояния HTTP в зависимости от статуса аутентифицированной учетной записи. Я использую Sinatra для размещения API, и вот как выглядит мое приложение:
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'active_record'
require 'openssl'
# Define the paths that the application responds to, and include the authentication mechanism to provide
# basic HTTP authentication using the Rack middleware.
use Rack::Auth::Basic do |username, password|
result = LoginApi::User.authenticate(username, password)
# Check the status of the user we just authenticated. If he is banned or unverified then we
# need to return special responses.
throw :halt, [403, 'User is banned'] if LoginApi::AuthUser.user.banned?
throw :halt, [412, 'User is not verified'] unless LoginApi::AuthUser.user.verified?
# Return the result if no exceptions were raised in the meanwhile
result
end
# This action responds on the root of the application and will return the basic authentication of a User
# in the headers. This is done by Rack::Auth, which requires the credentials to be sent in the initial
# call so they can be verified by the application
get '/' do
headers \
"X-UserID" => "#{LoginApi::AuthUser.user.id}|",
"X-P1" => "#{LoginApi::AuthUser.p1}",
"X-P2" => "#{LoginApi::AuthUser.p2}"
end
Таким образом, он возвращает статус 200 для успешной аутентификации, 403 для заблокированного пользователя, 412 для непроверенного пользователя и 400, если аутентификация не удалась.
Проблема в том, что я не могу заставить свои тесты правильно работать с этим. Тест для обычной проверки подлинности работает и проходит, но тесты, которые должны проверять пользовательские ответы состояния HTTP, завершаются со следующей ошибкой:
ArgumentError: uncaught throw :halt ./app.rb:23:in
throw' ./app.rb:23:in
block in ' ./features/step_definitions/when_steps.rb:8:in/^I make the call to the API$/' ./features/banned.feature:8:in
Когда я делаю вызов API'Пропущенный шаг
Может кто-нибудь объяснить, что я делаю неправильно или упускаю, чтобы заставить это работать так, как я хочу?