Problemi nell'aggiunta di un semplice unit test per Youtube oneboxing

Ciao!

Voglio aggiungere il supporto per i link di Youtube /shorts/.

La mia modifica alla classe YoutubeOnebox funziona, ma è necessario aggiungere un test in youtube_onebox.rb e il mio test non funziona.

Simile al test "can parse youtube embed results", ho aggiunto questo codice:

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

Ho anche aggiunto questo all’inizio del file, in before do:

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

Non ho mai fatto unit testing prima, quindi non capisco bene tutto questo lavoro.

Il mio test fallisce:

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

Hai qualche idea sul perché fallisce?

Inoltre, cosa fanno queste parti in before do?

onebox_response("youtube-embed")

La stringa “youtube-embed” non sembra essere utilizzata da nessuna parte nel codice di Discourse tranne che qui. Qual è il suo scopo?

Perché ci sono collegamenti /embed/ due volte e perché onebox_response ha un parametro stringa diverso?

stub_request(:get, "https://www.youtube.com/embed/21Lk4YiASMo")
.to_return(status: 200, body: onebox_response("youtube"))
stub_request(:get, "https://www.youtube.com/embed/KCyIfcevExE")
.to_return(status: 200, body: onebox_response("youtube-embed"))
1 Mi Piace

Non sono molto bravo nemmeno io, ma devi fare in modo che quello stub abbia esattamente le stesse cose che YouTube restituirebbe. Quindi deve avere un sottoinsieme di ciò che restituisce wget https://www.youtube.com/embed/wi2jAtpBl0Y. Questo è ciò che la tua specifica necessita come input.

wc wi2jAtpBl0Y 
    6   251 65245 wi2jAtpBl0Y

È un sacco di testo da capire e inserire nel tuo stub.

1 Mi Piace

Puoi aprire una bozza di PR con le tue modifiche attuali? Sarà più facile da seguire e da aiutare. Con le informazioni attuali sembra che tu debba creare i file stub e stub e le richieste in uscita.

2 Mi Piace

Certo, eccolo!

3 Mi Piace

L’ho corretto in una nuova PR che ti attribuisce correttamente come Co-Autore:

Per fare in modo che la nostra suite di test non dipenda da una connessione internet funzionante (e da Youtube funzionante), intercettiamo tutte le richieste internet facendo in modo che tali chiamate restituiscano una risposta memorizzata nella cache. la funzione onebox_response chiamerà il file con un nome corrispondente al parametro passato dalla cartella discourse/spec/fixtures/onebox at main · discourse/discourse · GitHub.

Ho aggiunto un nuovo file .response per uno short in modo da avere un buon test, ma era tutto ciò che era necessario per questo. Grazie per la PR!

3 Mi Piace

Test di shorts onebox:

https://youtube.com/shorts/VvoFuaLAslw

1 Mi Piace
  • Video non disponibile
  • Il caricatore non ha reso disponibile questo video nel tuo paese

A parte questo, dovrebbe funzionare :smile:

1 Mi Piace

E per quanto riguarda

?

Funziona per me.

Domanda veloce. Ecco la notifica e-mail che ho ricevuto:

L’URL qui è /embed/ e non /shorts/, ma suppongo che sia normale?

1 Mi Piace

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