-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgithub_test_helper.rb
More file actions
103 lines (76 loc) · 2.21 KB
/
github_test_helper.rb
File metadata and controls
103 lines (76 loc) · 2.21 KB
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
require 'FileHelpers'
require 'git-process/git_process'
require 'webmock/rspec'
################
#
# Monkey-patch WebMock to stop screwing with the capitalization of resource headers
#
################
module WebMock
module Util
class Headers
def self.normalize_headers(headers)
headers
end
end
end
end
module Net::HTTPHeader
def add_field(key, val)
if @header.key?(key)
@header[key].push val
else
@header[key] = [val]
end
end
end
module GitHubTestHelper
def stub_get(url, opts = {})
stub = stub_request(:get, url)
if opts[:token]
stub.with(:Authorization => "token #{opts[:token]}")
end
stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => to_body(opts[:body]))
stub
end
def stub_post(url, opts = {})
stub = stub_request(:post, url)
with_headers = opts[:headers] || {}
if opts[:token]
stub.with(:Authorization => "token #{opts[:token]}")
end
if opts[:send]
stub.with(:body => to_body(opts[:send]))
end
if opts[:two_factor]
# noinspection RubyStringKeysInHashInspection
with_headers.merge!({'X-GitHub-OTP'.downcase => [opts[:two_factor]]})
end
if opts[:body]
# noinspection RubyStringKeysInHashInspection
opts[:response_headers] = {'Content-Type' => 'application/json'}.merge(opts[:response_headers] || {})
end
stub.with(:headers => with_headers) unless with_headers.empty?
stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => to_body(opts[:body]), :headers => opts[:response_headers] ? opts[:response_headers] : {})
return stub
end
def stub_patch(url, opts = {})
stub = stub_request(:patch, url)
if opts[:token]
stub.with(:Authorization => "token #{opts[:token]}")
end
if opts[:send]
stub.with(:body => to_body(opts[:send]))
end
stub.to_return(:status => opts[:status] ? opts[:status] : 200, :body => to_body(opts[:body]))
stub
end
def to_body(body)
return '' if body.nil?
return body if body.is_a? String
if body.is_a? Hash or body.is_a? Array
return JSON(body).to_s
end
raise "Do not know what to do with #{body.class} #{body}"
end
end