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

Method ensureConfigRunning

caddytest/caddytest.go:212–258  ·  view source on GitHub ↗
(rawConfig string, configType string)

Source from the content-addressed store, hash-verified

210}
211
212func (tc *Tester) ensureConfigRunning(rawConfig string, configType string) error {
213 expectedBytes := []byte(prependCaddyFilePath(rawConfig))
214 if configType != "json" {
215 adapter := caddyconfig.GetAdapter(configType)
216 if adapter == nil {
217 return fmt.Errorf("adapter of config type is missing: %s", configType)
218 }
219 expectedBytes, _, _ = adapter.Adapt([]byte(rawConfig), nil)
220 }
221
222 var expected any
223 err := json.Unmarshal(expectedBytes, &expected)
224 if err != nil {
225 return err
226 }
227
228 client := &http.Client{
229 Timeout: tc.config.LoadRequestTimeout,
230 }
231
232 fetchConfig := func(client *http.Client) any {
233 resp, err := client.Get(fmt.Sprintf("http://localhost:%d/config/", tc.config.AdminPort))
234 if err != nil {
235 return nil
236 }
237 defer resp.Body.Close()
238 actualBytes, err := io.ReadAll(resp.Body)
239 if err != nil {
240 return nil
241 }
242 var actual any
243 err = json.Unmarshal(actualBytes, &actual)
244 if err != nil {
245 return nil
246 }
247 return actual
248 }
249
250 for retries := 10; retries > 0; retries-- {
251 if reflect.DeepEqual(expected, fetchConfig(client)) {
252 return nil
253 }
254 time.Sleep(1 * time.Second)
255 }
256 tc.t.Errorf("POSTed configuration isn't active")
257 return errors.New("EnsureConfigRunning: POSTed configuration isn't active")
258}
259
260const initConfig = `{
261 admin localhost:%d

Callers 1

InitServerMethod · 0.95

Calls 5

GetAdapterFunction · 0.92
prependCaddyFilePathFunction · 0.85
AdaptMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected