forked from bblimke/webmock
/
curb_spec_helper.rb
144 lines (118 loc) · 3.12 KB
/
curb_spec_helper.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require 'ostruct'
module CurbSpecHelper
def http_request(method, uri, options = {}, &block)
uri = Addressable::URI.heuristic_parse(uri)
body = options[:body]
curl = curb_http_request(uri, method, body, options)
status, response_headers =
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(curl.header_str)
# Deal with the fact that the HTTP spec allows multi-values headers
# to either be a single entry with a comma-separated listed of
# values, or multiple separate entries
response_headers.keys.each do |k|
v = response_headers[k]
if v.is_a?(Array)
response_headers[k] = v.join(', ')
end
end
OpenStruct.new(
:body => curl.body_str,
:headers => WebMock::Util::Headers.normalize_headers(response_headers),
:status => curl.response_code.to_s,
:message => status
)
end
def setup_request(uri, curl, options={})
curl ||= Curl::Easy.new
curl.url = uri.omit(:userinfo).to_s
curl.username = uri.user
curl.password = uri.password
curl.timeout = 30
curl.connect_timeout = 30
if headers = options[:headers]
headers.each {|k,v| curl.headers[k] = v }
end
curl
end
def client_timeout_exception_class
Curl::Err::TimeoutError
end
def connection_refused_exception_class
Curl::Err::ConnectionFailedError
end
def http_library
:curb
end
module DynamicHttp
def curb_http_request(uri, method, body, options)
curl = setup_request(uri, nil, options)
case method
when :post
curl.post_body = body
when :put
curl.put_data = body
end
curl.http(method)
curl
end
end
module NamedHttp
def curb_http_request(uri, method, body, options)
curl = setup_request(uri, nil, options)
case method
when :put, :post
curl.send( "http_#{method}", body )
else
curl.send( "http_#{method}" )
end
curl
end
end
module Perform
def curb_http_request(uri, method, body, options)
curl = setup_request(uri, nil, options)
case method
when :post
curl.post_body = body
when :put
curl.put_data = body
when :head
curl.head = true
when :delete
curl.delete = true
end
curl.perform
curl
end
end
module ClassNamedHttp
def curb_http_request(uri, method, body, options)
args = ["http_#{method}", uri]
args << body if method == :post || method == :put
c = Curl::Easy.send(*args) do |curl|
setup_request(uri, curl, options)
end
c
end
end
module ClassPerform
def curb_http_request(uri, method, body, options)
args = ["http_#{method}", uri]
args << body if method == :post || method == :put
c = Curl::Easy.send(*args) do |curl|
setup_request(uri, curl, options)
case method
when :post
curl.post_body = body
when :put
curl.put_data = body
when :head
curl.head = true
when :delete
curl.delete = true
end
end
c
end
end
end