File: web_mock_helper.rb

package info (click to toggle)
ruby-jekyll-github-metadata 2.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: ruby: 2,355; javascript: 107; sh: 41; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 1,732 bytes parent folder | download | duplicates (2)
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
# 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 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