MCPcopy
hub / github.com/caddyserver/caddy / initServer

Method initServer

caddytest/caddytest.go:131–210  ·  caddytest/caddytest.go::Tester.initServer

InitServer this will configure the server with a configurion of a specific type. The configType must be either "json" or the adapter type.

(rawConfig string, configType string)

Source from the content-addressed store, hash-verified

129// InitServer this will configure the server with a configurion of a specific
130// type. The configType must be either "json" or the adapter type.
131func (tc *Tester) initServer(rawConfig string, configType string) error {
132 if testing.Short() {
133 tc.t.SkipNow()
134 return nil
135 }
136
137 err := validateTestPrerequisites(tc)
138 if err != nil {
139 tc.t.Skipf("skipping tests as failed integration prerequisites. %s", err)
140 return nil
141 }
142
143 tc.t.Cleanup(func() {
144 if tc.t.Failed() && tc.configLoaded {
145 res, err := http.Get(fmt.Sprintf("http://localhost:%d/config/", tc.config.AdminPort))
146 if err != nil {
147 tc.t.Log("unable to read the current config")
148 return
149 }
150 defer res.Body.Close()
151 body, _ := io.ReadAll(res.Body)
152
153 var out bytes.Buffer
154 _ = json.Indent(&out, body, "", " ")
155 tc.t.Logf("----------- failed with config -----------\n%s", out.String())
156 }
157 })
158
159 rawConfig = prependCaddyFilePath(rawConfig)
160 // normalize JSON config
161 if configType == "json" {
162 tc.t.Logf("Before: %s", rawConfig)
163 var conf any
164 if err := json.Unmarshal([]byte(rawConfig), &conf); err != nil {
165 return err
166 }
167 c, err := json.Marshal(conf)
168 if err != nil {
169 return err
170 }
171 rawConfig = string(c)
172 tc.t.Logf("After: %s", rawConfig)
173 }
174 client := &http.Client{
175 Timeout: tc.config.LoadRequestTimeout,
176 }
177 start := time.Now()
178 req, err := http.NewRequest("POST", fmt.Sprintf("http://localhost:%d/load", tc.config.AdminPort), strings.NewReader(rawConfig))
179 if err != nil {
180 tc.t.Errorf("failed to create request. %s", err)
181 return err
182 }
183
184 if configType == "json" {
185 req.Header.Add("Content-Type", "application/json")
186 } else {
187 req.Header.Add("Content-Type", "text/"+configType)
188 }

Callers 2

InitServerMethod · 0.95
AssertLoadErrorFunction · 0.95

Calls 9

prependCaddyFilePathFunction · 0.85
timeElapsedFunction · 0.85
DoMethod · 0.80
CleanupMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.45
StringMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected