summaryrefslogtreecommitdiff
path: root/test/unit/request_handler_test.rb
blob: 6c7a036e838cd528a46d31619d1375a3079cba27 (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
68
69
70
71
72
73
74
75
76
require 'test_helper'
require 'nickserver/request_handler'

class Nickserver::RequestHandlerTest < Minitest::Test

  def test_empty_query
    handle
    assert_response status: 404, content: "404 Not Found\n"
  end

  def test_invalid_query
    handle address: ['asdf']
    assert_response status: 500, content: "500 Not a valid address\n"
  end

  def test_missing_domain
    handle address: ['valid@email.tld']
    assert_response status: 500, content: "500 HTTP request must include a Host header.\n"
  end

  def test_email_from_hkp
    handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" }
    source = Minitest::Mock.new
    source.expect :query, Nickserver::Response.new(200, "fake content"), [Nickserver::EmailAddress]
    Nickserver::Hkp::Source.stub :new, source do
      assert_response status: 200, content: "200 fake content"
    end
  end

  def test_fingerprint_to_short
    handle fingerprint: ['44F2F455E28']
    assert_response status: 500, content: "500 Fingerprint invalid: 44F2F455E28\n"
  end

  def test_fingerprint_is_not_hex
    handle fingerprint: ['X36E738D69173C13Z709E44F2F455E2824D18DDX']
    assert_response status: 500,
      content: "500 Fingerprint invalid: X36E738D69173C13Z709E44F2F455E2824D18DDX\n"
  end

  def test_get_key_with_fingerprint_from_hkp
    handle fingerprint: ['E36E738D69173C13D709E44F2F455E2824D18DDF']
    source = Minitest::Mock.new
    source.expect :get_key_by_fingerprint,
      Nickserver::Response.new(200, "fake fingerprint"),
      ['E36E738D69173C13D709E44F2F455E2824D18DDF']
    Nickserver::Hkp::Source.stub :new, source do
      assert_response status: 200, content: "200 fake fingerprint"
    end
  end

  protected

  def handle(params = {})
    @headers = params.delete(:headers) || {}
    @params = Hash[ params.map{ |k,v| [k.to_s, v] } ]
  end

  def assert_response(args)
    responder.expect :respond, nil, [args[:status], args[:content]]
    handler.respond_to @params, @headers
    responder.verify
  end

  def handler
    Nickserver::RequestHandler.new responder, adapter
  end

  def responder
    @responder ||= Minitest::Mock.new
  end

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