-
Notifications
You must be signed in to change notification settings - Fork 130
/
web_mock_helper.rb
65 lines (56 loc) · 1.83 KB
/
web_mock_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
# frozen_string_literal: true
module WebMockHelper
REQUEST_HEADERS = {
"Accept" => %r!application/vnd\.github\.(v3|drax-preview|mercy-preview)\+json!,
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
"Content-Type" => "application/json",
"User-Agent" => "Octokit Ruby Gem #{Octokit::VERSION}",
}.freeze
RESPONSE_HEADERS = {
"Transfer-Encoding" => "chunked",
"Content-Type" => "application/json; charset=utf-8",
"Vary" => "Accept-Encoding",
"Content-Encoding" => "gzip",
"X-GitHub-Media-Type" => "github.v3; format=json",
}.freeze
def stub_api(path, filename, req_headers = {})
WebMock.disable_net_connect!
stub_request(:get, url(path))
.with(:headers => request_headers.merge(req_headers))
.to_return(
:status => 200,
:headers => RESPONSE_HEADERS,
:body => webmock_data(filename)
)
end
def stub_api_404(path, req_headers = {})
WebMock.disable_net_connect!
stub_request(:get, url(path))
.with(:headers => request_headers.merge(req_headers))
.to_return(
:status => 404,
:headers => RESPONSE_HEADERS
)
end
def expect_api_call(path)
expect(WebMock).to have_requested(:get, url(path))
.with(:headers => request_headers).once
end
def not_expect_api_call(path)
expect(WebMock).to have_requested(:get, url(path))
.with(:headers => request_headers).times(0)
end
def request_headers
REQUEST_HEADERS.merge(
"Authorization" => "token #{ENV.fetch("JEKYLL_GITHUB_TOKEN", "1234abc")}"
)
end
private
def url(path)
"#{Jekyll::GitHubMetadata::Pages.api_url}#{path}"
end
def webmock_data(filename)
@webmock_data ||= {}
@webmock_data[filename] ||= SPEC_DIR.join("webmock/api_get_#{filename}.json").read
end
end