-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
91 lines (77 loc) · 2.51 KB
/
main_test.go
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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/nielsole/go_tile/utils"
)
// cpu: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
// BenchmarkPngRead-4 88027 13260 ns/op
// BenchmarkWriteTileResponse-4 55233 21662 ns/op
// 22 microsecond and 13microsecond avg. response time is really nothing worth optimizing
func BenchmarkPngRead(b *testing.B) {
for i := 0; i < b.N; i++ {
readTile("mock_data/0.meta", 0)
}
}
func BenchmarkParsePath(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, _, _, err := utils.ParsePath("/tile/4/3/2.png")
if err != nil {
b.Error(err)
}
}
}
func TestWriteTileResponse(t *testing.T) {
// Prepare test request and response
req := httptest.NewRequest("GET", "/tile/0/0/0.png", nil)
w := httptest.NewRecorder()
// Call writeTileResponse function
modTime := time.Now()
err := writeTileResponse(w, req, "mock_data/0.meta", 0, modTime, "png")
if err != nil {
t.Fatal(err)
}
// Check response status code
if w.Code != http.StatusOK {
t.Errorf("expected status code %d but got %d", http.StatusOK, w.Code)
}
// Check response header
if w.Header().Get("Content-Type") != "image/png" {
t.Errorf("unexpected content type: %s", w.Header().Get("Content-Type"))
}
if w.Body.Len() < 100 {
t.Errorf("expected body,but got %q", w.Body.String())
}
// Check response caching headers
expectedCacheControl := "no-cache"
if w.Header().Get("Cache-Control") != expectedCacheControl {
t.Errorf("unexpected Cache-Control header: %s", w.Header().Get("Cache-Control"))
}
expectedLastModified := modTime.UTC().Format(http.TimeFormat)
if w.Header().Get("Last-Modified") != expectedLastModified {
t.Errorf("unexpected Last-Modified header: %s", w.Header().Get("Last-Modified"))
}
}
func TestWriteTileResponse404(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/", bytes.NewReader([]byte{}))
resp := httptest.ResponseRecorder{}
if err := writeTileResponse(&resp, req, "mock_data/404.meta", 0, time.Now(), "png"); err != nil {
t.Error(err)
}
if resp.Code != 404 {
t.Errorf("Unexpected response code: %d", resp.Code)
}
}
func TestWriteTileResponseOutOfBounds(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/", bytes.NewReader([]byte{}))
resp := httptest.ResponseRecorder{}
if err := writeTileResponse(&resp, req, "mock_data/0.meta", 65, time.Now(), "png"); err != nil {
t.Error(err)
}
if resp.Code != 500 {
t.Errorf("Unexpected response code: %d", resp.Code)
}
}