Lu Stadler лет назад: 7
Родитель
Сommit
1739d854a0
1 измененных файлов с 0 добавлено и 24 удалено
  1. 0 24
      go/fake-http/fake-http.go

+ 0 - 24
go/fake-http/fake-http.go

243
	if err != nil {
243
	if err != nil {
244
		return nil, err
244
		return nil, err
245
	}
245
	}
246
247
	return json.MarshalIndent(val, "", "    ")
246
	return json.MarshalIndent(val, "", "    ")
248
}
247
}
249
248
256
	return nil
255
	return nil
257
}
256
}
258
257
259
func renderHTML(w http.ResponseWriter, responses []Response) error {
260
	w.Header().Set("Content-Type", "text/html")
261
	fmt.Fprintf(w, "<!doctype html><html><head><style>pre{max-width:100vw;padding:0.5em;background-color:#eee;white-space:pre-wrap;}</style></head><body><ul>\n")
262
	for _, resp := range responses {
263
		fmt.Fprintf(w, "<li><pre>%s</pre></li>\n", resp.String())
264
	}
265
	fmt.Fprintf(w, "\n</ul></body></html>")
266
	return nil
267
}
268
269
// LogEntry is a request/respond pair for logging.
258
// LogEntry is a request/respond pair for logging.
270
type LogEntry struct {
259
type LogEntry struct {
271
	Request  Request
260
	Request  Request
340
	Value string `yaml:"value"`
329
	Value string `yaml:"value"`
341
}
330
}
342
331
343
func readResponse(form url.Values) Response {
344
	r := Response{}
345
	r.Method = form.Get("method")
346
	r.Path = form.Get("path")
347
	r.Status = 200
348
	headers := make([]Header, 0)
349
	for i, name := range form["header"] {
350
		headers = append(headers, Header{Name: name, Value: form["value"][i]})
351
	}
352
	r.Body = form.Get("body")
353
	return r
354
}
355
356
// Load loads responses from the YAML file at path.
332
// Load loads responses from the YAML file at path.
357
func (rs *Responses) Load(path string) {
333
func (rs *Responses) Load(path string) {
358
	responses, err := rs.loadFile(path)
334
	responses, err := rs.loadFile(path)