spec/lib/ldp/response_spec.rb in ldp-0.4.1 vs spec/lib/ldp/response_spec.rb in ldp-0.5.0
- old
+ new
@@ -1,35 +1,27 @@
require 'spec_helper'
describe Ldp::Response do
- LDP_RDF_RESOURCE_HEADERS = { "Link" => "<#{Ldp.resource.to_s}>;rel=\"type\", <#{Ldp.direct_container.to_s}>;rel=\"type\""}
- LDP_NON_RDF_SOURCE_HEADERS = { "Link" => "<#{Ldp.resource.to_s}>;rel=\"type\", <#{Ldp.non_rdf_source.to_s}>;rel=\"type\""}
+ LDP_RDF_RESOURCE_HEADERS = { "Link" => "<#{RDF::Vocab::LDP.Resource}>;rel=\"type\", <#{RDF::Vocab::LDP.DirectContainer}>;rel=\"type\""}
+ LDP_NON_RDF_SOURCE_HEADERS = { "Link" => "<#{RDF::Vocab::LDP.Resource}>;rel=\"type\", <#{RDF::Vocab::LDP.NonRDFSource}>;rel=\"type\""}
let(:mock_response) { double("mock response", headers: {}, env: { url: "info:a" }) }
let(:mock_client) { double(Ldp::Client) }
subject do
- Ldp::Response.wrap mock_client, mock_response
+ Ldp::Response.new mock_response
end
- describe ".wrap" do
- it "should mixin Ldp::Response into the raw response" do
- Ldp::Response.wrap(mock_client, mock_response)
- expect(mock_response).to be_a_kind_of(Ldp::Response)
- end
- end
-
describe "#dup" do
let(:mock_conn) { Faraday.new { |builder| builder.adapter :test, conn_stubs } }
let(:client) { Ldp::Client.new mock_conn }
- let(:raw_response) { client.get "a_container" }
let(:conn_stubs) do
Faraday::Adapter::Test::Stubs.new do |stub|
stub.get('/a_container') { [200, {"Link" => link}, body] }
end
end
- let(:response) { Ldp::Response.wrap mock_client, raw_response }
+ let(:response) { client.get "a_container" }
subject { response.dup }
context "for a container resource" do
let(:body) { "<> a <http://www.w3.org/ns/ldp#Container> ." }
@@ -59,44 +51,44 @@
end
end
end
- describe ".links" do
+ describe "#links" do
it "should extract link headers with relations as a hash" do
allow(mock_response).to receive(:headers).and_return(
"Link" => [
"<xyz>;rel=\"some-rel\"",
"<abc>;rel=\"some-multi-rel\"",
"<123>;rel=\"some-multi-rel\"",
"<vanilla-link>"
]
)
- h = Ldp::Response.links mock_response
+ h = subject.links
expect(h['some-rel']).to include("xyz")
expect(h['some-multi-rel']).to include("abc", "123")
expect(h['doesnt-exist']).to be_nil
end
it "should return an empty hash if no link headers are availabe" do
allow(mock_response).to receive(:headers).and_return({})
- h = Ldp::Response.links mock_response
+ h = subject.links
expect(h).to be_empty
end
end
- describe ".resource?" do
+ describe "#resource?" do
it "should be a resource if a Link[rel=type] header asserts it is an ldp:resource" do
allow(mock_response).to receive(:headers).and_return(
"Link" => [
- "<#{Ldp.resource}>;rel=\"type\""
+ "<#{RDF::Vocab::LDP.Resource}>;rel=\"type\""
]
)
- expect(Ldp::Response.resource? mock_response).to be true
+ expect(subject.resource?).to be true
end
end
describe "#graph" do
context "for an RDFSource (or Container)" do
@@ -107,18 +99,10 @@
expect(graph).to have_subject(RDF::URI.new("info:a"))
expect(graph).to have_statement RDF::Statement.new(RDF::URI.new("info:a"), RDF::URI.new("info:b"), RDF::URI.new("info:c"))
end
end
-
- context "for a NonRDFSource" do
- it "should parse the response body for an RDF graph" do
- allow(mock_response).to receive(:body).and_return("<> <info:b> <info:c> .")
- allow(mock_response).to receive(:headers).and_return(LDP_NON_RDF_SOURCE_HEADERS)
- expect { subject.graph }.to raise_error Ldp::UnexpectedContentType
- end
- end
end
describe "#etag" do
it "should pass through the response's ETag" do
allow(mock_response).to receive(:headers).and_return('ETag' => 'xyz')
@@ -140,11 +124,11 @@
allow(mock_response).to receive(:headers).and_return(LDP_RDF_RESOURCE_HEADERS)
end
it "should see if the response has an ldp:Page statement" do
graph = RDF::Graph.new
- graph << [RDF::URI.new('info:a'), RDF.type, Ldp.page]
+ graph << [RDF::URI.new('info:a'), RDF.type, RDF::Vocab::LDP.Page]
allow(mock_response).to receive(:body).and_return(graph.dump(:ttl))
expect(subject).to have_page
end
it "should be false otherwise" do
@@ -167,12 +151,12 @@
describe "#page" do
it "should get the ldp:Page data from the query" do
graph = RDF::Graph.new
- graph << [RDF::URI.new('info:a'), RDF.type, Ldp.page]
- graph << [RDF::URI.new('info:b'), RDF.type, Ldp.page]
+ graph << [RDF::URI.new('info:a'), RDF.type, RDF::Vocab::LDP.Page]
+ graph << [RDF::URI.new('info:b'), RDF.type, RDF::Vocab::LDP.Page]
allow(mock_response).to receive(:body).and_return(graph.dump(:ttl))
allow(mock_response).to receive(:headers).and_return(LDP_RDF_RESOURCE_HEADERS)
expect(subject.page.count).to eq(1)
@@ -183,8 +167,44 @@
it "should extract the HTTP request URI as an RDF URI" do
allow(mock_response).to receive(:body).and_return('')
allow(mock_response).to receive(:headers).and_return(LDP_RDF_RESOURCE_HEADERS)
allow(mock_response).to receive(:env).and_return(:url => 'http://xyz/a')
expect(subject.subject).to eq(RDF::URI.new("http://xyz/a"))
+ end
+ end
+
+ describe '#content_type' do
+ before do
+ allow(mock_response).to receive(:headers).and_return(
+ 'Content-Type' => 'application/octet-stream'
+ )
+ end
+
+ it 'provides the content type from the response' do
+ expect(subject.content_type).to eq 'application/octet-stream'
+ end
+ end
+
+ describe '#content_length' do
+ before do
+ allow(mock_response).to receive(:headers).and_return(
+ 'Content-Length' => '123'
+ )
+ end
+
+ it 'provides the content length from the response' do
+ expect(subject.content_length).to eq 123
+ end
+ end
+
+ describe '#content_disposition_filename' do
+ before do
+ allow(mock_response).to receive(:headers).and_return(
+ 'Content-Disposition' => 'filename="xyz.txt";'
+ )
+ end
+
+ it 'provides the filename from the content disposition header' do
+ expect(subject.content_disposition_filename).to eq 'xyz.txt'
end
end
end