# Trouble on adding a simple unit test for Youtube oneboxing

**URL:** https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546
**Category:** Development
**Tags:** onebox, testing
**Created:** [December 2, 2022, 8:49pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546 "2022-12-02T20:49:55Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [December 2, 2022, 8:49pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/1 "2022-12-02T20:49:55Z")

</div>

Hi!

I want to add support of /shorts/ Youtube link.

My modification of the `YoutubeOnebox` class works, but it is required that I add a test in [`youtube_onebox.rb`](https://github.com/discourse/discourse/blob/493d437e79f88ab9829a36fa000cc6107085e424/spec/lib/onebox/engine/youtube_onebox_spec.rb) and my test doesn’t work.

Similar to the [“can parse youtube embed results” test](https://github.com/discourse/discourse/blob/493d437e79f88ab9829a36fa000cc6107085e424/spec/lib/onebox/engine/youtube_onebox_spec.rb#L101), I added this code:

```ruby
it "can parse youtube shorts results" do
  preview = expect(Onebox.preview('https://www.youtube.com/watch?v=wi2jAtpBl0Y').placeholder_html)
  preview.to match(/From which sound/)
  preview.to match(/hqdefault/)
end

```

I also added this at the beginning of the file, in `before do`:

```ruby
stub_request(:get, "https://www.youtube.com/shorts/wi2jAtpBl0Y").to_return(status: 200, body: onebox_response("youtube-shorts"))

```

I have never done unit testing before, so I don’t really understand of all of this work.

My test failure:

```bash
Failures:

  1) Onebox::Engine::YoutubeOnebox can parse youtube shorts results
     Failure/Error:
               http.request(request) do |response|

                 if cookie = response.get_fields('set-cookie')
                   # HACK: If this breaks again in the future, use HTTP::CookieJar from gem 'http-cookie'
                   # See test: it "does not send cookies to the wrong domain"
                   redir_header = { 'Cookie' => cookie.join('; ') }
                 end

                 redir_header = nil unless redir_header.is_a? Hash

     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET https://www.youtube.com/embed/wi2jAtpBl0Y with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Discourse Forum Onebox v2.9.0.beta12'}

       You can stub this request with the following snippet:

       stub_request(:get, "https://www.youtube.com/embed/wi2jAtpBl0Y").
         with(
           headers: {
          'Accept'=>'*/*',
          'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
          'User-Agent'=>'Discourse Forum Onebox v2.9.0.beta12'
           }).
         to_return(status: 200, body: "", headers: {})

       registered request stubs:

       stub_request(:get, "https://www.youtube.com/shorts/wi2jAtpBl0Y")
       stub_request(:get, "https://www.youtube.com/embed/KCyIfcevExE")
       stub_request(:get, "https://www.youtube.com/playlist?list=PL5308B2E5749D1696")
       stub_request(:get, "http://www.youtube.com/user/googlechrome")
       stub_request(:get, "https://www.youtube.com/channel/UCL8ZULXASCc1I_oaOT0NaOQ")
       stub_request(:get, "https://www.youtube.com/watch?v=21Lk4YiASMo")
       stub_request(:get, "http://www.youtube.com/watch?v=21Lk4YiASMo")
       stub_request(:get, "https://www.youtube.com/embed/21Lk4YiASMo")
       stub_request(:get, "https://youtu.be/21Lk4YiASMo")
       stub_request(:get, "https://www.youtube.com/watch?feature=player_embedded&v=21Lk4YiASMo")

       ============================================================
     # ./lib/onebox/helpers.rb:83:in `block in fetch_response'
     # ./lib/onebox/helpers.rb:69:in `fetch_response'
     # ./lib/onebox/helpers.rb:28:in `fetch_html_doc'
     # ./lib/onebox/engine/youtube_onebox.rb:21:in `parse_embed_response'
     # ./lib/onebox/engine/youtube_onebox.rb:42:in `placeholder_html'
     # ./lib/onebox/preview.rb:28:in `placeholder_html'
     # ./spec/lib/onebox/engine/youtube_onebox_spec.rb:110:in `block (2 levels) in <main>'
     # ./spec/rails_helper.rb:328:in `block (2 levels) in <top (required)>'

Finished in 0.89418 seconds (files took 5.04 seconds to load)
17 examples, 1 failure

Failed examples:

rspec ./spec/lib/onebox/engine/youtube_onebox_spec.rb:109 # Onebox::Engine::YoutubeOnebox can parse youtube shorts results

```

Do you have any clue why it fails?

Also, what do these parts do in `before do`?

```ruby
onebox_response("youtube-embed")

```

The “youtube-embed” string doesn’t seem to be used anywhere in Discourse’s code except here. What is its purpose?

Why are there twice /embed/ links and why `onebox_response` has a different string parameter?

```ruby
stub_request(:get, "https://www.youtube.com/embed/21Lk4YiASMo")
.to_return(status: 200, body: onebox_response("youtube"))

```

```ruby
stub_request(:get, "https://www.youtube.com/embed/KCyIfcevExE")
.to_return(status: 200, body: onebox_response("youtube-embed"))

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [December 2, 2022, 8:56pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/2 "2022-12-02T20:56:28Z")

</div>

> [@Canapin](#):
>
> Do you have any clue why it fails?

I’m not very good at this either, but you need to make that stub have exacctly the same stuff that youtube would return. So it needs to have some subset of what `wget https://www.youtube.com/embed/wi2jAtpBl0Y` returns. That’s what your spec needs as its input.

```plaintext
wc wi2jAtpBl0Y 
    6 251 65245 wi2jAtpBl0Y

```

That’s a lot of text to make sense of and stick in your stub.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [December 2, 2022, 9:57pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/3 "2022-12-02T21:57:52Z")

</div>

Can you open a draft PR with your current changes? That will be easier to follow along and help with. With the current information it looks like you need to create the stub files and stub and the outgoing requests.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [December 2, 2022, 10:35pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/4 "2022-12-02T22:35:07Z")

</div>

Sure, here it is!  
[https://github.com/discourse/discourse/pull/19305](https://github.com/discourse/discourse/pull/19305)

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [December 5, 2022, 11:43pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/5 "2022-12-05T23:43:16Z")

</div>

I fixed it in a new PR that has correct attribution to you as a Co-Author:

[https://github.com/discourse/discourse/pull/19335](https://github.com/discourse/discourse/pull/19335)

> [@Canapin](#):
>
> Why are there twice /embed/ links and why `onebox_response` has a different string parameter?

To make our test suite not depend on a working internet connection (and working Youtube), we stub all internet requests by making those call return with a cached response. the `onebox_response` function will call the file with a matching name as the parameter passed from the folder [discourse/spec/fixtures/onebox at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/tree/main/spec/fixtures/onebox).

I added a new .response file for a shorts so we have a good test, but that was all that was necessary for this. Thanks for the PR!

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [December 6, 2022, 5:17pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/6 "2022-12-06T17:17:37Z")

</div>

Testing shorts onebox:

`https://youtube.com/shorts/VvoFuaLAslw`

https://www.youtube.com/embed/VvoFuaLAslw?feature=oembed&wmode=opaque

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [December 6, 2022, 5:19pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/7 "2022-12-06T17:19:37Z")

</div>

> **Video unavailable**  
> The uploader has not made this video available in your country

Besides this, it should work 😄

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [December 6, 2022, 5:22pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/8 "2022-12-06T17:22:01Z")

</div>

What about

[![](https://global.discourse-cdn.com/meta/original/4X/6/4/3/643f89770b193dfe4cdeb02072f3c42010177e46.jpeg "I got him") ](https://www.youtube.com/watch?v=kt7jiaGVsto)

?

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [December 6, 2022, 5:47pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/9 "2022-12-06T17:47:28Z")

</div>

It works for me.

Quick question. Here’s the email notification I got:

 ![image](https://global.discourse-cdn.com/meta/original/4X/c/a/1/ca1604d9d90a69f5a38916f6101276ba72b0d921.png)

The URL here is /embed/ and not /shorts/, but I suppose this is normal?

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [January 5, 2023, 5:47pm UTC](https://meta.discourse.org/t/trouble-on-adding-a-simple-unit-test-for-youtube-oneboxing/247546/10 "2023-01-05T17:47:40Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
