internal/providers/anipub/megaplay_decrypt_test.go
64 lines · 2022 bytes
4 symbols in this file
1package anipub
2
3import (
4 "encoding/json"
5 "net/http"
6 "net/http/httptest"
7 "strings"
8 "testing"
9)
10
11const testMegaplayEnc = "wdeBruh3qqn_i5wUNnyaPcXqidp1UWP84FfPHzGyKXAz4mAVkH6j3DueswO2yXLWn8H-XMHNvbAo5Gsg7zIcFBuQI_zsUvMGI1gKwQsPTSHQHiF55R4BopgEQ-7jebQQ4C0Gu7YhaMucopp6d3Q8yAY9b5GdsSvPGq6CUn7SHyc"
12
13func TestDecryptMegaplayEnc(t *testing.T) {
14 got, err := decryptMegaplayEnc(testMegaplayEnc)
15 if err != nil {
16 t.Fatalf("decryptMegaplayEnc: %v", err)
17 }
18 want := "https://fetch.nexabloom.top/anime/bb6d2babd7797d94d8f4a8600bc9b44e/b7d51fb7e838ee9b60dcdb34b953bc07/master.m3u8"
19 if got != want {
20 t.Fatalf("got %q, want %q", got, want)
21 }
22}
23
24func TestDecryptMegaplayEncRejectsGarbage(t *testing.T) {
25 for _, enc := range []string{"", "!!!", "aGVsbG8="} {
26 if _, err := decryptMegaplayEnc(enc); err == nil {
27 t.Fatalf("expected error for %q", enc)
28 }
29 }
30}
31
32func TestResolveMegaplayStreamFallsBackToEnc(t *testing.T) {
33 megaplay := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34 switch r.URL.Path {
35 case "/stream/s-2/42/sub":
36 w.Header().Set("Content-Type", "text/html")
37 _, _ = w.Write([]byte(`<div data-id="99"></div>`))
38 case "/stream/getSources":
39 _ = json.NewEncoder(w).Encode(megaplaySourcesResponse{Enc: testMegaplayEnc})
40 default:
41 http.NotFound(w, r)
42 }
43 }))
44 defer megaplay.Close()
45
46 client := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
47 if strings.HasPrefix(req.URL.String(), megaplay.URL) {
48 return http.DefaultTransport.RoundTrip(req)
49 }
50 return textResponse(req, http.StatusNotFound, ""), nil
51 })}
52 withAnipubTestClient(t, client)
53 originalMega := megaplayBaseURL
54 t.Cleanup(func() { megaplayBaseURL = originalMega })
55 megaplayBaseURL = megaplay.URL
56
57 streamURL, _, err := resolveMegaplayStream("https://anipub.xyz/video/42/sub", "sub")
58 if err != nil {
59 t.Fatalf("resolveMegaplayStream: %v", err)
60 }
61 if !strings.HasSuffix(streamURL, "/master.m3u8") {
62 t.Fatalf("unexpected stream %q", streamURL)
63 }
64}