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 (
"io"
"net/http"
"net/http/httptest"
"testing"
)
func testServer(t *testing.T) *httptest.Server {
t.Helper()
db, err := openDB(":memory:")
if err != nil {
t.Fatalf("openDB: %v", err)
}
t.Cleanup(func() { db.Close() })
insertLink(db, "https://example.com")
return httptest.NewServer(newMux(db))
}
func TestRobotsTxt(t *testing.T) {
srv := testServer(t)
defer srv.Close()
resp, err := http.Get(srv.URL + "/robots.txt")
if err != nil {
t.Fatalf("GET /robots.txt: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("status = %d, want 200", resp.StatusCode)
}
ct := resp.Header.Get("Content-Type")
if ct != "text/plain" {
t.Errorf("Content-Type = %q, want text/plain", ct)
}
body, _ := io.ReadAll(resp.Body)
want := "User-agent: *\nDisallow: /\n"
if string(body) != want {
t.Errorf("body = %q, want %q", body, want)
}
}
func TestRedirect(t *testing.T) {
srv := testServer(t)
defer srv.Close()
short, _ := encodeID(1)
client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
}}
resp, err := client.Get(srv.URL + "/" + short)
if err != nil {
t.Fatalf("GET /%s: %v", short, err)
}
defer resp.Body.Close()
if resp.StatusCode != 301 {
t.Errorf("status = %d, want 301", resp.StatusCode)
}
loc := resp.Header.Get("Location")
if loc != "https://example.com" {
t.Errorf("Location = %q, want %q", loc, "https://example.com")
}
}
func TestNotFoundInvalidID(t *testing.T) {
srv := testServer(t)
defer srv.Close()
resp, err := http.Get(srv.URL + "/ZZZZ")
if err != nil {
t.Fatalf("GET /ZZZZ: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 404 {
t.Errorf("status = %d, want 404", resp.StatusCode)
}
}
func TestNotFoundRoot(t *testing.T) {
srv := testServer(t)
defer srv.Close()
resp, err := http.Get(srv.URL + "/")
if err != nil {
t.Fatalf("GET /: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 404 {
t.Errorf("status = %d, want 404", resp.StatusCode)
}
}
func TestNotFoundNested(t *testing.T) {
srv := testServer(t)
defer srv.Close()
resp, err := http.Get(srv.URL + "/nested/path")
if err != nil {
t.Fatalf("GET /nested/path: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 404 {
t.Errorf("status = %d, want 404", resp.StatusCode)
}
}
|