test/integration/server.rb in roar-0.12.2 vs test/integration/server.rb in roar-0.12.3
- old
+ new
@@ -1,9 +1,10 @@
require "bundler/setup"
require "sinatra"
require "ostruct"
require "roar/representer/json"
+require "sinatra/multi_route"
require File.expand_path("../band_representer.rb", __FILE__)
class Band
attr_reader :name, :label
@@ -73,5 +74,36 @@
end
delete '/bands/metallica' do
status 204
end
+
+
+helpers do
+ def protected!
+ return if authorized?
+ headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
+ halt 401, "Not authorized\n"
+ end
+
+ def authorized?
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
+ @auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['admin', 'password']
+ end
+end
+
+route :get, :post, :put, :delete, "/protected/bands/bodyjar" do
+ protected!
+
+ OpenStruct.new(:name => "Bodyjar").
+ extend(Integration::BandRepresenter).
+ to_json
+end
+
+route :get, :post, :put, :delete, "/cookies" do
+ raise "No cookies!" unless request.env["HTTP_COOKIE"] == "Yumyum"
+ %{{"name": "Bodyjar"}}
+end
+
+get "/ping" do
+ "1"
+end
\ No newline at end of file