aboutsummaryrefslogtreecommitdiffstats
path: root/repo/repo_test.go
blob: 57f249c796e4acc4849407b3541621012b2fc9a4 (plain) (blame)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package repo_test

import (
	"bytes"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"go.sudomsg.com/gopkgserver/repo"
)

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

	meta, err := repo.NewMeta()
	if err != nil {
		t.Fatalf("failed to create Meta: %v", err)
	}

	pkg := "example.com/foo"
	r := repo.Repo{
		VCS:        "git",
		Repository: "https://github.com/user/foo",
		Home:       "https://github.com/user/foo",
		Directory:  "https://github.com/user/foo/tree/master{/dir}",
		File:       "https://github.com/user/foo/blob/master{/dir}/{file}#L{line}",
	}

	html, err := meta.Exec(pkg, r)
	if err != nil {
		t.Fatalf("Exec failed: %v", err)
	}

	htmlStr := string(html)
	if !strings.Contains(htmlStr, `<meta name="go-import"`) {
		t.Fatalf("missing go-import meta tag: %q", htmlStr)
	}
	if !strings.Contains(htmlStr, `<meta name="go-source"`) {
		t.Fatalf("missing go-source meta tag: %q", htmlStr)
	}
	if !strings.Contains(htmlStr, pkg) {
		t.Fatalf("missing package name in output: %q", htmlStr)
	}
}

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

	repos := map[string]repo.Repo{
		"example.com/foo": {
			VCS:        "git",
			Repository: "https://github.com/user/foo",
			Home:       "https://github.com/user/foo",
			Directory:  "https://github.com/user/foo/tree/master{/dir}",
			File:       "https://github.com/user/foo/blob/master{/dir}/{file}#L{line}",
		},
	}

	t.Run("returns meta tag for go-get=1", func(t *testing.T) {
		t.Parallel()

		handler, err := repo.New(repos)
		if err != nil {
			t.Fatalf("failed to create handler: %v", err)
		}

		req := httptest.NewRequest(http.MethodGet, "http://example.com/foo?go-get=1", nil)
		w := httptest.NewRecorder()

		handler.ServeHTTP(w, req)

		resp := w.Result()
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusOK {
			t.Fatalf("expected %v, got %v", http.StatusOK, resp.StatusCode)
		}

		var buf bytes.Buffer
		buf.ReadFrom(resp.Body)
		body := buf.String()

		if !strings.Contains(body, `<meta name="go-import"`) {
			t.Fatalf("expected go-import tag in body, got: %v", body)
		}
	})

	t.Run("redirects to pkg.go.dev otherwise", func(t *testing.T) {
		t.Parallel()

		handler, err := repo.New(repos)
		if err != nil {
			t.Fatalf("failed to create handler: %v", err)
		}

		req := httptest.NewRequest(http.MethodGet, "http://example.com/foo", nil)
		w := httptest.NewRecorder()

		handler.ServeHTTP(w, req)

		resp := w.Result()
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusFound {
			t.Fatalf("expected %v, got %v", http.StatusFound, resp.StatusCode)
		}
		loc, err := resp.Location()
		if err != nil {
			t.Fatalf("Invalid redirect location: %v", err)
		}

		if !strings.HasPrefix(loc.String(), "https://pkg.go.dev/example.com/foo") {
			t.Fatalf("unexpected redirect location: %v", loc)
		}
	})

	t.Run("returns 404 for unknown path", func(t *testing.T) {
		t.Parallel()

		handler, err := repo.New(repos)
		if err != nil {
			t.Fatalf("failed to create handler: %v", err)
		}

		w := httptest.NewRecorder()
		req := httptest.NewRequest(http.MethodGet, "http://example.com/bar", nil)
		handler.ServeHTTP(w, req)

		resp := w.Result()
		defer resp.Body.Close()

		if resp.StatusCode != http.StatusNotFound {
			t.Fatalf("expected %v, got %v", http.StatusNotFound, resp.StatusCode)
		}
	})
}