~evanj/cms

ref: 7c487cb4cca6290eae5f854300d6e9c78f81ea25 cms/internal/c/file/file_test.go -rw-r--r-- 2.2 KiB
7c487cb4Evan J Feat(dynamic): Dynamic content pages have been added (rather than 9 months ago
                                                                                
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
package file_test

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

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/c/file"
	E3 "git.sr.ht/~evanj/cms/pkg/e3"
	"github.com/bmizerany/assert"
	gomock "github.com/golang/mock/gomock"
)

var ctx = gomock.Any()

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

	var (
		ctrl  = gomock.NewController(t)
		db    = file.NewMockdber(ctrl)
		e3    = file.NewMockE3er(ctrl)
		s     = file.New(c.New(nil, db, true, "test"), nil, db, e3, E3.DefaultURL)
		ts    = httptest.NewServer(s)
		fp    = "/file/some-file.jpg"
		url   = strings.TrimRight(E3.DefaultURL, "api") + "some-file.jpg"
		bytes []byte
	)

	db.EXPECT().FileExists(ctx, E3.DefaultURL+fp).Return(true, nil).AnyTimes()
	e3.EXPECT().URL().Return(E3.DefaultURL).AnyTimes()
	e3.EXPECT().Proxy(gomock.Any(), url).Return(bytes, nil).AnyTimes()

	res, err := http.Get(ts.URL + fp)
	assert.Equal(t, nil, err)
	assert.Equal(t, res.StatusCode, http.StatusOK)
}

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

	var (
		ctrl = gomock.NewController(t)
		db   = file.NewMockdber(ctrl)
		e3   = file.NewMockE3er(ctrl)
		s    = file.New(c.New(nil, db, true, "test"), nil, db, e3, E3.DefaultURL)
		ts   = httptest.NewServer(s)
		fp   = "/file/some-file.jpg"
		url  = strings.TrimRight(E3.DefaultURL, "api") + "some-file.jpg"
		err  = errors.New("placeholder")
	)

	db.EXPECT().FileExists(ctx, E3.DefaultURL+fp).Return(true, nil).AnyTimes()
	e3.EXPECT().URL().Return(E3.DefaultURL).AnyTimes()
	e3.EXPECT().Proxy(gomock.Any(), url).Return(nil, err).AnyTimes()

	res, err := http.Get(ts.URL + fp)
	assert.Equal(t, nil, err)
	assert.Equal(t, res.StatusCode, http.StatusInternalServerError)
}

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

	var (
		ctrl = gomock.NewController(t)
		db   = file.NewMockdber(ctrl)
		e3   = file.NewMockE3er(ctrl)
		s    = file.New(c.New(nil, db, true, "test"), nil, db, e3, E3.DefaultURL)
		ts   = httptest.NewServer(s)
		fp   = "/file/some-file.jpg"
		err  = errors.New("placeholder")
	)

	db.EXPECT().FileExists(ctx, E3.DefaultURL+fp).Return(false, err).AnyTimes()

	res, err := http.Get(ts.URL + fp)
	assert.Equal(t, nil, err)
	assert.Equal(t, res.StatusCode, http.StatusNotFound)
}