code.pablomurad.com

goascii

Web app that renders images as colored ASCII art

pic2html_test.go raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main

import (
	"log"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestValidateRemoteURLRejectsBlockedTargets(t *testing.T) {
	t.Parallel()

	cases := []string{
		"http://localhost/image.png",
		"http://127.0.0.1/image.png",
		"http://[::1]/image.png",
	}

	for _, raw := range cases {
		u, err := url.Parse(raw)
		if err != nil {
			t.Fatalf("parse %q: %v", raw, err)
		}
		if err := validateRemoteURL(u); err == nil {
			t.Fatalf("expected %q to be blocked", raw)
		}
	}
}

func TestValidateRemoteURLAcceptsPublicHTTPS(t *testing.T) {
	t.Parallel()

	u, err := url.Parse("https://example.com/image.png")
	if err != nil {
		t.Fatal(err)
	}
	if err := validateRemoteURL(u); err != nil {
		t.Fatalf("expected public URL to be valid: %v", err)
	}
}

func TestHandleHomeUsesDemoByDefault(t *testing.T) {
	t.Parallel()

	app := newApp(log.New(os.Stdout, "", 0))
	app.demoPath = filepath.Join("img", "cat.png")

	req := httptest.NewRequest(http.MethodGet, "/", nil)
	rec := httptest.NewRecorder()

	app.handleHome(rec, req)

	if rec.Code != http.StatusOK {
		t.Fatalf("expected status 200, got %d", rec.Code)
	}
	body := rec.Body.String()
	if !strings.Contains(body, "goASCII - developed by Pablo Murad - 2026") {
		t.Fatalf("expected header text in response")
	}
	if strings.Contains(body, "hint: append ?url=https://example.com/image.png") {
		t.Fatalf("expected old hint to be removed")
	}
	if !strings.Contains(body, "source: cat.png") {
		t.Fatalf("expected cat demo source in response")
	}
	if !strings.Contains(body, `id="terminal-line"`) {
		t.Fatalf("expected terminal line markup in response")
	}
	if !strings.Contains(body, "Type help for usage.") {
		t.Fatalf("expected help prompt text in response")
	}
}

func TestHandleHomeRejectsMalformedURL(t *testing.T) {
	t.Parallel()

	app := newApp(log.New(os.Stdout, "", 0))
	req := httptest.NewRequest(http.MethodGet, "/?url=://bad-url", nil)
	rec := httptest.NewRecorder()

	app.handleHome(rec, req)

	if rec.Code != http.StatusBadRequest {
		t.Fatalf("expected status 400, got %d", rec.Code)
	}
	if !strings.Contains(rec.Body.String(), "invalid image URL") {
		t.Fatalf("expected invalid URL error, got %q", rec.Body.String())
	}
}

func TestLoadImageMarksRemoteSources(t *testing.T) {
	t.Parallel()

	app := newApp(log.New(os.Stdout, "", 0))
	_, sourceLabel, _, err := app.loadImage(t.Context(), "https://example.invalid/cat.png")
	if err == nil {
		t.Fatalf("expected remote image lookup to fail in test")
	}
	if sourceLabel != "source: remote" {
		t.Fatalf("expected remote source label, got %q", sourceLabel)
	}
}