summaryrefslogtreecommitdiff
path: root/test/support/http_stub_helper.rb
blob: 1710b237ef4e60f20d2c17c5c1fdfab206139d7a (plain)
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
require 'nickserver/reel_server'
require 'nickserver/email_address'

module HttpStubHelper
  protected

  def stubbing_http
    Nickserver::ReelServer::DEFAULT_ADAPTER_CLASS.stub :new, adapter do
      yield
    end
    adapter.verify
  end

  def stub_nicknym_available_response(domain, response = {})
    stub_http_get "https://#{domain}/provider.json",
                  response,
                  Hash
  end

  def stub_sks_vindex_reponse(uid, response = {})
    stub_http_get config.hkp_url,
                  response,
                  query: vindex_query(uid)
  end

  def vindex_query(uid)
    { op: 'vindex',
      search: uid,
      exact: 'on',
      options: 'mr',
      fingerprint: 'on' }
  end

  def stub_sks_get_reponse(key_id, response = {})
    stub_http_get config.hkp_url,
                  response,
                  query: sks_get_query(key_id)
  end

  def sks_get_query(key_id)
    { op: 'get',
      search: '0x' + key_id,
      exact: 'on',
      options: 'mr' }
  end

  def stub_couch_response(uid, response = {})
    query = "\?key=#{"%22#{uid}%22"}&reduce=false"
    stub_http_get(/#{Regexp.escape(config.couch_url)}.*#{query}/, response)
  end

  def stub_wkd_response(uid, response = {})
    email = Nickserver::EmailAddress.new(uid)
    url = Nickserver::Wkd::Url.new(email)
    stub_http_get url, response, Hash
  end

  def stub_http_get(url, response, options = nil)
    response = { status: 200, body: '' }.merge(response || {})
    adapter.expect :get, [response[:status], response[:body]],
                   [url, options].compact
  end

  def adapter
    @adapter ||= MiniTest::Mock.new
  end
end