22require File . join ( File . dirname ( __FILE__ ) , 'spec_helper' )
33require 'webmock/rspec'
44require 'json'
5+ require 'rdf/turtle'
56
67describe SPARQL ::Client do
78 let ( :query ) { 'DESCRIBE ?kb WHERE { ?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . }' }
89 let ( :construct_query ) { 'CONSTRUCT {?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . } WHERE { ?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . }' }
910 let ( :select_query ) { 'SELECT ?kb WHERE { ?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . }' }
1011 let ( :describe_query ) { 'DESCRIBE ?kb WHERE { ?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . }' }
1112 let ( :ask_query ) { 'ASK WHERE { ?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . }' }
13+ let ( :update_query ) { 'DELETE {?s ?p ?o} WHERE {}' }
1214
1315 describe "#initialize" do
1416 it "calls block" do
2426
2527 def response ( header )
2628 response = Net ::HTTPSuccess . new '1.1' , 200 , 'body'
27- response . content_type = header
29+ response . content_type = header if header
2830 allow ( response ) . to receive ( :body ) . and_return ( 'body' )
2931 response
3032 end
@@ -55,7 +57,7 @@ def response(header)
5557
5658 it "should handle successful response with plain header" do
5759 expect ( subject ) . to receive ( :request ) . and_yield response ( 'text/plain' )
58- expect ( RDF ::Reader ) . to receive ( :for ) . with ( :content_type => 'text/plain' )
60+ expect ( RDF ::Reader ) . to receive ( :for ) . with ( :content_type => 'text/plain' ) . and_call_original
5961 subject . query ( query )
6062 end
6163
@@ -94,15 +96,14 @@ def response(header)
9496 subject . query ( query , :content_type => SPARQL ::Client ::RESULT_JSON )
9597 end
9698
97- it "should handle successful response with overridden JSON header" do
98- expect ( subject ) . to receive ( :request ) . and_yield response ( SPARQL ::Client ::RESULT_JSON )
99- expect ( subject . class ) . to receive ( :parse_xml_bindings )
100- subject . query ( query , :content_type => SPARQL ::Client ::RESULT_XML )
99+ it "should handle successful response with no content type" do
100+ expect ( subject ) . to receive ( :request ) . and_yield response ( nil )
101+ expect { subject . query ( query ) } . not_to raise_error
101102 end
102103
103104 it "should handle successful response with overridden plain header" do
104105 expect ( subject ) . to receive ( :request ) . and_yield response ( 'text/plain' )
105- expect ( RDF ::Reader ) . to receive ( :for ) . with ( :content_type => 'text/turtle' )
106+ expect ( RDF ::Reader ) . to receive ( :for ) . with ( :content_type => 'text/turtle' ) . and_call_original
106107 subject . query ( query , :content_type => 'text/turtle' )
107108 end
108109
@@ -122,7 +123,7 @@ def response(header)
122123
123124 it "should enable overriding the http method" do
124125 stub_request ( :get , "http://data.linkedmdb.org/sparql?query=DESCRIBE%20?kb%20WHERE%20%7B%20?kb%20%3Chttp://data.linkedmdb.org/resource/movie/actor_name%3E%20%22Kevin%20Bacon%22%20.%20%7D" ) .
125- to_return ( :status => 200 , :body => "" , :headers => { } )
126+ to_return ( :status => 200 , :body => "" , :headers => { 'Content-Type' => 'application/n-triples' } )
126127 allow ( subject ) . to receive ( :request_method ) . with ( query ) . and_return ( :get )
127128 expect ( subject ) . to receive ( :make_get_request ) . and_call_original
128129 subject . query ( query )
@@ -152,7 +153,7 @@ def response(header)
152153
153154 it 'follows redirects' do
154155 WebMock . stub_request ( :any , 'http://sparql.linkedmdb.org/sparql' ) .
155- to_return ( :body => '{}' , :status => 200 )
156+ to_return ( :body => '{}' , :status => 200 , :headers => { :content_type => SPARQL :: Client :: RESULT_JSON } )
156157 subject . query ( ask_query )
157158 expect ( WebMock ) . to have_requested ( :post , "http://sparql.linkedmdb.org/sparql" ) .
158159 with ( :body => 'query=ASK+WHERE+%7B+%3Fkb+%3Chttp%3A%2F%2Fdata.linkedmdb.org%2Fresource%2Fmovie%2Factor_name%3E+%22Kevin+Bacon%22+.+%7D' )
@@ -171,31 +172,58 @@ def response(header)
171172 to_return ( :body => '{}' , :status => 200 , :headers => { 'Content-Type' => 'application/sparql-results+json' } )
172173 subject . query ( ask_query )
173174 expect ( WebMock ) . to have_requested ( :post , "http://data.linkedmdb.org/sparql" ) .
174- with ( :headers => { 'Accept' => 'application/sparql-results+json, application/sparql-results+xml, text/boolean, text/tab-separated-values;p =0.8, text/csv;p =0.2, */*;p =0.1' } )
175+ with ( :headers => { 'Accept' => 'application/sparql-results+json, application/sparql-results+xml, text/boolean, text/tab-separated-values;q =0.8, text/csv;q =0.2, */*;q =0.1' } )
175176 end
176177
177178 it "should use application/n-triples for CONSTRUCT" do
178179 WebMock . stub_request ( :any , 'http://data.linkedmdb.org/sparql' ) .
179180 to_return ( :body => '' , :status => 200 , :headers => { 'Content-Type' => 'application/n-triples' } )
180181 subject . query ( construct_query )
181182 expect ( WebMock ) . to have_requested ( :post , "http://data.linkedmdb.org/sparql" ) .
182- with ( :headers => { 'Accept' => 'application/n-triples, text/plain, */*;p =0.1' } )
183+ with ( :headers => { 'Accept' => 'application/n-triples, text/plain, */*;q =0.1' } )
183184 end
184185
185186 it "should use application/n-triples for DESCRIBE" do
186187 WebMock . stub_request ( :any , 'http://data.linkedmdb.org/sparql' ) .
187188 to_return ( :body => '' , :status => 200 , :headers => { 'Content-Type' => 'application/n-triples' } )
188189 subject . query ( describe_query )
189190 expect ( WebMock ) . to have_requested ( :post , "http://data.linkedmdb.org/sparql" ) .
190- with ( :headers => { 'Accept' => 'application/n-triples, text/plain, */*;p =0.1' } )
191+ with ( :headers => { 'Accept' => 'application/n-triples, text/plain, */*;q =0.1' } )
191192 end
192193
193194 it "should use application/sparql-results+json for SELECT" do
194195 WebMock . stub_request ( :any , 'http://data.linkedmdb.org/sparql' ) .
195196 to_return ( :body => '{}' , :status => 200 , :headers => { 'Content-Type' => 'application/sparql-results+json' } )
196197 subject . query ( select_query )
197198 expect ( WebMock ) . to have_requested ( :post , "http://data.linkedmdb.org/sparql" ) .
198- with ( :headers => { 'Accept' => 'application/sparql-results+json, application/sparql-results+xml, text/boolean, text/tab-separated-values;p=0.8, text/csv;p=0.2, */*;p=0.1' } )
199+ with ( :headers => { 'Accept' => 'application/sparql-results+json, application/sparql-results+xml, text/boolean, text/tab-separated-values;q=0.8, text/csv;q=0.2, */*;q=0.1' } )
200+ end
201+ end
202+
203+ context "Alternative Endpoint" do
204+ it "should use the default endpoint if no alternative endpoint is provided" do
205+ WebMock . stub_request ( :any , 'http://data.linkedmdb.org/sparql' ) .
206+ to_return ( :body => '' , :status => 200 )
207+ subject . update ( update_query )
208+ expect ( WebMock ) . to have_requested ( :post , "http://data.linkedmdb.org/sparql" )
209+ end
210+
211+ it "should use the alternative endpoint if provided" do
212+ WebMock . stub_request ( :any , 'http://data.linkedmdb.org/alternative' ) .
213+ to_return ( :body => '' , :status => 200 )
214+ subject . update ( update_query , { endpoint : "http://data.linkedmdb.org/alternative" } )
215+ expect ( WebMock ) . to have_requested ( :post , "http://data.linkedmdb.org/alternative" )
216+ end
217+
218+ it "should not use the alternative endpoint for a select query" do
219+ WebMock . stub_request ( :any , 'http://data.linkedmdb.org/sparql' ) .
220+ to_return ( :body => '' , :status => 200 )
221+ WebMock . stub_request ( :any , 'http://data.linkedmdb.org/alternative' ) .
222+ to_return ( :body => '' , :status => 200 )
223+ subject . update ( update_query , { endpoint : "http://data.linkedmdb.org/alternative" } )
224+ expect ( WebMock ) . to have_requested ( :post , "http://data.linkedmdb.org/alternative" )
225+ subject . query ( select_query )
226+ expect ( WebMock ) . to have_requested ( :post , "http://data.linkedmdb.org/sparql" )
199227 end
200228 end
201229
0 commit comments