This commit is contained in:
@@ -2,7 +2,12 @@ package services
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -176,3 +181,58 @@ func TestSearchServiceFetchCacheRoundTrip(t *testing.T) {
|
||||
t.Fatalf("unexpected cached body: %q", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchServiceSkipsArtgridAPIAfter403(t *testing.T) {
|
||||
var apiRequests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case strings.HasPrefix(r.URL.Path, "/api/clip/details"):
|
||||
apiRequests.Add(1)
|
||||
http.Error(w, "forbidden", http.StatusForbidden)
|
||||
case strings.HasPrefix(r.URL.Path, "/clip/114756/"):
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = fmt.Fprintf(w, `<html><head><title>Friendly Couple | Stock Video Footage - Artgrid.io</title><meta property="og:title" content="Friendly Couple"><meta property="og:description" content="A warm couple moment"></head><body><script>window.__clip="%s";</script></body></html>`, "114756")
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
service := NewSearchService(server.URL, "", "")
|
||||
serverURL, err := url.Parse(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse test server url: %v", err)
|
||||
}
|
||||
service.Client = &http.Client{
|
||||
Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
clone := req.Clone(req.Context())
|
||||
if clone.URL.Host == "artgrid.io" {
|
||||
clone.URL.Scheme = serverURL.Scheme
|
||||
clone.URL.Host = serverURL.Host
|
||||
clone.Host = serverURL.Host
|
||||
}
|
||||
return http.DefaultTransport.RoundTrip(clone)
|
||||
}),
|
||||
}
|
||||
|
||||
item := SearchResult{
|
||||
Link: "https://artgrid.io/clip/114756/friendly-couple",
|
||||
Source: "Artgrid",
|
||||
}
|
||||
|
||||
first := service.enrichArtgrid(item)
|
||||
second := service.enrichArtgrid(item)
|
||||
|
||||
if apiRequests.Load() != 1 {
|
||||
t.Fatalf("expected artgrid API to be skipped after first 403, got %d requests", apiRequests.Load())
|
||||
}
|
||||
if first.Title == "" || second.Title == "" {
|
||||
t.Fatalf("expected HTML fallback enrichment to preserve title, got %#v %#v", first, second)
|
||||
}
|
||||
}
|
||||
|
||||
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return fn(req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user