aboutsummaryrefslogtreecommitdiff
path: root/httputil/static_test.go
blob: a258d523dcd6e74babe25a14a152eadff2405f8c (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.

package httputil_test

import (
	"crypto/sha1"
	"encoding/hex"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"strconv"
	"testing"
	"time"

	"github.com/golang/gddo/httputil"
	"github.com/google/go-cmp/cmp"
)

var (
	testHash          = computeTestHash()
	testEtag          = `"` + testHash + `"`
	testContentLength = computeTestContentLength()
)

func mustParseURL(urlStr string) *url.URL {
	u, err := url.Parse(urlStr)
	if err != nil {
		panic(err)
	}
	return u
}

func computeTestHash() string {
	p, err := ioutil.ReadFile("static_test.go")
	if err != nil {
		panic(err)
	}
	w := sha1.New()
	w.Write(p)
	return hex.EncodeToString(w.Sum(nil))
}

func computeTestContentLength() string {
	info, err := os.Stat("static_test.go")
	if err != nil {
		panic(err)
	}
	return strconv.FormatInt(info.Size(), 10)
}

var fileServerTests = []*struct {
	name   string // test name for log
	ss     *httputil.StaticServer
	r      *http.Request
	header http.Header // expected response headers
	status int         // expected response status
	empty  bool        // true if response body not expected.
}{
	{
		name: "get",
		ss:   &httputil.StaticServer{MaxAge: 3 * time.Second},
		r: &http.Request{
			URL:    mustParseURL("/dir/static_test.go"),
			Method: "GET",
		},
		status: http.StatusOK,
		header: http.Header{
			"Etag":           {testEtag},
			"Cache-Control":  {"public, max-age=3"},
			"Content-Length": {testContentLength},
			"Content-Type":   {"application/octet-stream"},
		},
	},
	{
		name: "get .",
		ss:   &httputil.StaticServer{Dir: ".", MaxAge: 3 * time.Second},
		r: &http.Request{
			URL:    mustParseURL("/dir/static_test.go"),
			Method: "GET",
		},
		status: http.StatusOK,
		header: http.Header{
			"Etag":           {testEtag},
			"Cache-Control":  {"public, max-age=3"},
			"Content-Length": {testContentLength},
			"Content-Type":   {"application/octet-stream"},
		},
	},
	{
		name: "get with ?v=",
		ss:   &httputil.StaticServer{MaxAge: 3 * time.Second},
		r: &http.Request{
			URL:    mustParseURL("/dir/static_test.go?v=xxxxx"),
			Method: "GET",
		},
		status: http.StatusOK,
		header: http.Header{
			"Etag":           {testEtag},
			"Cache-Control":  {"public, max-age=31536000"},
			"Content-Length": {testContentLength},
			"Content-Type":   {"application/octet-stream"},
		},
	},
	{
		name: "head",
		ss:   &httputil.StaticServer{MaxAge: 3 * time.Second},
		r: &http.Request{
			URL:    mustParseURL("/dir/static_test.go"),
			Method: "HEAD",
		},
		status: http.StatusOK,
		header: http.Header{
			"Etag":           {testEtag},
			"Cache-Control":  {"public, max-age=3"},
			"Content-Length": {testContentLength},
			"Content-Type":   {"application/octet-stream"},
		},
		empty: true,
	},
	{
		name: "if-none-match",
		ss:   &httputil.StaticServer{MaxAge: 3 * time.Second},
		r: &http.Request{
			URL:    mustParseURL("/dir/static_test.go"),
			Method: "GET",
			Header: http.Header{"If-None-Match": {testEtag}},
		},
		status: http.StatusNotModified,
		header: http.Header{
			"Cache-Control": {"public, max-age=3"},
			"Etag":          {testEtag},
		},
		empty: true,
	},
}

func testStaticServer(t *testing.T, f func(*httputil.StaticServer) http.Handler) {
	for _, tt := range fileServerTests {
		w := httptest.NewRecorder()

		h := f(tt.ss)
		h.ServeHTTP(w, tt.r)

		if w.Code != tt.status {
			t.Errorf("%s, status=%d, want %d", tt.name, w.Code, tt.status)
		}

		if !cmp.Equal(w.HeaderMap, tt.header) {
			t.Errorf("%s\n\theader=%v,\n\twant   %v", tt.name, w.HeaderMap, tt.header)
		}

		empty := w.Body.Len() == 0
		if empty != tt.empty {
			t.Errorf("%s empty=%v, want %v", tt.name, empty, tt.empty)
		}
	}
}

func TestFileHandler(t *testing.T) {
	testStaticServer(t, func(ss *httputil.StaticServer) http.Handler { return ss.FileHandler("static_test.go") })
}

func TestDirectoryHandler(t *testing.T) {
	testStaticServer(t, func(ss *httputil.StaticServer) http.Handler { return ss.DirectoryHandler("/dir", ".") })
}

func TestFilesHandler(t *testing.T) {
	testStaticServer(t, func(ss *httputil.StaticServer) http.Handler { return ss.FilesHandler("static_test.go") })
}