File: response_spec.rb

package info (click to toggle)
ruby-oembed 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,468 kB
  • sloc: ruby: 2,351; makefile: 3
file content (230 lines) | stat: -rw-r--r-- 7,408 bytes parent folder | download | duplicates (3)
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
require File.dirname(__FILE__) + '/spec_helper'

def expected_helpers
  {
    "type" => "random",
    "version" => "1.0",
    "html" => "<em>Hello world!</em>",
    "url" => "http://foo.com/bar",
  }.freeze
end

def expected_skipped
  {
    "fields" => "hello",
    "__id__" => 1234,
    "provider" => "oohEmbed",
    "to_s" => "random string",
  }.freeze
end

def all_expected
  expected_helpers.merge(expected_skipped).freeze
end

describe OEmbed::Response do
  include OEmbedSpecHelper

  let(:flickr) {
    flickr = OEmbed::Provider.new("http://www.flickr.com/services/oembed/")
    flickr << "http://*.flickr.com/*"
    flickr
  }

  let(:skitch) {
    OEmbed::Provider.new("https://skitch.com/oembed")
  }

  let(:qik) {
    qik = OEmbed::Provider.new("http://qik.com/api/oembed.{format}", :xml)
    qik << "http://qik.com/video/*"
    qik << "http://qik.com/*"
    qik
  }

  let(:viddler) {
    viddler = OEmbed::Provider.new("http://lab.viddler.com/services/oembed/", :json)
    viddler << "http://*.viddler.com/*"
    viddler
  }

  let(:new_res) {
    OEmbed::Response.new(valid_response(:object), OEmbed::Providers::OohEmbed)
  }

  let(:default_res) {
    OEmbed::Response.create_for(valid_response(:json), @flickr, example_url(:flickr), :json)
  }

  let(:xml_res) {
    OEmbed::Response.create_for(valid_response(:xml), @qik, example_url(:qik), :xml)
  }

  let(:json_res) {
    OEmbed::Response.create_for(valid_response(:json), @viddler, example_url(:viddler), :json)
  }

  describe "#initialize" do
    it "should parse the data into fields" do
      # We need to compare keys & values separately because we don't expect all
      # non-string values to be recognized correctly.

      expect(new_res.fields.keys).to eq(valid_response(:object).keys)
      expect(new_res.fields.values.map{|v|v.to_s}).to eq(valid_response(:object).values.map{|v|v.to_s})

      expect(default_res.fields.keys).to eq(valid_response(:object).keys)
      expect(default_res.fields.values.map{|v|v.to_s}).to eq(valid_response(:object).values.map{|v|v.to_s})

      expect(xml_res.fields.keys).to eq(valid_response(:object).keys)
      expect(xml_res.fields.values.map{|v|v.to_s}).to eq(valid_response(:object).values.map{|v|v.to_s})

      expect(json_res.fields.keys).to eq(valid_response(:object).keys)
      expect(json_res.fields.values.map{|v|v.to_s}).to eq(valid_response(:object).values.map{|v|v.to_s})
    end

    it "should set the provider" do
      expect(new_res.provider).to eq(OEmbed::Providers::OohEmbed)
      expect(default_res.provider).to eq(@flickr)
      expect(xml_res.provider).to eq(@qik)
      expect(json_res.provider).to eq(@viddler)
    end

    it "should set the format" do
      expect(new_res.format).to be_nil
      expect(default_res.format.to_s).to eq('json')
      expect(xml_res.format.to_s).to eq('xml')
      expect(json_res.format.to_s).to eq('json')
    end

    it "should set the request_url" do
      expect(new_res.request_url).to be_nil
      expect(default_res.request_url.to_s).to eq(example_url(:flickr))
      expect(xml_res.request_url.to_s).to eq(example_url(:qik))
      expect(json_res.request_url.to_s).to eq(example_url(:viddler))
    end
  end

  describe "create_for" do
    it "should only allow JSON or XML" do
      expect {
        OEmbed::Response.create_for(valid_response(:json), flickr, example_url(:flickr), :json)
      }.not_to raise_error

      expect {
        OEmbed::Response.create_for(valid_response(:xml), flickr, example_url(:flickr), :xml)
      }.not_to raise_error

      expect {
        OEmbed::Response.create_for(valid_response(:yml), flickr, example_url(:flickr), :yml)
      }.to raise_error(OEmbed::FormatNotSupported)
    end

    it "should not parse the incorrect format" do
      expect {
        OEmbed::Response.create_for(valid_response(:object), example_url(:flickr), flickr, :json)
      }.to raise_error(OEmbed::ParseError)

      expect {
        OEmbed::Response.create_for(valid_response(:xml), example_url(:flickr), viddler, :json)
      }.to raise_error(OEmbed::ParseError)

      expect {
        OEmbed::Response.create_for(valid_response(:json), example_url(:flickr), viddler, :xml)
      }.to raise_error(OEmbed::ParseError)
    end
  end

  it "should access the XML data through #field" do
    expect(xml_res.field(:type)).to eq("photo")
    expect(xml_res.field(:version)).to eq("1.0")
    expect(xml_res.field(:fields)).to eq("hello")
    expect(xml_res.field(:__id__)).to eq("1234")
  end

  it "should access the JSON data through #field" do
    expect(json_res.field(:type)).to eq("photo")
    expect(json_res.field(:version)).to eq("1.0")
    expect(json_res.field(:fields)).to eq("hello")
    expect(json_res.field(:__id__)).to eq("1234")
  end

  describe "#define_methods!" do
    context "with automagic" do
      all_expected.each do |method, value|
        before do
          @local_res = OEmbed::Response.new(all_expected, OEmbed::Providers::OohEmbed)
        end

        it "should define the #{method} method" do
          expect(@local_res).to respond_to(method)
        end
      end

      expected_helpers.each do |method, value|
        it "should define #{method} to return #{value.inspect}" do
          expect(@local_res.send(method)).to eq(value)
        end
      end

      expected_skipped.each do |method, value|
        it "should NOT override #{method} to not return #{value.inspect}" do
          expect(@local_res.send(method)).to_not eq(value)
        end
      end
    end

    it "should protect most already defined methods" do
      expect(Object.new).to respond_to('__id__')
      expect(Object.new).to respond_to('to_s')

      expect(all_expected.keys).to include('__id__')
      expect(all_expected.keys).to include('to_s')

      local_res = OEmbed::Response.new(all_expected, OEmbed::Providers::OohEmbed)

      expect(local_res.__id__).to_not eq(local_res.field('__id__'))
      expect(local_res.to_s).to_not eq(local_res.field('to_s'))
    end

    it "should not protect already defined methods that are specifically overridable" do
      class Object
        def version
          "two point oh"
        end
      end

      expect(Object.new).to respond_to('version')
      expect(String.new).to respond_to('version')

      expect(all_expected.keys).to include('version')
      expect(all_expected['version']).to_not eq(String.new.version)

      local_res = OEmbed::Response.new(all_expected, OEmbed::Providers::OohEmbed)

      expect(local_res.version).to eq(local_res.field('version'))
      expect(local_res.version).to_not eq(String.new.version)
    end
  end

  describe "OEmbed::Response::Photo" do
    describe "#html" do
      it "should include the title, if given" do
        response = OEmbed::Response.create_for(example_body(:flickr), example_url(:flickr), flickr, :json)
        expect(response).to respond_to(:title)
        expect(response.title).to_not be_empty

        expect(response.html).to_not be_nil
        expect(response.html).to match(/alt='#{response.title}'/)
      end

      it "should work just fine, without a title" do
        response = OEmbed::Response.create_for(example_body(:skitch), example_url(:skitch), skitch, :json)
        expect(response).to_not respond_to(:title)

        expect(response.html).to_not be_nil
        expect(response.html).to match(/alt=''/)
      end
    end
  end

end