MCPcopy
hub / github.com/valyala/fasthttp / TestServerResponseServerHeader

Function TestServerResponseServerHeader

server_test.go:608–683  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

606}
607
608func TestServerResponseServerHeader(t *testing.T) {
609 t.Parallel()
610
611 serverName := "foobar serv"
612
613 s := &Server{
614 Handler: func(ctx *RequestCtx) {
615 name := ctx.Response.Header.Server()
616 if string(name) != serverName {
617 fmt.Fprintf(ctx, "unexpected server name: %q. Expecting %q", name, serverName)
618 } else {
619 ctx.WriteString("OK") //nolint:errcheck
620 }
621
622 // make sure the server name is sent to the client after ctx.Response.Reset()
623 ctx.NotFound()
624 },
625 Name: serverName,
626 }
627
628 ln := fasthttputil.NewInmemoryListener()
629
630 serverCh := make(chan struct{})
631 go func() {
632 if err := s.Serve(ln); err != nil {
633 t.Errorf("unexpected error: %v", err)
634 }
635 close(serverCh)
636 }()
637
638 clientCh := make(chan struct{})
639 go func() {
640 c, err := ln.Dial()
641 if err != nil {
642 t.Errorf("unexpected error: %v", err)
643 }
644 if _, err = c.Write([]byte("GET / HTTP/1.1\r\nHost: aa\r\n\r\n")); err != nil {
645 t.Errorf("unexpected error: %v", err)
646 }
647 br := bufio.NewReader(c)
648 var resp Response
649 if err = resp.Read(br); err != nil {
650 t.Errorf("unexpected error: %v", err)
651 }
652
653 if resp.StatusCode() != StatusNotFound {
654 t.Errorf("unexpected status code: %d. Expecting %d", resp.StatusCode(), StatusNotFound)
655 }
656 if string(resp.Body()) != "404 Page not found" {
657 t.Errorf("unexpected body: %q. Expecting %q", resp.Body(), "404 Page not found")
658 }
659 if string(resp.Header.Server()) != serverName {
660 t.Errorf("unexpected server header: %q. Expecting %q", resp.Header.Server(), serverName)
661 }
662 if err = c.Close(); err != nil {
663 t.Errorf("unexpected error: %v", err)
664 }
665 close(clientCh)

Callers

nothing calls this directly

Calls 13

ServeMethod · 0.95
DialMethod · 0.95
ReadMethod · 0.95
StatusCodeMethod · 0.95
BodyMethod · 0.95
CloseMethod · 0.95
NewInmemoryListenerFunction · 0.92
ServerMethod · 0.80
NotFoundMethod · 0.80
NewReaderMethod · 0.80
WriteMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…