-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathutil_file_spec.rb
225 lines (195 loc) · 7.16 KB
/
util_file_spec.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
require_relative 'spec_helper'
require 'webmock/rspec'
require 'rdf/ntriples'
describe RDF::Util::File do
before(:each) {WebMock.disable_net_connect!}
after(:each) {WebMock.allow_net_connect!}
describe ".http_adapter" do
after do
RDF::Util::File.http_adapter = nil
end
it "returns Net::HTTP if rest-client is not available" do
hide_const("RestClient")
RDF::Util::File.remove_instance_variable(:@http_adapter) if RDF::Util::File.instance_variable_defined?(:@http_adapter)
RDF::Util::File.http_adapter
expect(RDF::Util::File.http_adapter).to eq RDF::Util::File::NetHttpAdapter
end
it "returns RestClient if rest-client is available" do
require 'rest-client'
expect(RDF::Util::File.http_adapter).to eq RDF::Util::File::RestClientAdapter
end
it "return Net::HTTP if explicitly requested" do
require 'rest-client'
expect(RDF::Util::File.http_adapter(true)).to eq RDF::Util::File::NetHttpAdapter
end
end
describe RDF::Util::File::HttpAdapter do
describe ".default_accept_header" do
subject { RDF::Util::File::HttpAdapter.default_accept_header.split(", ") }
before do
allow(RDF::Format).to receive(:accept_types).and_return(["text/html;q=0.5", "text/plain;q=0.2", "application/xhtml+xml;q=0.7", "text/csv;q=0.4", "text/tab-separated-values;q=0.4"])
end
it "should demote text/plain to q=0.2" do
expect(subject).to include "text/plain;q=0.2"
end
it "should demote text/csv to q=0.4" do
expect(subject).to include "text/plain;q=0.2"
end
it "should demote text/tab-separated-values to q=0.4" do
expect(subject).to include "text/plain;q=0.2"
end
it "should demote text/html to q=0.5" do
expect(subject).to include "text/html;q=0.5"
end
it "should demote application/xhtml+xml to q=0.7" do
expect(subject).to include "application/xhtml+xml;q=0.7"
end
end
describe ".default_user_agent" do
subject {RDF::Util::File::HttpAdapter.default_user_agent}
specify {is_expected.to eq "Ruby RDF.rb/#{RDF::VERSION}"}
end
end
describe RDF::Util::File::FaradayAdapter do
let(:http_adapter) { RDF::Util::File::FaradayAdapter }
require 'faraday'
require 'faraday_middleware'
describe ".conn=" do
after do
http_adapter.conn = nil
end
it "should set the Faraday connection" do
custom_connection = Faraday.new
http_adapter.conn = custom_connection
expect(http_adapter.conn).to eq custom_connection
end
end
describe ".conn" do
it "should return a Faraday connection that follows redirects" do
expect(http_adapter.conn.builder.handlers).to include FaradayMiddleware::FollowRedirects
end
end
end
describe ".open_file" do
let(:uri) {"https://ruby-rdf.github.io/rdf/etc/doap.nt"}
let(:opened) {double("opened")}
before(:each) do
expect(opened).to receive(:opened)
end
it "yields a local file" do
r = RDF::Util::File.open_file(fixture_path("test.nt")) do |f|
expect(f).to respond_to(:read)
opened.opened
RDF::Reader.new
end
expect(r).to be_a(RDF::Reader)
end
it "yields a file scheme" do
path = fixture_path("test.nt")
path = Gem.win_platform? ? "file:/#{path}" : "file:#{path}"
r = RDF::Util::File.open_file(path) do |f|
expect(f).to respond_to(:read)
opened.opened
RDF::Reader.new
end
expect(r).to be_a(RDF::Reader)
end
it "raises IOError on missing local file" do
expect {RDF::Util::File.open_file(fixture_path("not-here"))}.to raise_error IOError
opened.opened
end
it "yields an RemoteDocument and returns yieldreturn" do
WebMock.stub_request(:get, uri).
to_return(body: File.read(File.expand_path("../../etc/doap.nt", __FILE__)),
status: 200,
headers: { 'Content-Type' => RDF::NTriples::Format.content_type.first})
r = RDF::Util::File.open_file(uri) do |f|
expect(f).to respond_to(:read)
expect(f.content_type).to eq RDF::NTriples::Format.content_type.first
expect(f.code).to eq 200
opened.opened
RDF::Reader.new
end
expect(r).to be_a(RDF::Reader)
end unless ENV["CI"]
it "yields a file URL" do
r = RDF::Util::File.open_file("file:" + fixture_path("test.nt")) do |f|
expect(f).to respond_to(:read)
opened.opened
RDF::Reader.new
end
expect(r).to be_a(RDF::Reader)
end
it "returns a local file" do
f = RDF::Util::File.open_file(fixture_path("test.nt"))
expect(f).to respond_to(:read)
opened.opened
end
it "returns a local file with fragment identifier" do
f = RDF::Util::File.open_file(fixture_path("test.nt#fragment"))
expect(f).to respond_to(:read)
opened.opened
end
it "returns a local file with query" do
f = RDF::Util::File.open_file(fixture_path("test.nt?query"))
expect(f).to respond_to(:read)
opened.opened
end
it "returns a file URL" do
f = RDF::Util::File.open_file("file:" + fixture_path("test.nt"))
expect(f).to respond_to(:read)
opened.opened
end
it "raises IOError on missing file URL" do
expect {RDF::Util::File.open_file("file:" + fixture_path("not-here"))}.to raise_error IOError
opened.opened
end
end
describe RDF::Util::File::RemoteDocument do
subject {
described_class.new("body",
headers: {
content_type: %(text/turtle ; charset=UTF-8 ; foo="a B c"),
last_modified: "Thu, 24 Oct 2013 23:46:56 GMT",
etag: "abc123",
location: "http://location.example.org/",
content_encoding: "gzip, identity",
link: %(<http://example.com/foo>; rel="self"),
},
base_uri: "http://base.example.org/",
code: 200
)
}
its(:read) {is_expected.to eq "body"}
its(:base_uri) {is_expected.to eq "http://base.example.org/"}
its(:content_type) {is_expected.to eq "text/turtle"}
its(:charset) {is_expected.to eq "utf-8"}
its(:code) {is_expected.to eq 200}
its(:etag) {is_expected.to eq "abc123"}
its(:parameters) {is_expected.to eq({charset: "UTF-8", foo: "a B c"})}
its(:last_modified) {is_expected.to eq DateTime.parse("Thu, 24 Oct 2013 23:46:56 GMT")}
its(:content_encoding) {is_expected.to eq %w(gzip identity)}
its(:links) {expect(subject.links.to_a).to eq [["http://example.com/foo", [%w(rel self)]]]}
end
context "HTTP Adapters" do
require 'rdf/spec/http_adapter'
context "using Net::HTTP" do
it_behaves_like 'an RDF::HttpAdapter' do
let(:http_adapter) { RDF::Util::File::NetHttpAdapter }
end
end
context "using RestClient" do
require 'rest_client'
it_behaves_like 'an RDF::HttpAdapter' do
let(:http_adapter) { RDF::Util::File::RestClientAdapter }
end
end
context "using Faraday" do
require 'faraday'
require 'faraday_middleware'
it_behaves_like 'an RDF::HttpAdapter' do
let(:http_adapter) { RDF::Util::File::FaradayAdapter }
end
end
end unless ENV["CI"]
end