Permalink
Browse files

Pick a free port dynamically to run the tests.

  • Loading branch information...
1 parent ae8ded3 commit 764585a608333c6209fee016a3d321913c0fe734 @jcoglan jcoglan committed Dec 10, 2012
Showing with 20 additions and 16 deletions.
  1. +14 −10 spec/request_helpers.rb
  2. +6 −6 spec/songkick/oauth2/provider_spec.rb
View
24 spec/request_helpers.rb
@@ -1,42 +1,46 @@
+require "socket"
+
module RequestHelpers
- require 'net/http'
+ require "net/http"
+
+ SERVER_PORT = TCPServer.new(0).addr[1]
def querystring(params)
- params.map { |k,v| "#{ CGI.escape k.to_s }=#{ CGI.escape v.to_s }" }.join('&')
+ params.map { |k,v| "#{ CGI.escape k.to_s }=#{ CGI.escape v.to_s }" }.join("&")
end
def get(query_params)
- uri = URI.parse('http://localhost:8000/authorize?' + querystring(query_params))
+ uri = URI.parse("http://localhost:#{SERVER_PORT}/authorize?" + querystring(query_params))
Net::HTTP.get_response(uri)
end
def allow_or_deny(query_params)
- Net::HTTP.post_form(URI.parse('http://localhost:8000/allow'), query_params)
+ Net::HTTP.post_form(URI.parse("http://localhost:#{SERVER_PORT}/allow"), query_params)
end
def post_basic_auth(auth_params, query_params)
- url = "http://#{ auth_params['client_id'] }:#{ auth_params['client_secret'] }@localhost:8000/authorize"
+ url = "http://#{ auth_params["client_id"] }:#{ auth_params["client_secret"] }@localhost:#{SERVER_PORT}/authorize"
Net::HTTP.post_form(URI.parse(url), query_params)
end
def post(body_params, query_params = {})
- uri = URI.parse('http://localhost:8000/authorize?' + querystring(query_params))
+ uri = URI.parse("http://localhost:#{SERVER_PORT}/authorize?" + querystring(query_params))
Net::HTTP.start(uri.host, uri.port) do |http|
- http.post(uri.path + '?' + uri.query.to_s, querystring(body_params))
+ http.post(uri.path + "?" + uri.query.to_s, querystring(body_params))
end
end
def validate_response(response, status, body)
response.code.to_i.should == status
response.body.should == body
- response['Cache-Control'].should == 'no-store'
+ response["Cache-Control"].should == "no-store"
end
def validate_json_response(response, status, body)
response.code.to_i.should == status
JSON.parse(response.body).should == body
- response['Content-Type'].should == 'application/json'
- response['Cache-Control'].should == 'no-store'
+ response["Content-Type"].should == "application/json"
+ response["Cache-Control"].should == "no-store"
end
def mock_request(request_class, stubs = {})
View
12 spec/songkick/oauth2/provider_spec.rb
@@ -1,7 +1,9 @@
require 'spec_helper'
describe Songkick::OAuth2::Provider do
- before(:all) { TestApp::Provider.start(8000) }
+ include RequestHelpers
+
+ before(:all) { TestApp::Provider.start(RequestHelpers::SERVER_PORT) }
after(:all) { TestApp::Provider.stop }
let(:params) { { 'response_type' => 'code',
@@ -14,8 +16,6 @@
@owner = TestApp::User['Bob']
end
- include RequestHelpers
-
describe "access grant request" do
shared_examples_for "asks for user permission" do
it "creates an authorization" do
@@ -530,7 +530,7 @@
describe "for header-based requests" do
def request(path, params = {})
access_token = params.delete('oauth_token')
- http = Net::HTTP.new('localhost', 8000)
+ http = Net::HTTP.new('localhost', RequestHelpers::SERVER_PORT)
qs = params.map { |k,v| "#{ CGI.escape k.to_s }=#{ CGI.escape v.to_s }" }.join('&')
header = {'Authorization' => "OAuth #{access_token}"}
http.request_get(path + '?' + qs, header)
@@ -542,7 +542,7 @@ def request(path, params = {})
describe "for GET requests" do
def request(path, params = {})
qs = params.map { |k,v| "#{ CGI.escape k.to_s }=#{ CGI.escape v.to_s }" }.join('&')
- uri = URI.parse('http://localhost:8000' + path + '?' + qs)
+ uri = URI.parse("http://localhost:#{RequestHelpers::SERVER_PORT}" + path + '?' + qs)
Net::HTTP.get_response(uri)
end
@@ -551,7 +551,7 @@ def request(path, params = {})
describe "for POST requests" do
def request(path, params = {})
- Net::HTTP.post_form(URI.parse('http://localhost:8000' + path), params)
+ Net::HTTP.post_form(URI.parse("http://localhost:#{RequestHelpers::SERVER_PORT}" + path), params)
end
it_should_behave_like "protected resource"

0 comments on commit 764585a

Please sign in to comment.