|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCanonicalTilepackAsset(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + format string |
| 15 | + href string |
| 16 | + fileSize int64 |
| 17 | + wantType string |
| 18 | + wantTitle string |
| 19 | + wantHasSize bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "mbtiles with size", |
| 23 | + format: "mbtiles", |
| 24 | + href: "https://example.test/item.mbtiles", |
| 25 | + fileSize: 12345, |
| 26 | + wantType: "application/vnd.mbtiles", |
| 27 | + wantTitle: "MBTILES archive", |
| 28 | + wantHasSize: true, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "pmtiles without size", |
| 32 | + format: "pmtiles", |
| 33 | + href: "https://example.test/item.pmtiles", |
| 34 | + fileSize: 0, |
| 35 | + wantType: "application/vnd.pmtiles", |
| 36 | + wantTitle: "PMTILES archive", |
| 37 | + wantHasSize: false, |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + asset := canonicalTilepackAsset(tt.format, tt.href, tt.fileSize) |
| 44 | + |
| 45 | + if asset.Href != tt.href { |
| 46 | + t.Fatalf("Href = %q, want %q", asset.Href, tt.href) |
| 47 | + } |
| 48 | + if asset.Type != tt.wantType { |
| 49 | + t.Fatalf("Type = %q, want %q", asset.Type, tt.wantType) |
| 50 | + } |
| 51 | + if asset.Title != tt.wantTitle { |
| 52 | + t.Fatalf("Title = %q, want %q", asset.Title, tt.wantTitle) |
| 53 | + } |
| 54 | + if len(asset.Roles) != 1 || asset.Roles[0] != "tiles" { |
| 55 | + t.Fatalf("Roles = %#v, want [\"tiles\"]", asset.Roles) |
| 56 | + } |
| 57 | + if asset.ProjCode != 3857 { |
| 58 | + t.Fatalf("ProjCode = %d, want 3857", asset.ProjCode) |
| 59 | + } |
| 60 | + if tt.wantHasSize { |
| 61 | + if asset.FileSize != tt.fileSize { |
| 62 | + t.Fatalf("FileSize = %d, want %d", asset.FileSize, tt.fileSize) |
| 63 | + } |
| 64 | + } else if asset.FileSize != 0 { |
| 65 | + t.Fatalf("FileSize = %d, want 0 when size is not positive", asset.FileSize) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestParseZooms(t *testing.T) { |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + min string |
| 75 | + max string |
| 76 | + wantMin int |
| 77 | + wantMax int |
| 78 | + wantErrMsg string |
| 79 | + }{ |
| 80 | + {name: "default zooms", min: "", max: "", wantMin: 0, wantMax: 0}, |
| 81 | + {name: "explicit valid range", min: "3", max: "8", wantMin: 3, wantMax: 8}, |
| 82 | + {name: "missing max", min: "3", max: "", wantErrMsg: "min_zoom and max_zoom must both be set"}, |
| 83 | + {name: "invalid min", min: "x", max: "8", wantErrMsg: "min_zoom must be an integer"}, |
| 84 | + {name: "invalid max", min: "3", max: "x", wantErrMsg: "max_zoom must be an integer"}, |
| 85 | + {name: "min greater than max", min: "9", max: "8", wantErrMsg: "zoom must be 0..24 and min<=max"}, |
| 86 | + {name: "out of range", min: "0", max: "25", wantErrMsg: "zoom must be 0..24 and min<=max"}, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(tt.name, func(t *testing.T) { |
| 91 | + gotMin, gotMax, err := parseZooms(tt.min, tt.max) |
| 92 | + if tt.wantErrMsg != "" { |
| 93 | + if err == nil { |
| 94 | + t.Fatalf("parseZooms(%q, %q) expected error %q", tt.min, tt.max, tt.wantErrMsg) |
| 95 | + } |
| 96 | + if err.Error() != tt.wantErrMsg { |
| 97 | + t.Fatalf("error = %q, want %q", err.Error(), tt.wantErrMsg) |
| 98 | + } |
| 99 | + return |
| 100 | + } |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("parseZooms(%q, %q) unexpected error: %v", tt.min, tt.max, err) |
| 103 | + } |
| 104 | + if gotMin != tt.wantMin || gotMax != tt.wantMax { |
| 105 | + t.Fatalf("got (%d, %d), want (%d, %d)", gotMin, gotMax, tt.wantMin, tt.wantMax) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestBearerToken(t *testing.T) { |
| 112 | + tests := []struct { |
| 113 | + name string |
| 114 | + header string |
| 115 | + wantToken string |
| 116 | + wantOK bool |
| 117 | + }{ |
| 118 | + {name: "valid", header: "Bearer abc123", wantToken: "abc123", wantOK: true}, |
| 119 | + {name: "valid with extra spaces", header: "Bearer abc123 ", wantToken: "abc123", wantOK: true}, |
| 120 | + {name: "wrong prefix", header: "Token abc123", wantToken: "", wantOK: false}, |
| 121 | + {name: "missing token", header: "Bearer ", wantToken: "", wantOK: false}, |
| 122 | + {name: "empty", header: "", wantToken: "", wantOK: false}, |
| 123 | + } |
| 124 | + |
| 125 | + for _, tt := range tests { |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + gotToken, gotOK := bearerToken(tt.header) |
| 128 | + if gotOK != tt.wantOK || gotToken != tt.wantToken { |
| 129 | + t.Fatalf("bearerToken(%q) = (%q, %v), want (%q, %v)", tt.header, gotToken, gotOK, tt.wantToken, tt.wantOK) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestRoutes_Healthz(t *testing.T) { |
| 136 | + h := &Handler{} |
| 137 | + rr := httptest.NewRecorder() |
| 138 | + req := httptest.NewRequest(http.MethodGet, "/healthz", nil) |
| 139 | + |
| 140 | + h.Routes().ServeHTTP(rr, req) |
| 141 | + |
| 142 | + if rr.Code != http.StatusOK { |
| 143 | + t.Fatalf("status = %d, want %d", rr.Code, http.StatusOK) |
| 144 | + } |
| 145 | + if strings.TrimSpace(rr.Body.String()) != "ok" { |
| 146 | + t.Fatalf("body = %q, want %q", rr.Body.String(), "ok") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func TestPostTilepack_EarlyValidationPaths(t *testing.T) { |
| 151 | + tests := []struct { |
| 152 | + name string |
| 153 | + url string |
| 154 | + wantStatus int |
| 155 | + wantMsg string |
| 156 | + }{ |
| 157 | + { |
| 158 | + name: "invalid stac id", |
| 159 | + url: "/tilepacks/bad$id?format=pmtiles", |
| 160 | + wantStatus: http.StatusBadRequest, |
| 161 | + wantMsg: "invalid stac id", |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "invalid format", |
| 165 | + url: "/tilepacks/valid_id-123?format=zip", |
| 166 | + wantStatus: http.StatusBadRequest, |
| 167 | + wantMsg: "format must be pmtiles or mbtiles", |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "missing max zoom", |
| 171 | + url: "/tilepacks/valid_id-123?format=pmtiles&min_zoom=1", |
| 172 | + wantStatus: http.StatusBadRequest, |
| 173 | + wantMsg: "min_zoom and max_zoom must both be set", |
| 174 | + }, |
| 175 | + } |
| 176 | + |
| 177 | + for _, tt := range tests { |
| 178 | + t.Run(tt.name, func(t *testing.T) { |
| 179 | + h := &Handler{} |
| 180 | + rr := httptest.NewRecorder() |
| 181 | + req := httptest.NewRequest(http.MethodPost, tt.url, nil) |
| 182 | + |
| 183 | + h.Routes().ServeHTTP(rr, req) |
| 184 | + |
| 185 | + if rr.Code != tt.wantStatus { |
| 186 | + t.Fatalf("status = %d, want %d", rr.Code, tt.wantStatus) |
| 187 | + } |
| 188 | + var resp response |
| 189 | + if err := json.NewDecoder(rr.Body).Decode(&resp); err != nil { |
| 190 | + t.Fatalf("decode response: %v", err) |
| 191 | + } |
| 192 | + if resp.Message != tt.wantMsg { |
| 193 | + t.Fatalf("message = %q, want %q", resp.Message, tt.wantMsg) |
| 194 | + } |
| 195 | + }) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func TestClientIP(t *testing.T) { |
| 200 | + tests := []struct { |
| 201 | + name string |
| 202 | + xff string |
| 203 | + remoteAddr string |
| 204 | + want string |
| 205 | + }{ |
| 206 | + { |
| 207 | + name: "uses first x-forwarded-for entry", |
| 208 | + xff: "203.0.113.9, 10.0.0.1", |
| 209 | + remoteAddr: "192.0.2.1:54321", |
| 210 | + want: "203.0.113.9", |
| 211 | + }, |
| 212 | + { |
| 213 | + name: "uses single x-forwarded-for value with spaces", |
| 214 | + xff: " 203.0.113.11 ", |
| 215 | + remoteAddr: "192.0.2.1:54321", |
| 216 | + want: "203.0.113.11", |
| 217 | + }, |
| 218 | + { |
| 219 | + name: "falls back to remote host", |
| 220 | + xff: "", |
| 221 | + remoteAddr: "192.0.2.77:8080", |
| 222 | + want: "192.0.2.77", |
| 223 | + }, |
| 224 | + { |
| 225 | + name: "returns raw remote addr on split failure", |
| 226 | + xff: "", |
| 227 | + remoteAddr: "not-a-host-port", |
| 228 | + want: "not-a-host-port", |
| 229 | + }, |
| 230 | + } |
| 231 | + |
| 232 | + for _, tt := range tests { |
| 233 | + t.Run(tt.name, func(t *testing.T) { |
| 234 | + req := httptest.NewRequest(http.MethodGet, "/", nil) |
| 235 | + req.Header.Set("X-Forwarded-For", tt.xff) |
| 236 | + req.RemoteAddr = tt.remoteAddr |
| 237 | + got := clientIP(req) |
| 238 | + if got != tt.want { |
| 239 | + t.Fatalf("clientIP() = %q, want %q", got, tt.want) |
| 240 | + } |
| 241 | + }) |
| 242 | + } |
| 243 | +} |
0 commit comments