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, 22:30 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 caddy |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "encoding/json" |
| 20 | "io" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | func TestContextWithValuePreservesMetricsRegistry(t *testing.T) { |
| 25 | ctx, cancel := NewContext(Context{Context: context.Background()}) |
| 26 | t.Cleanup(cancel) |
| 27 | |
| 28 | reg := ctx.GetMetricsRegistry() |
| 29 | if reg == nil { |
| 30 | t.Fatal("expected a metrics registry on the base context") |
| 31 | } |
| 32 | |
| 33 | type testKey struct{} |
| 34 | derived := ctx.WithValue(testKey{}, "value") |
| 35 | if got := derived.GetMetricsRegistry(); got != reg { |
| 36 | t.Fatalf("WithValue must preserve the metrics registry: got %p, want %p", got, reg) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func ExampleContext_LoadModule() { |
| 41 | // this whole first part is just setting up for the example; |
| 42 | // note the struct tags - very important; we specify inline_key |
| 43 | // because that is the only way to know the module name |
| 44 | var ctx Context |
| 45 | myStruct := &struct { |
| 46 | // This godoc comment will appear in module documentation. |
| 47 | GuestModuleRaw json.RawMessage `json:"guest_module,omitempty" caddy:"namespace=example inline_key=name"` |
| 48 | |
| 49 | // this is where the decoded module will be stored; in this |
| 50 | // example, we pretend we need an io.Writer but it can be |
| 51 | // any interface type that is useful to you |
| 52 | guestModule io.Writer |
| 53 | }{ |
| 54 | GuestModuleRaw: json.RawMessage(`{"name":"module_name","foo":"bar"}`), |
| 55 | } |
| 56 | |
| 57 | // if a guest module is provided, we can load it easily |
| 58 | if myStruct.GuestModuleRaw != nil { |
| 59 | mod, err := ctx.LoadModule(myStruct, "GuestModuleRaw") |
| 60 | if err != nil { |
| 61 | // you'd want to actually handle the error here |
| 62 | // return fmt.Errorf("loading guest module: %v", err) |
| 63 | } |
| 64 | // mod contains the loaded and provisioned module, |
| 65 | // it is now ready for us to use |
| 66 | myStruct.guestModule = mod.(io.Writer) |
| 67 | } |
| 68 | |
| 69 | // use myStruct.guestModule from now on |
| 70 | } |
| 71 | |
| 72 | func ExampleContext_LoadModule_array() { |
| 73 | // this whole first part is just setting up for the example; |
| 74 | // note the struct tags - very important; we specify inline_key |
| 75 | // because that is the only way to know the module name |
| 76 | var ctx Context |
| 77 | myStruct := &struct { |
| 78 | // This godoc comment will appear in module documentation. |
| 79 | GuestModulesRaw []json.RawMessage `json:"guest_modules,omitempty" caddy:"namespace=example inline_key=name"` |
| 80 | |
| 81 | // this is where the decoded module will be stored; in this |
| 82 | // example, we pretend we need an io.Writer but it can be |
| 83 | // any interface type that is useful to you |
| 84 | guestModules []io.Writer |
| 85 | }{ |
| 86 | GuestModulesRaw: []json.RawMessage{ |
| 87 | json.RawMessage(`{"name":"module1_name","foo":"bar1"}`), |
| 88 | json.RawMessage(`{"name":"module2_name","foo":"bar2"}`), |
| 89 | }, |
| 90 | } |
| 91 | |
| 92 | // since our input is []json.RawMessage, the output will be []any |
| 93 | mods, err := ctx.LoadModule(myStruct, "GuestModulesRaw") |
| 94 | if err != nil { |
| 95 | // you'd want to actually handle the error here |
| 96 | // return fmt.Errorf("loading guest modules: %v", err) |
| 97 | } |
| 98 | for _, mod := range mods.([]any) { |
| 99 | myStruct.guestModules = append(myStruct.guestModules, mod.(io.Writer)) |
| 100 | } |
| 101 | |
| 102 | // use myStruct.guestModules from now on |
| 103 | } |
| 104 | |
| 105 | func ExampleContext_LoadModule_map() { |
| 106 | // this whole first part is just setting up for the example; |
| 107 | // note the struct tags - very important; we don't specify |
| 108 | // inline_key because the map key is the module name |
| 109 | var ctx Context |
| 110 | myStruct := &struct { |
| 111 | // This godoc comment will appear in module documentation. |
| 112 | GuestModulesRaw ModuleMap `json:"guest_modules,omitempty" caddy:"namespace=example"` |
| 113 | |
| 114 | // this is where the decoded module will be stored; in this |
| 115 | // example, we pretend we need an io.Writer but it can be |
| 116 | // any interface type that is useful to you |
| 117 | guestModules map[string]io.Writer |
| 118 | }{ |
| 119 | GuestModulesRaw: ModuleMap{ |
| 120 | "module1_name": json.RawMessage(`{"foo":"bar1"}`), |
| 121 | "module2_name": json.RawMessage(`{"foo":"bar2"}`), |
| 122 | }, |
| 123 | } |
| 124 | |
| 125 | // since our input is map[string]json.RawMessage, the output will be map[string]any |
| 126 | mods, err := ctx.LoadModule(myStruct, "GuestModulesRaw") |
| 127 | if err != nil { |
| 128 | // you'd want to actually handle the error here |
| 129 | // return fmt.Errorf("loading guest modules: %v", err) |
| 130 | } |
| 131 | for modName, mod := range mods.(map[string]any) { |
| 132 | myStruct.guestModules[modName] = mod.(io.Writer) |
| 133 | } |
| 134 | |
| 135 | // use myStruct.guestModules from now on |
| 136 | } |