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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
require File.dirname(__FILE__) + '/spec_helper'
describe OEmbed::Provider do
before(:all) do
VCR.insert_cassette('OEmbed_Provider')
end
after(:all) do
VCR.eject_cassette
end
include OEmbedSpecHelper
before(:all) do
@default = OEmbed::Formatter.default
@flickr = OEmbed::Provider.new("http://www.flickr.com/services/oembed/")
@qik = OEmbed::Provider.new("http://qik.com/api/oembed.{format}", :xml)
@viddler = OEmbed::Provider.new("http://lab.viddler.com/services/oembed/", :json)
@flickr << "http://*.flickr.com/*"
@qik << "http://qik.com/video/*"
@qik << "http://qik.com/*"
@viddler << "http://*.viddler.com/*"
end
it "should require a valid endpoint for a new instance" do
expect { OEmbed::Provider.new("http://foo.com/oembed/") }.
not_to raise_error
expect { OEmbed::Provider.new("https://foo.com/oembed/") }.
not_to raise_error
end
it "should allow a {format} string in the endpoint for a new instance" do
expect { OEmbed::Provider.new("http://foo.com/oembed.{format}/get") }.
not_to raise_error
end
it "should raise an ArgumentError given an invalid endpoint for a new instance" do
[
"httpx://foo.com/oembed/",
"ftp://foo.com/oembed/",
"foo.com/oembed/",
"http://not a uri",
nil, 1,
].each do |endpoint|
expect { OEmbed::Provider.new(endpoint) }.
to raise_error(ArgumentError)
end
end
it "should allow no URI schema to be given" do
provier = OEmbed::Provider.new("http://foo.com/oembed")
expect(provier).to include("http://foo.com/1")
expect(provier).to include("http://bar.foo.com/1")
expect(provier).to include("http://bar.foo.com/show/1")
expect(provier).to include("https://bar.foo.com/1")
expect(provier).to include("http://asdf.com/1")
expect(provier).to include("asdf")
end
it "should allow a String as a URI schema" do
provier = OEmbed::Provider.new("http://foo.com/oembed")
provier << "http://bar.foo.com/*"
expect(provier).to include("http://bar.foo.com/1")
expect(provier).to include("http://bar.foo.com/show/1")
expect(provier).to_not include("https://bar.foo.com/1")
expect(provier).to_not include("http://foo.com/1")
end
it "should allow multiple path wildcards in a String URI schema" do
provier = OEmbed::Provider.new("http://foo.com/oembed")
provier << "http://bar.foo.com/*/show/*"
expect(provier).to include("http://bar.foo.com/photo/show/1")
expect(provier).to include("http://bar.foo.com/video/show/2")
expect(provier).to include("http://bar.foo.com/help/video/show/2")
expect(provier).to_not include("https://bar.foo.com/photo/show/1")
expect(provier).to_not include("http://foo.com/video/show/2")
expect(provier).to_not include("http://bar.foo.com/show/1")
expect(provier).to_not include("http://bar.foo.com/1")
end
it "should NOT allow multiple domain wildcards in a String URI schema", :pending => true do
provier = OEmbed::Provider.new("http://foo.com/oembed")
expect { provier << "http://*.com/*" }.
to raise_error(ArgumentError)
expect(provier).to_not include("http://foo.com/1")
end
it "should allow a sub-domain wildcard in String URI schema" do
provier = OEmbed::Provider.new("http://foo.com/oembed")
provier << "http://*.foo.com/*"
expect(provier).to include("http://bar.foo.com/1")
expect(provier).to include("http://foo.foo.com/2")
expect(provier).to include("http://foo.com/3")
expect(provier).to_not include("https://bar.foo.com/1")
expect(provier).to_not include("http://my.bar.foo.com/1")
provier << "http://my.*.foo.com/*"
end
it "should allow multiple sub-domain wildcards in a String URI schema" do
provier = OEmbed::Provider.new("http://foo.com/oembed")
provier << "http://*.my.*.foo.com/*"
expect(provier).to include("http://my.bar.foo.com/1")
expect(provier).to include("http://my.foo.com/2")
expect(provier).to include("http://bar.my.bar.foo.com/3")
expect(provier).to_not include("http://bar.foo.com/1")
expect(provier).to_not include("http://foo.bar.foo.com/1")
end
it "should NOT allow a scheme wildcard in a String URI schema", :pending => true do
provier = OEmbed::Provider.new("http://foo.com/oembed")
expect { provier << "*://foo.com/*" }.
to raise_error(ArgumentError)
expect(provier).to_not include("http://foo.com/1")
end
it "should allow a scheme other than http in a String URI schema" do
provier = OEmbed::Provider.new("http://foo.com/oembed")
provier << "https://foo.com/*"
expect(provier).to include("https://foo.com/1")
gopher_url = "gopher://foo.com/1"
expect(provier).to_not include(gopher_url)
provier << "gopher://foo.com/*"
expect(provier).to include(gopher_url)
end
it "should allow a Regexp as a URI schema" do
provier = OEmbed::Provider.new("http://foo.com/oembed")
provier << %r{^https?://([^\.]*\.)?foo.com/(show/)?\d+}
expect(provier).to include("http://bar.foo.com/1")
expect(provier).to include("http://bar.foo.com/show/1")
expect(provier).to include("http://foo.com/1")
expect(provier).to include("https://bar.foo.com/1")
expect(provier).to_not include("http://bar.foo.com/video/1")
expect(provier).to_not include("gopher://foo.com/1")
end
it "should by default use OEmbed::Formatter.default" do
expect(@flickr.format).to eq(@default)
end
it "should allow xml" do
expect(@qik.format).to eq(:xml)
end
it "should allow json" do
expect(@viddler.format).to eq(:json)
end
it "should allow random formats on initialization" do
expect {
yaml_provider = OEmbed::Provider.new("http://foo.com/api/oembed.{format}", :yml)
yaml_provider << "http://foo.com/*"
}.
not_to raise_error
end
it "should not allow random formats to be parsed" do
yaml_provider = OEmbed::Provider.new("http://foo.com/api/oembed.{format}", :yml)
yaml_provider << "http://foo.com/*"
yaml_url = "http://foo.com/video/1"
expect(yaml_provider).to receive(:raw).
with(yaml_url, {:format=>:yml}).
and_return(valid_response(:json))
expect { yaml_provider.get(yaml_url) }.
to raise_error(OEmbed::FormatNotSupported)
end
it "should add URL schemes" do
expect(@flickr.urls).to eq([%r{^http://([^\.]+\.)?flickr\.com/(.*?)}])
expect(@qik.urls).to eq([%r{^http://qik\.com/video/(.*?)},
%r{^http://qik\.com/(.*?)}])
end
it "should match URLs" do
expect(@flickr).to include(example_url(:flickr))
expect(@qik).to include(example_url(:qik))
end
it "should raise error if the URL is invalid" do
expect{ @flickr.send(:build, example_url(:fake)) }.to raise_error(OEmbed::NotFound)
expect{ @qik.send(:build, example_url(:fake)) }.to raise_error(OEmbed::NotFound)
end
describe "#build" do
it "should return a proper URL" do
uri = @flickr.send(:build, example_url(:flickr))
expect(uri.host).to eq("www.flickr.com")
expect(uri.path).to eq("/services/oembed/")
expect(uri.query).to include("format=#{@flickr.format}")
expect(uri.query).to include("url=#{CGI.escape 'http://flickr.com/photos/bees/2362225867/'}")
uri = @qik.send(:build, example_url(:qik))
expect(uri.host).to eq("qik.com")
expect(uri.path).to eq("/api/oembed.xml")
expect(uri.query).to_not include("format=#{@qik.format}")
expect(uri.query).to eq("url=#{CGI.escape 'http://qik.com/video/49565'}")
end
it "should accept parameters" do
uri = @flickr.send(:build, example_url(:flickr),
:maxwidth => 600,
:maxheight => 200,
:format => :xml,
:another => "test")
expect(uri.query).to include("maxwidth=600")
expect(uri.query).to include("maxheight=200")
expect(uri.query).to include("format=xml")
expect(uri.query).to include("another=test")
end
it "should build correctly when format is in the endpoint URL" do
uri = @qik.send(:build, example_url(:qik), :format => :json)
expect(uri.path).to eq("/api/oembed.json")
end
it "should build correctly with query parameters in the endpoint URL" do
provider = OEmbed::Provider.new('http://www.youtube.com/oembed?scheme=https')
provider << 'http://*.youtube.com/*'
url = 'http://youtube.com/watch?v=M3r2XDceM6A'
expect(provider).to include(url)
uri = provider.send(:build, url)
expect(uri.query).to include("scheme=https")
expect(uri.query).to include("url=#{CGI.escape url}")
end
it "should not include the :timeout parameter in the query string" do
uri = @flickr.send(:build, example_url(:flickr),
:timeout => 5,
:another => "test")
expect(uri.query).to_not include("timeout=5")
expect(uri.query).to include("another=test")
end
end
describe "#raw" do
it "should return the body on 200" do
res = @flickr.send(:raw, example_url(:flickr))
expect(res).to eq(example_body(:flickr))
end
it "should return the body on 200 even over https" do
@vimeo_ssl = OEmbed::Provider.new("https://vimeo.com/api/oembed.{format}")
@vimeo_ssl << "http://*.vimeo.com/*"
@vimeo_ssl << "https://*.vimeo.com/*"
expect {
res = @vimeo_ssl.send(:raw, example_url(:vimeo_ssl))
expect(res).to eq(example_body(:vimeo_ssl))
}.not_to raise_error
end
it "should raise an UnknownFormat error on 501" do
# Note: This test relies on a custom-written VCR response in the
# cassettes/OEmbed_Provider.yml file.
expect {
@flickr.send(:raw, File.join(example_url(:flickr), '501'))
}.to raise_error(OEmbed::UnknownFormat)
end
it "should raise a NotFound error on 404" do
# Note: This test relies on a custom-written VCR response in the
# cassettes/OEmbed_Provider.yml file.
expect {
@flickr.send(:raw, File.join(example_url(:flickr), '404'))
}.to raise_error(OEmbed::NotFound)
end
it "should raise an UnknownResponse error on other responses" do
# Note: This test relies on a custom-written VCR response in the
# cassettes/OEmbed_Provider.yml file.
statuses_to_check = ['405', '500']
statuses_to_check.each do |status|
expect {
@flickr.send(:raw, File.join(example_url(:flickr), status))
}.to raise_error(OEmbed::UnknownResponse)
end
end
end
describe "#get" do
it "should send the specified format" do
expect(@flickr).to receive(:raw).
with(example_url(:flickr), {:format=>:json}).
and_return(valid_response(:json))
@flickr.get(example_url(:flickr), :format=>:json)
expect(@flickr).to receive(:raw).
with(example_url(:flickr), {:format=>:xml}).
and_return(valid_response(:xml))
@flickr.get(example_url(:flickr), :format=>:xml)
expect {
expect(@flickr).to receive(:raw).
with(example_url(:flickr), {:format=>:yml}).
and_return(valid_response(:json))
@flickr.get(example_url(:flickr), :format=>:yml)
}.to raise_error(OEmbed::FormatNotSupported)
end
it "should return OEmbed::Response" do
allow(@flickr).to receive(:raw).and_return(valid_response(@default))
expect(@flickr.get(example_url(:flickr))).to be_a(OEmbed::Response)
end
it "should be calling OEmbed::Response#create_for internally" do
allow(@flickr).to receive(:raw).and_return(valid_response(@default))
expect(OEmbed::Response).to receive(:create_for).
with(valid_response(@default), @flickr, example_url(:flickr), @default.to_s)
@flickr.get(example_url(:flickr))
allow(@qik).to receive(:raw).and_return(valid_response(:xml))
expect(OEmbed::Response).to receive(:create_for).
with(valid_response(:xml), @qik, example_url(:qik), 'xml')
@qik.get(example_url(:qik))
allow(@viddler).to receive(:raw).and_return(valid_response(:json))
expect(OEmbed::Response).to receive(:create_for).
with(valid_response(:json), @viddler, example_url(:viddler), 'json')
@viddler.get(example_url(:viddler))
end
it "should send the provider's format if none is specified" do
expect(@flickr).to receive(:raw).
with(example_url(:flickr), :format => @default).
and_return(valid_response(@default))
@flickr.get(example_url(:flickr))
expect(@qik).to receive(:raw).
with(example_url(:qik), :format=>:xml).
and_return(valid_response(:xml))
@qik.get(example_url(:qik))
expect(@viddler).to receive(:raw).
with(example_url(:viddler), :format=>:json).
and_return(valid_response(:json))
@viddler.get(example_url(:viddler))
end
it "handles the :timeout option" do
expect_any_instance_of(Net::HTTP).to receive(:open_timeout=).with(5)
expect_any_instance_of(Net::HTTP).to receive(:read_timeout=).with(5)
@flickr.get(example_url(:flickr), :timeout => 5)
end
end
end
|