aboutsummaryrefslogtreecommitdiff
path: root/internal/domain_redirect_test.go
blob: ba922d05c909f1b9e94d7ded6e68c1a71db664e2 (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
package internal

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestDomainRedirect(t *testing.T) {
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	})

	t.Run("development", func(t *testing.T) {
		r := httptest.NewRequest("GET", "http://localhost/", nil)
		w := httptest.NewRecorder()

		DomainRedirect(h, true).ServeHTTP(w, r)

		if w.Code != http.StatusOK {
			t.Errorf("expected status 200, got %d", w.Code)
		}
	})

	t.Run("redirect", func(t *testing.T) {
		r := httptest.NewRequest("GET", "http://example.com/", nil)
		w := httptest.NewRecorder()

		DomainRedirect(h, false).ServeHTTP(w, r)

		if w.Code != http.StatusMovedPermanently {
			t.Errorf("expected status 301, got %d", w.Code)
		}
	})

	t.Run("allowed", func(t *testing.T) {
		r := httptest.NewRequest("GET", "http://example.com/blog.rss", nil)
		w := httptest.NewRecorder()

		DomainRedirect(h, false).ServeHTTP(w, r)

		if w.Code != http.StatusOK {
			t.Errorf("expected status 200, got %d", w.Code)
		}
	})
}