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

Function TestServerHTTP10ConnectionKeepAlive

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

Source from the content-addressed store, hash-verified

1752}
1753
1754func TestServerHTTP10ConnectionKeepAlive(t *testing.T) {
1755 t.Parallel()
1756
1757 ln := fasthttputil.NewInmemoryListener()
1758
1759 ch := make(chan struct{})
1760 go func() {
1761 err := Serve(ln, func(ctx *RequestCtx) {
1762 if string(ctx.Path()) == "/close" {
1763 ctx.SetConnectionClose()
1764 }
1765 })
1766 if err != nil {
1767 t.Errorf("unexpected error: %v", err)
1768 }
1769 close(ch)
1770 }()
1771
1772 conn, err := ln.Dial()
1773 if err != nil {
1774 t.Fatalf("unexpected error: %v", err)
1775 }
1776 _, err = fmt.Fprintf(conn, "%s", "GET / HTTP/1.0\r\nHost: aaa\r\nConnection: keep-alive\r\n\r\n")
1777 if err != nil {
1778 t.Fatalf("error when writing request: %v", err)
1779 }
1780 _, err = fmt.Fprintf(conn, "%s", "GET /close HTTP/1.0\r\nHost: aaa\r\nConnection: keep-alive\r\n\r\n")
1781 if err != nil {
1782 t.Fatalf("error when writing request: %v", err)
1783 }
1784
1785 br := bufio.NewReader(conn)
1786 var resp Response
1787 if err = resp.Read(br); err != nil {
1788 t.Fatalf("error when reading response: %v", err)
1789 }
1790 if resp.ConnectionClose() {
1791 t.Fatal("response mustn't have 'Connection: close' header")
1792 }
1793 if err = resp.Read(br); err != nil {
1794 t.Fatalf("error when reading response: %v", err)
1795 }
1796 if !resp.ConnectionClose() {
1797 t.Fatal("response must have 'Connection: close' header")
1798 }
1799
1800 tailCh := make(chan struct{})
1801 go func() {
1802 tail, err := io.ReadAll(br)
1803 if err != nil {
1804 t.Errorf("error when reading tail: %v", err)
1805 }
1806 if len(tail) > 0 {
1807 t.Errorf("unexpected non-zero tail %q", tail)
1808 }
1809 close(tailCh)
1810 }()
1811

Callers

nothing calls this directly

Calls 10

DialMethod · 0.95
ReadMethod · 0.95
ConnectionCloseMethod · 0.95
CloseMethod · 0.95
NewInmemoryListenerFunction · 0.92
ServeFunction · 0.85
NewReaderMethod · 0.80
CloseMethod · 0.65
PathMethod · 0.45
SetConnectionCloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…