webfinger 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -1,11 +1,11 @@
1
1
  module WebFinger
2
2
  class Request
3
- attr_accessor :resource, :relations, :path
3
+ attr_accessor :resource, :relations
4
4
 
5
5
  def initialize(resource, options = {})
6
6
  self.resource = resource
7
7
  self.relations = Array(options[:rel] || options[:relations])
8
- self.path ||= '/.well-known/webfinger'
8
+ @options = options
9
9
  end
10
10
 
11
11
  def discover!(cache_options = nil)
@@ -30,10 +30,11 @@ module WebFinger
30
30
  end
31
31
 
32
32
  def endpoint
33
- host = URI.parse(resource).host || resource.split(':').last.split('@').last
34
- WebFinger.url_builder.build [nil, host, nil, path, query_string, nil]
35
- rescue URI::Error => e
36
- raise Exception.new(e.message)
33
+ uri = URI.parse(resource)
34
+ path = '/.well-known/webfinger'
35
+ host = @options[:host] || uri.host || resource.split(':').last.split('@').last.split('/').first
36
+ port = @options[:port] || ([80, 443].include?(uri.port) ? nil : uri.port)
37
+ WebFinger.url_builder.build [nil, host, port, path, query_string, nil]
37
38
  end
38
39
 
39
40
  def query_string
@@ -41,31 +42,31 @@ module WebFinger
41
42
  end
42
43
 
43
44
  def query_params
44
- _query_params_ = [{resource: resource}.to_query]
45
+ query_params = [{resource: resource}]
45
46
  relations.each do |relation|
46
- _query_params_ << {rel: relation}.to_query
47
+ query_params << {rel: relation}
47
48
  end
48
- _query_params_
49
+ query_params.collect(&:to_query)
49
50
  end
50
51
 
51
52
  def handle_response
52
- raw_jrd = yield
53
- jrd = MultiJson.load raw_jrd, symbolize_keys: true
53
+ raw_response = yield
54
+ jrd = MultiJson.load raw_response, symbolize_keys: true
54
55
  Response.new jrd
55
56
  rescue HTTPClient::BadResponseError => e
56
57
  case e.res.try(:status)
57
58
  when nil
58
- raise Exception.new(e.message)
59
+ raise e
59
60
  when 400
60
- raise BadRequest.new('Bad Request', res)
61
+ raise BadRequest.new('Bad Request', raw_response)
61
62
  when 401
62
- raise Unauthorized.new('Unauthorized', res)
63
+ raise Unauthorized.new('Unauthorized', raw_response)
63
64
  when 403
64
- raise Forbidden.new('Forbidden', res)
65
+ raise Forbidden.new('Forbidden', raw_response)
65
66
  when 404
66
- raise NotFound.new('Not Found', res)
67
+ raise NotFound.new('Not Found', raw_response)
67
68
  else
68
- raise HttpError.new(e.res.status, e.res.reason, res)
69
+ raise HttpError.new(e.res.status, e.res.reason, raw_response)
69
70
  end
70
71
  end
71
72
  end
@@ -26,7 +26,7 @@ module WebFinger
26
26
  end
27
27
  def expires_in
28
28
  if expires.present?
29
- (Time.now - expires).to_i
29
+ (expires - Time.now).to_i
30
30
  end
31
31
  end
