MCPcopy
hub / github.com/fabiolb/fabio / testProxyLogOutput

Function testProxyLogOutput

proxy/http_integration_test.go:445–550  ·  view source on GitHub ↗
(t *testing.T, bodySize int, cfg config.Proxy)

Source from the content-addressed store, hash-verified

443}
444
445func testProxyLogOutput(t *testing.T, bodySize int, cfg config.Proxy) {
446 t.Helper()
447
448 // build a format string from all log fields and one header field
449 fields := []string{"header.X-Foo:$header.X-Foo"}
450 for _, k := range logger.Fields {
451 fields = append(fields, k[1:]+":"+k)
452 }
453 format := strings.Join(fields, ";")
454
455 // create a logger
456 var b bytes.Buffer
457 l, err := logger.New(&b, format)
458 if err != nil {
459 t.Fatal("logger.New: ", err)
460 }
461
462 // create an upstream server
463 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
464 fmt.Fprint(w, "foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo")
465 }))
466 defer server.Close()
467
468 // create a proxy handler with mocked time
469 tm := time.Date(2016, 1, 1, 0, 0, 0, 12345678, time.UTC)
470 proxyHandler := &HTTPProxy{
471 Config: cfg,
472 Time: func() time.Time {
473 defer func() { tm = tm.Add(1111111111 * time.Nanosecond) }()
474 return tm
475 },
476 Transport: http.DefaultTransport,
477 Lookup: func(r *http.Request) *route.Target {
478 return &route.Target{
479 Service: "svc-a",
480 URL: mustParse(server.URL),
481 }
482 },
483 Logger: l,
484 }
485
486 // start an http server with the proxy handler
487 // which captures some parameters from the request
488 var remoteAddr string
489 proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
490 remoteAddr = r.RemoteAddr
491 proxyHandler.ServeHTTP(w, r)
492 }))
493 defer proxy.Close()
494
495 // create the request
496 req, _ := http.NewRequest("GET", proxy.URL+"/foo?x=y", nil)
497 req.Host = "example.com"
498 req.Header.Set("X-Foo", "bar")
499
500 // execute the request
501 resp, _ := mustDo(req)
502 if resp.StatusCode != http.StatusOK {

Callers 1

TestProxyLogOutputFunction · 0.85

Calls 9

CloseMethod · 0.95
ServeHTTPMethod · 0.95
NewFunction · 0.92
mustDoFunction · 0.85
FatalMethod · 0.80
mustParseFunction · 0.70
AddMethod · 0.45
SetMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected