Commit 9af155d40a
Unsigned
Layout: unified · split
app/api.go +1 −1
| @@ -46,7 +46,7 @@ func (a API) Error(description string, status int) { | |||
| 46 | func (a API) sendMedia(d *devianter.Deviation) { | 46 | func (a API) sendMedia(d *devianter.Deviation) { |
| 47 | mediaURL, name := devianter.UrlFromMedia(d.Media) | 47 | mediaURL, name := devianter.UrlFromMedia(d.Media) |
| 48 | a.main.SetFilename(name) | 48 | a.main.SetFilename(name) |
| 49 | if len(mediaURL) != 0 { | 49 | if len(mediaURL) == 0 { |
| 50 | return | 50 | return |
| 51 | } | 51 | } |
| 52 | 52 | ||
app/api_test.go added +59
| @@ -0,0 +1,59 @@ | |||
| 1 | package app | ||
| 2 | |||
| 3 | import ( | ||
| 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. | ||
| 12 | func 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. | ||
| 26 | func 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. | ||
| 45 | func 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 | } | ||