krz/skunky-art

Alternative privacy frontend for DeviantArt.

clone: git clone https://gitbay.org/krz/skunky-art.git

v1.3.9: app/api_test.go · raw

 1package app
 2
 3import (
 4	"net/http/httptest"
 5	"testing"
 6
 7	"github.com/krazywarez/devianter"
 8)
 9
10// fullviewDeviation returns a deviation whose media assembles into a non-empty
11// wixmp URL, i.e. one that sendMedia is meant to serve.
12func fullviewDeviation() *devianter.Deviation {
13	d := &devianter.Deviation{}
14	d.Media.BaseUri = "https://images-wixmp-abc.wixmp.com/f/u/x.png"
15	d.Media.Name = "x"
16	d.Media.Types = append(d.Media.Types, struct {
17		T    string
18		H, W int
19	}{T: "fullview", H: 1920, W: 1280})
20	return d
21}
22
23// TestSendMediaServesRealMedia is the regression test for the inverted guard: a
24// deviation that has media must be sent, not dropped. In non-proxy mode that is
25// a 302 to the wixmp URL; the pre-fix guard returned before writing anything.
26func TestSendMediaServesRealMedia(t *testing.T) {
27	proxy := CFG.Proxy
28	CFG.Proxy = false
29	defer func() { CFG.Proxy = proxy }()
30
31	w := httptest.NewRecorder()
32	API{main: &skunkyart{Writer: w}}.sendMedia(fullviewDeviation())
33
34	if w.Code != 302 {
35		t.Errorf("status is %d, want a 302 redirect to the media", w.Code)
36	}
37	if w.Header().Get("Location") == "" {
38		t.Error("no Location header set — the media was dropped")
39	}
40}
41
42// TestSendMediaIgnoresEmptyMedia pins the other half of the bug: a deviation
43// with no media must be a no-op. With proxy on, the pre-fix code fell through to
44// mediaURL[21:] on an empty string and panicked.
45func TestSendMediaIgnoresEmptyMedia(t *testing.T) {
46	proxy := CFG.Proxy
47	CFG.Proxy = true
48	defer func() { CFG.Proxy = proxy }()
49
50	w := httptest.NewRecorder()
51	API{main: &skunkyart{Writer: w}}.sendMedia(&devianter.Deviation{})
52
53	if w.Code != 200 {
54		t.Errorf("status is %d, want nothing written (recorder default 200)", w.Code)
55	}
56	if loc := w.Header().Get("Location"); loc != "" {
57		t.Errorf("Location %q set for a media-less deviation, want none", loc)
58	}
59}