32
32
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe WebFinger::Response do
4
+ let(:expires) { 10.minutes.from_now }
5
+ let(:_subject_) { 'acct:nov@matake.jp' }
6
+ let(:aliases) { ['mailto:nov@matake.jp'] }
7
+ let(:properties) do
8
+ {'http://webfinger.net/rel/name' => 'Nov Matake'}
9
+ end
10
+ let(:links) do
11
+ [{
12
+ rel: 'http://openid.net/specs/connect/1.0/issuer',
13
+ href: 'https://openid.example.com/'
14
+ }]
15
+ end
16
+ subject do
17
+ WebFinger::Response.new(
18
+ expires: expires,
19
+ subject: _subject_,
20
+ aliases: aliases,
21
+ properties: properties,
22
+ links: links
23
+ )
24
+ end
25
+
26
+ its(:expired?) { should be_false }
27
+ its(:expires) { should == expires }
28
+ its(:expires_in) { should == (expires - Time.now).to_i }
29
+ its(:subject) { should == subject }
30
+ its(:aliases) { should == aliases }
31
+ its(:properties) { should == properties }
32
+ its(:links) { should == links }
33
+ end
@@ -1,12 +1,100 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe WebFinger do
4
- let(:resource) { "acct:nov@example.com" }
5
- describe '#discover' do
6
- it 'should return WebFinger::Response' do
7
- mock_json "https://example.com/.well-known/webfinger", 'all', query: {resource: resource} do
8
- response = WebFinger.discover! resource
9
- response.should be_instance_of WebFinger::Response
4
+ let(:resource) { 'acct:nov@example.com' }
5
+
6
+ describe '#discover!' do
7
+ shared_examples_for :discovery_succeeded do
8
+ it 'should return WebFinger::Response' do
9
+ mock_json 'https://example.com/.well-known/webfinger', 'all', query: {resource: resource} do
10
+ response = WebFinger.discover! resource
11
+ response.should be_instance_of WebFinger::Response
12
+ end
13
+ end
14
+ end
15
+
16
+ [:acct, :mailto, :device, :unknown].each do |scheme|
17
+ context "with #{scheme} scheme" do
18
+ let(:resource) { "#{scheme}:nov@example.com" }
19
+ it_behaves_like :discovery_succeeded
20
+ end
21
+ end
22
+
23
+ context 'with http scheme' do
24
+ let(:resource) { 'http://example.com/nov' }
25
+ it_behaves_like :discovery_succeeded
26
+ end
27
+
28
+ context 'with https scheme' do
29
+ let(:resource) { 'https://example.com/nov' }
30
+ it_behaves_like :discovery_succeeded
31
+ end
32
+
33
+ context 'with host option' do
34
+ it 'should use given host' do
35
+ mock_json 'https://discover.example.com/.well-known/webfinger', 'all', query: {resource: resource} do
36
+ response = WebFinger.discover! resource, host: 'discover.example.com'
37
+ response.should be_instance_of WebFinger::Response
38
+ end
39
+ end
40
+ end
41
+
42
+ context 'with port option' do
43
+ it 'should use given port' do
44
+ mock_json 'https://example.com:8080/.well-known/webfinger', 'all', query: {resource: resource} do
45
+ response = WebFinger.discover! resource, port: 8080
46
+ response.should be_instance_of WebFinger::Response
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'with rel option' do
52
+ shared_examples_for :discovery_with_rel do
53
+ let(:query_string) do
54
+ query_params = [{resource: resource}.to_query]
55
+ Array(rel).each do |_rel_|
56
+ query_params << {rel: _rel_}.to_query
57
+ end
58
+ query_params.join('&')
59
+ end
60
+
61
+ it 'should request with rel' do
62
+ query_string.scan('rel').count.should == Array(rel).count
63
+ mock_json 'https://example.com/.well-known/webfinger', 'all', query: query_string do
64
+ response = WebFinger.discover! resource, rel: rel
65
+ response.should be_instance_of WebFinger::Response
66
+ end
67
+ end
68
+ end
69
+
70
+ context 'when single rel' do
71
+ let(:rel) { 'http://openid.net/specs/connect/1.0/issuer' }
72
+ it_behaves_like :discovery_with_rel
73
+ end
74
+
75
+ context 'when multiple rel' do
76
+ let(:rel) { ['http://openid.net/specs/connect/1.0/issuer', 'vcard'] }
77
+ it_behaves_like :discovery_with_rel
78
+ end
79
+ end
80
+
81
+ context 'when error' do
82
+ {
83
+ 400 => WebFinger::BadRequest,
84
+ 401 => WebFinger::Unauthorized,
85
+ 403 => WebFinger::Forbidden,
86
+ 404 => WebFinger::NotFound,
87
+ 500 => WebFinger::HttpError
88
+ }.each do |status, exception_class|
89
+ context "when status=#{status}" do
90
+ it "should raise #{exception_class}" do
91
+ expect do
92
+ mock_json 'https://example.com/.well-known/webfinger', 'all', query: {resource: resource}, status: [status, 'HTTPError'] do
93
+ response = WebFinger.discover! resource
94
+ end
95
+ end.to raise_error exception_class
96
+ end
97
+ end
10
98
  end
11
99
  end
12
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webfinger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -134,7 +134,7 @@ files:
134
134
  - spec/mock_json/open_id.json
135
135
  - spec/spec_helper.rb
136
136
  - spec/webfinger/debugger/request_filter_spec.rb
137
- - spec/webfinger/request_spec.rb
137
+ - spec/webfinger/response_spec.rb
138
138
  - spec/webfinger_spec.rb
139
139
  - webfinger.gemspec
140
140
  homepage: https://github.com/nov/webfinger
@@ -169,5 +169,5 @@ test_files:
169
169
  - spec/mock_json/open_id.json
170
170
  - spec/spec_helper.rb
171
171
  - spec/webfinger/debugger/request_filter_spec.rb
172
- - spec/webfinger/request_spec.rb
172
+ - spec/webfinger/response_spec.rb
173
173
  - spec/webfinger_spec.rb
@@ -1,5 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe WebFinger::Request do
4
-
5
- end