-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.rb
138 lines (114 loc) · 3.01 KB
/
test.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
require 'minitest'
require 'minitest/autorun'
require 'execjs'
require 'webrick'
class TestCurlToGo < Minitest::Test
JS_CONTEXT = ExecJS.compile(File.read("resources/js/curl-to-ruby.js"))
def test_simple_get
assert_curl_eq "/"
assert_curl_eq "/foo.txt"
end
def test_post
assert_curl_eq "/", '-X POST -d "foo"'
assert_curl_eq "/", '-X POST -d "foobar"'
end
def test_headers
assert_curl_eq "/", '-XPOST -H MyCrazyHeader:foo -d "foo"'
end
def test_basic_auth
assert_curl_eq "/", "-u banana:coconuts"
end
def test_complex_post
assert_curl_eq "/", <<-CURL
-X POST \
-u API_KEY: \
-d 'shipment[to_address][id]=adr_HrBKVA85' \
-d 'shipment[from_address][id]=adr_VtuTOj7o' \
-d 'shipment[parcel][id]=prcl_WDv2VzHp' \
-d 'shipment[is_return]=true' \
-d 'shipment[customs_info][id]=cstinfo_bl5sE20Y'
CURL
end
def test_simple_data
assert_curl_eq "/", "-d foo=bar"
end
def test_data_file_reference
assert_curl_eq "/", "-d @README.md"
end
def test_query_string
assert_curl_eq "/curl-to-ruby/?foo=bar", "-d @README.md"
end
private
def assert_curl_eq(path, curl_args="")
curl_req = normalize_request capture_http { |url|
system "curl -s -o /dev/null #{url}#{path} #{curl_args}"
}
ruby_req = normalize_request capture_http { |url|
ruby = curl_to_ruby("curl #{url}#{path} #{curl_args}")
eval(ruby)
}
assert_equal curl_req.verb, ruby_req.verb
assert_equal curl_req.path, ruby_req.path
if curl_req.body.nil?
assert_nil ruby_req.body
else
assert_equal curl_req.body, ruby_req.body
end
assert_equal curl_req.headers, ruby_req.headers
end
Request = Struct.new(:verb, :path, :headers, :body)
def curl_to_ruby(cmd)
JS_CONTEXT.call("curlToRuby.default", cmd)
end
def normalize_request(original)
headers = original.headers.dup
headers.delete("accept-encoding")
headers.delete("user-agent")
headers.delete("host")
headers.delete("expect")
if original.body
body = original.body
if headers['content-type'] == ['application/x-www-form-urlencoded']
# May encode slightly differently
begin
body = URI.decode_www_form(body).sort
headers.delete("content-length")
rescue ArgumentError
end
end
end
Request.new(
original.verb,
original.path,
headers,
body
)
end
def capture_http
server = WEBrick::HTTPServer.new(
Port: 0,
Logger: WEBrick::Log.new("/dev/null"),
AccessLog: []
)
port = server[:Port]
request = nil
thread = Thread.new do
server.mount_proc('/') do |req, res|
request = Request.new(
req.request_method,
req.path,
req.header,
req.body
)
res.body = "hello, world"
server.shutdown
end
server.start
request
end
url = "http://127.0.0.1:#{port}"
yield url, port
thread.join
thread.value
end
end