2
2
require File . join ( File . dirname ( __FILE__ ) , 'spec_helper' )
3
3
require 'webmock/rspec'
4
4
require 'json'
5
+ require 'rdf/turtle'
5
6
6
7
describe SPARQL ::Client do
7
8
let ( :query ) { 'DESCRIBE ?kb WHERE { ?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . }' }
8
9
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" . }' }
9
10
let ( :select_query ) { 'SELECT ?kb WHERE { ?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . }' }
10
11
let ( :describe_query ) { 'DESCRIBE ?kb WHERE { ?kb <http://data.linkedmdb.org/resource/movie/actor_name> "Kevin Bacon" . }' }
11
12
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 {}' }
12
14
13
15
describe "#initialize" do
14
16
it "calls block" do
24
26
25
27
def response ( header )
26
28
response = Net ::HTTPSuccess . new '1.1' , 200 , 'body'
27
- response . content_type = header
29
+ response . content_type = header if header
28
30
allow ( response ) . to receive ( :body ) . and_return ( 'body' )
29
31
response
30
32
end
@@ -55,7 +57,7 @@ def response(header)
55
57
56
58
it "should handle successful response with plain header" do
57
59
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
59
61
subject . query ( query )
60
62
end
61
63
@@ -94,15 +96,14 @@ def response(header)
94
96
subject . query ( query , :content_type => SPARQL ::Client ::RESULT_JSON )
95
97
end
96
98
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
101
102
end
102
103
103
104
it "should handle successful response with overridden plain header" do
104
105
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
106
107
subject . query ( query , :content_type => 'text/turtle' )
107
108
end
108
109
@@ -122,7 +123,7 @@ def response(header)
122
123
123
124
it "should enable overriding the http method" do
124
125
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' } )
126
127
allow ( subject ) . to receive ( :request_method ) . with ( query ) . and_return ( :get )
127
128
expect ( subject ) . to receive ( :make_get_request ) . and_call_original
128
129
subject . query ( query )
@@ -152,7 +153,7 @@ def response(header)
152
153
153
154
it 'follows redirects' do
154
155
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 } )
156
157
subject . query ( ask_query )
157
158
expect ( WebMock ) . to have_requested ( :post , "http://sparql.linkedmdb.org/sparql" ) .
158
159
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)
171
172
to_return ( :body => '{}' , :status => 200 , :headers => { 'Content-Type' => 'application/sparql-results+json' } )
172
173
subject . query ( ask_query )
173
174
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' } )
175
176
end
176
177
177
178
it "should use application/n-triples for CONSTRUCT" do
178
179
WebMock . stub_request ( :any , 'http://data.linkedmdb.org/sparql' ) .
179
180
to_return ( :body => '' , :status => 200 , :headers => { 'Content-Type' => 'application/n-triples' } )
180
181
subject . query ( construct_query )
181
182
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' } )
183
184
end
184
185
185
186
it "should use application/n-triples for DESCRIBE" do
186
187
WebMock . stub_request ( :any , 'http://data.linkedmdb.org/sparql' ) .
187
188
to_return ( :body => '' , :status => 200 , :headers => { 'Content-Type' => 'application/n-triples' } )
188
189
subject . query ( describe_query )
189
190
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' } )
191
192
end
192
193
193
194
it "should use application/sparql-results+json for SELECT" do
194
195
WebMock . stub_request ( :any , 'http://data.linkedmdb.org/sparql' ) .
195
196
to_return ( :body => '{}' , :status => 200 , :headers => { 'Content-Type' => 'application/sparql-results+json' } )
196
197
subject . query ( select_query )
197
198
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" )
199
227
end
200
228
end
201
229
0 commit comments