prepare basic auth test

master
Kanezoh 2022-01-04 09:53:04 +09:00
parent 27dccfd6e7
commit 712147aa70
3 changed files with 55 additions and 11 deletions

View File

@ -16,3 +16,8 @@ development_dependencies:
webmock:
github: manastech/webmock.cr
branch: master
kemal:
github: kemalcr/kemal
version: ~> 1.0.0
kemal-basic-auth:
github: kemalcr/kemal-basic-auth

View File

@ -1,15 +1,18 @@
require "./spec_helper"
WebMock.stub(:get, "http://auth.example.com/")
.with(headers: {"Authorization" => "Basic #{Base64.strict_encode("user:pass").chomp}"})
.to_return(status: 200)
WebMock.stub(:get, "http://auth.example.com/")
.to_return(status: 401, headers: {"WWW-Authenticate" => "Basic realm=\"Access to the staging site\", charset=\"UTF-8\""})
require "./server.cr"
#WebMock.stub(:get, "http://auth.example.com/")
# .with(headers: {"Authorization" => "Basic #{Base64.strict_encode("user:pass").chomp}"})
# .to_return(status: 200)
#
#WebMock.stub(:get, "http://auth.example.com/")
# .to_return(status: 401, headers: {"WWW-Authenticate" => "Basic realm=\"Access to the staging site\", charset=\"UTF-8\""})
describe "Mechanize HTTP Authentication test" do
# it "auth" do
# agent = Mechanize.new
# agent.get("http://auth.example.com/")
# end
WebMock.allow_net_connect = true
it "auth" do
agent = Mechanize.new
page = agent.get("#{TEST_SERVER_URL}/secret")
p page.body
WebMock.allow_net_connect = false
end
end

36
spec/server.cr Normal file
View File

@ -0,0 +1,36 @@
require "kemal"
require "kemal-basic-auth"
TEST_SERVER_HOST = "0.0.0.0"
TEST_SERVER_PORT = 4567
TEST_SERVER_URL = "http://#{TEST_SERVER_HOST}:#{TEST_SERVER_PORT}"
class BasicAuthHandler < Kemal::BasicAuth::Handler
only ["/secret"]
def call(env)
return call_next(env) unless only_match?(env)
super
end
end
add_handler BasicAuthHandler.new("username", "password")
get "/secret" do
"Authorized"
end
kemal_config = Kemal.config
kemal_config.env = "development"
kemal_config.logging = false
spawn do
Kemal.run(port: TEST_SERVER_PORT)
end
spawn do
sleep 300000
end
until Kemal.config.running
sleep 1.millisecond
end