-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspec_helper.rb
107 lines (91 loc) · 2.7 KB
/
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
require 'rubygems'
require 'bundler'
Bundler.require
require 'cgi'
require 'yaml'
require 'minitest/spec'
require 'minitest/autorun'
require "minitest/reporters"
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
# Always run specs in the same order
class Minitest::Spec
def self.test_order
:sorted
end
end
module MiniTest::Assertions
def assert_is_etag(string)
assert string.match(/^"([^"]|\\")*"/i), "Expected #{string} to be a strong ETag"
end
end
String.infect_an_assertion :assert_is_etag, :must_be_etag, :only_one_argument
begin
CONFIG = Hash[YAML.load_file('./config.yml').map{|(k,v)| [k.to_sym,v]}]
rescue Errno::ENOENT
puts "Config file missing!\n\r".red
puts "Please copy config.yml.example to config.yml and enter valid data.\n\r"
exit 1
end
def default_headers
@default_headers ||= { authorization: "Bearer #{CONFIG[:token]}" }
end
def do_network_request(path, options, &block)
begin
options[:base_url] = options[:base_url] || CONFIG[:storage_base_url]
options[:url] = "#{options[:base_url]}/#{escape(path)}"
options[:headers] = default_headers.merge(options[:headers] || {})
RestClient::Request.execute(options, &block)
rescue => e
puts "#{options[:method]} request failed with: #{e.message}"
e.response
end
end
def do_put_request(path, data, headers={}, &block)
begin
RestClient.put "#{CONFIG[:storage_base_url]}/#{escape(path)}", data,
default_headers.merge(headers), &block
rescue => e
puts "PUT request failed with: #{e.message}"
e.response
end
end
def do_get_request(path, headers={}, &block)
begin
RestClient.get "#{CONFIG[:storage_base_url]}/#{escape(path)}",
default_headers.merge(headers), &block
rescue => e
puts "GET request failed with: #{e.message}"
e.response
end
end
def do_delete_request(path, headers={}, &block)
begin
RestClient.delete "#{CONFIG[:storage_base_url]}/#{escape(path)}",
default_headers.merge(headers), &block
rescue => e
puts "DELETE request failed with: #{e.message}"
e.response
end
end
def do_head_request(path, headers={}, &block)
begin
RestClient.head "#{CONFIG[:storage_base_url]}/#{escape(path)}",
default_headers.merge(headers), &block
rescue => e
puts "HEAD request failed with: #{e.message}"
e.response
end
end
def do_options_request(path, headers={}, &block)
begin
RestClient.options "#{CONFIG[:storage_base_url]}/#{escape(path)}",
default_headers.merge(headers), &block
rescue => e
puts "OPTIONS request failed with: #{e.message}"
e.response
end
end
private
def escape(url)
CGI::escape(url).gsub('+', '%20').gsub('%2F', '/')
end