forked from songkick/oauth2-provider
/
request_helpers.rb
62 lines (49 loc) · 1.82 KB
/
request_helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require "socket"
module RequestHelpers
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("&")
end
def get(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:#{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:#{SERVER_PORT}/authorize"
Net::HTTP.post_form(URI.parse(url), query_params)
end
def post(body_params, 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))
end
end
def validate_response(response, status, body)
response.code.to_i.should == status
response.body.should == body
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"
end
def mock_request(request_class, stubs = {})
mock_request = mock(request_class)
method_stubs = {
:redirect? => false,
:response_body => nil,
:response_headers => {},
:response_status => 200
}.merge(stubs)
method_stubs.each do |method, value|
mock_request.should_receive(method).and_return(value)
end
mock_request
end
end