Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package fileserver |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "net/http" |
| 20 | "net/http/httptest" |
| 21 | "os" |
| 22 | "path/filepath" |
| 23 | "strings" |
| 24 | "testing" |
| 25 | "time" |
| 26 | |
| 27 | "github.com/caddyserver/caddy/v2" |
| 28 | "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 29 | ) |
| 30 | |
| 31 | func TestBrowsePlainTimeFormat(t *testing.T) { |
| 32 | root := t.TempDir() |
| 33 | filename := filepath.Join(root, "example.txt") |
| 34 | if err := os.WriteFile(filename, nil, 0o600); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | modTime := time.Date(2024, time.January, 2, 3, 4, 5, 0, time.UTC) |
| 38 | if err := os.Chtimes(filename, modTime, modTime); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | |
| 42 | for _, tc := range []struct { |
| 43 | name string |
| 44 | layout string |
| 45 | wantModTime string |
| 46 | }{ |
| 47 | { |
| 48 | name: "default", |
| 49 | wantModTime: "January 2, 2024 at 03:04:05", |
| 50 | }, |
| 51 | { |
| 52 | name: "custom", |
| 53 | layout: "2006-01-02T15:04:05Z07:00", |
| 54 | wantModTime: "2024-01-02T03:04:05Z", |
| 55 | }, |
| 56 | } { |
| 57 | t.Run(tc.name, func(t *testing.T) { |
| 58 | fsrv := FileServer{ |
| 59 | Root: root, |
| 60 | Browse: &Browse{ |
| 61 | PlainTimeFormat: tc.layout, |
| 62 | }, |
| 63 | } |
| 64 | ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 65 | if err := fsrv.Provision(ctx); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | |
| 69 | req := httptest.NewRequest(http.MethodGet, "http://example.com/", nil) |
| 70 | req.Header.Set("Accept", "text/plain") |
| 71 | originalReq := *req |
| 72 | reqCtx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, caddy.NewReplacer()) |
| 73 | reqCtx = context.WithValue(reqCtx, caddyhttp.OriginalRequestCtxKey, originalReq) |
| 74 | req = req.WithContext(reqCtx) |
| 75 | |
| 76 | w := httptest.NewRecorder() |
| 77 | if err := fsrv.ServeHTTP(w, req, nil); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | if got := w.Header().Get("Content-Type"); got != "text/plain; charset=utf-8" { |
| 81 | t.Fatalf("Content-Type = %q, want %q", got, "text/plain; charset=utf-8") |
| 82 | } |
| 83 | if !strings.Contains(w.Body.String(), tc.wantModTime) { |
| 84 | t.Fatalf("response body does not contain modification time %q:\n%s", tc.wantModTime, w.Body.String()) |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | } |