Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion internal/pkg/postprocessor/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package postprocessor

import (
"bytes"
_ "embed"
"io"
"net/http"
"os"
Expand All @@ -12,8 +13,11 @@ import (
"github.com/internetarchive/gowarc/pkg/spooledtempfile"
)

//go:embed testdata/ina_api_response.json
var inaFixture []byte

func TestExtractAssets_HTML(t *testing.T) {
config.Set(&config.Config{})
config.InitConfig()
config.Get().DisableHTMLTag = []string{} // initialize as empty slice

// Create a mock response with a minimal HTML body
Expand Down Expand Up @@ -58,6 +62,30 @@ func TestExtractAssets_HTML(t *testing.T) {
}
}

func TestExtractAssets_HydrateItemFixture(t *testing.T) {
config.InitConfig()
item := testutil.HydrateItem(t, inaFixture)
assets, _, err := ExtractAssetsOutlinks(item)
if err != nil {
t.Fatalf("extract assets from fixture: %v", err)
}
// INA API fixture should yield at least resourceUrl, resourceThumbnail, embed URL, uri
if len(assets) < 1 {
t.Errorf("expected at least one asset from INA fixture, got %d", len(assets))
}
// Sanity: one of the assets should be the resource URL from the fixture body
found := false
for _, a := range assets {
if a != nil && a.Raw == "https://example.com/video.mp4" {
found = true
break
}
}
if !found {
t.Errorf("expected asset https://example.com/video.mp4 in %v", assets)
}
}

func TestSanitizeAssetsOutlinks(t *testing.T) {
var err error
newURL, _ := models.NewURL("http://example.com")
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/postprocessor/testdata/ina_api_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"url": "https://apipartner.ina.fr/assets/CAF94008902",
"status_code": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"id\":\"CAF94008902\",\"resourceUrl\":\"https://example.com/video.mp4\",\"resourceThumbnail\":\"https://example.com/thumb.jpg\",\"embedUrl\":\"/player/embed/CAF94008902\",\"uri\":\"https://example.com/uri\"}"
}
106 changes: 106 additions & 0 deletions internal/pkg/postprocessor/testutil/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package testutil

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"testing"

"github.com/internetarchive/Zeno/pkg/models"

Check failure on line 12 in internal/pkg/postprocessor/testutil/item.go

View workflow job for this annotation

GitHub Actions / build-and-test

no required module provides package github.com/internetarchive/Zeno/pkg/models; to add it:

Check failure on line 12 in internal/pkg/postprocessor/testutil/item.go

View workflow job for this annotation

GitHub Actions / cross-build

no required module provides package github.com/internetarchive/Zeno/pkg/models; to add it:
"github.com/internetarchive/gowarc/pkg/spooledtempfile"
)

// ItemFixture is the JSON schema for a serialized models.Item test fixture.
// It can be embedded with go:embed and passed to HydrateItem.
type ItemFixture struct {
URL string `json:"url"`
StatusCode int `json:"status_code,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
Source string `json:"source,omitempty"`
Hops int `json:"hops,omitempty"`
}

// HydrateItem unmarshals a JSON fixture and builds a *models.Item for use in tests.
// Defaults: status_code=200, headers=empty, body=empty, source=unset, hops=0.
// Uses t.Helper() and t.Fatal on errors.
func HydrateItem(t *testing.T, data []byte) *models.Item {
t.Helper()

var f ItemFixture
if err := json.Unmarshal(data, &f); err != nil {
t.Fatalf("unmarshal fixture: %v", err)
}

if f.URL == "" {
t.Fatal("fixture url is required")
}

statusCode := f.StatusCode
if statusCode == 0 {
statusCode = 200
}

resp := &http.Response{
StatusCode: statusCode,
Header: make(http.Header),
Body: io.NopCloser(bytes.NewBufferString(f.Body)),
}
for k, v := range f.Headers {
resp.Header.Set(k, v)
}

newURL, err := models.NewURL(f.URL)
if err != nil {
t.Fatalf("create URL: %v", err)
}
newURL.SetResponse(resp)

spooledTempFile := spooledtempfile.NewSpooledTempFile("test", os.TempDir(), 2048, false, -1)
spooledTempFile.Write([]byte(f.Body))
newURL.SetBody(spooledTempFile)
if err := newURL.Parse(); err != nil {
t.Fatalf("parse URL: %v", err)
}
if f.Hops != 0 {
newURL.SetHops(f.Hops)
}

item := models.NewItem(&newURL, "")
if item == nil {
t.Fatal("NewItem returned nil")
}

if f.Source != "" {
src, err := parseItemSource(f.Source)
if err != nil {
t.Fatalf("invalid source %q: %v", f.Source, err)
}
if err := item.SetSource(src); err != nil {
t.Fatalf("SetSource: %v", err)
}
}

return item
}

// parseItemSource maps fixture source strings to models.ItemSource constants.
func parseItemSource(s string) (models.ItemSource, error) {
switch s {
case "insert":
return models.ItemSourceInsert, nil
case "queue":
return models.ItemSourceQueue, nil
case "hq":
return models.ItemSourceHQ, nil
case "postprocess":
return models.ItemSourcePostprocess, nil
case "feedback":
return models.ItemSourceFeedback, nil
default:
return 0, fmt.Errorf("unknown source %q (use: insert, queue, hq, postprocess, feedback)", s)
}
}
Loading