aboutsummaryrefslogtreecommitdiff
path: root/internal/ogtags/fetch_test.go
blob: 60af957f6c14db4f6122d91c1091f9d05ddcfd6b (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
package ogtags

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"
	"testing"
	"time"
)

func TestFetchHTMLDocument(t *testing.T) {
	tests := []struct {
		name          string
		htmlContent   string
		contentType   string
		statusCode    int
		contentLength int64
		expectError   bool
	}{
		{
			name: "Valid HTML",
			htmlContent: `<!DOCTYPE html>
				<html>
				<head><title>Test</title></head>
				<body><p>Test content</p></body>
				</html>`,
			contentType: "text/html",
			statusCode:  http.StatusOK,
			expectError: false,
		},
		{
			name:        "Empty HTML",
			htmlContent: "",
			contentType: "text/html",
			statusCode:  http.StatusOK,
			expectError: false,
		},
		{
			name:        "Not found error",
			htmlContent: "",
			contentType: "text/html",
			statusCode:  http.StatusNotFound,
			expectError: true,
		},
		{
			name:        "Unsupported Content-Type",
			htmlContent: "*Insert rick roll here*",
			contentType: "video/mp4",
			statusCode:  http.StatusOK,
			expectError: true,
		},
		{
			name:          "Too large content",
			contentType:   "text/html",
			statusCode:    http.StatusOK,
			expectError:   true,
			contentLength: 5 * 1024 * 1024, // 5MB (over 2MB limit)
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				if tt.contentType != "" {
					w.Header().Set("Content-Type", tt.contentType)
				}
				if tt.contentLength > 0 {
					// Simulate content length but avoid sending too much actual data
					w.Header().Set("Content-Length", fmt.Sprintf("%d", tt.contentLength))
					io.CopyN(w, strings.NewReader("X"), tt.contentLength)
				} else {
					w.WriteHeader(tt.statusCode)
					w.Write([]byte(tt.htmlContent))
				}
			}))
			defer ts.Close()

			cache := NewOGTagCache("", true, time.Minute)
			doc, err := cache.fetchHTMLDocument(ts.URL)

			if tt.expectError {
				if err == nil {
					t.Error("expected error, got nil")
				}
				if doc != nil {
					t.Error("expected nil document on error, got non-nil")
				}
			} else {
				if err != nil {
					t.Errorf("unexpected error: %v", err)
				}
				if doc == nil {
					t.Error("expected non-nil document, got nil")
				}
			}
		})
	}
}

func TestFetchHTMLDocumentInvalidURL(t *testing.T) {
	if os.Getenv("DONT_USE_NETWORK") != "" {
		t.Skip("test requires theoretical network egress")
	}

	cache := NewOGTagCache("", true, time.Minute)

	doc, err := cache.fetchHTMLDocument("http://invalid.url.that.doesnt.exist.example")

	if err == nil {
		t.Error("expected error for invalid URL, got nil")
	}

	if doc != nil {
		t.Error("expected nil document for invalid URL, got non-nil")
	}
}