Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 10, 23:45 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 caddycmd |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | // TestSplitModule_StrictGoSpec is the regression test for splitModule |
| 23 | // silently accepting inputs that violate go.dev/ref/mod#go-mod-file-ident |
| 24 | // (module paths cannot contain '@'). It also checks the happy paths and |
| 25 | // the empty-version edge case. |
| 26 | func TestSplitModule_StrictGoSpec(t *testing.T) { |
| 27 | for _, tt := range []struct { |
| 28 | name string |
| 29 | input string |
| 30 | module string |
| 31 | version string |
| 32 | wantErr bool |
| 33 | wantErrSub string |
| 34 | }{ |
| 35 | { |
| 36 | name: "no version", |
| 37 | input: "github.com/caddyserver/caddy", |
| 38 | module: "github.com/caddyserver/caddy", |
| 39 | version: "", |
| 40 | }, |
| 41 | { |
| 42 | name: "with version", |
| 43 | input: "github.com/caddyserver/caddy@v2.0.0", |
| 44 | module: "github.com/caddyserver/caddy", |
| 45 | version: "v2.0.0", |
| 46 | }, |
| 47 | { |
| 48 | name: "multiple '@' rejected", |
| 49 | input: "github.com/@user/module@v1.0.0", |
| 50 | wantErr: true, |
| 51 | wantErrSub: "module path must not contain '@'", |
| 52 | }, |
| 53 | { |
| 54 | name: "trailing '@' with empty version rejected", |
| 55 | input: "github.com/user/module@", |
| 56 | wantErr: true, |
| 57 | wantErrSub: "version is required after '@'", |
| 58 | }, |
| 59 | { |
| 60 | name: "empty input rejected", |
| 61 | input: "", |
| 62 | wantErr: true, |
| 63 | wantErrSub: "module name is required", |
| 64 | }, |
| 65 | } { |
| 66 | t.Run(tt.name, func(t *testing.T) { |
| 67 | module, version, err := splitModule(tt.input) |
| 68 | |
| 69 | if tt.wantErr { |
| 70 | if err == nil { |
| 71 | t.Fatalf("expected error containing %q, got nil", tt.wantErrSub) |
| 72 | } |
| 73 | if !strings.Contains(err.Error(), tt.wantErrSub) { |
| 74 | t.Errorf("error %q does not contain %q", err.Error(), tt.wantErrSub) |
| 75 | } |
| 76 | return |
| 77 | } |
| 78 | if err != nil { |
| 79 | t.Fatalf("unexpected error: %v", err) |
| 80 | } |
| 81 | if module != tt.module { |
| 82 | t.Errorf("module: got %q, want %q", module, tt.module) |
| 83 | } |
| 84 | if version != tt.version { |
| 85 | t.Errorf("version: got %q, want %q", version, tt.version) |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | } |