MCPcopy
hub / github.com/connectrpc/connect-go / TestHTTPCallGetBody

Function TestHTTPCallGetBody

duplex_http_call_test.go:34–133  ·  view source on GitHub ↗

TestHTTPCallGetBody tests that the client is able to retry requests on connection close errors. It will initialize a closing handler and ensure http.Request.GetBody is successfully called to replay the request.

(t *testing.T)

Source from the content-addressed store, hash-verified

32// connection close errors. It will initialize a closing handler and ensure
33// http.Request.GetBody is successfully called to replay the request.
34func TestHTTPCallGetBody(t *testing.T) {
35 t.Parallel()
36 handler := http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
37 // The "Connection: close" header is turned into a GOAWAY frame by the http2 server.
38 responseWriter.Header().Add("Connection", "close")
39 _, _ = io.Copy(responseWriter, request.Body)
40 _ = request.Body.Close()
41 })
42 // Must use httptest for this test.
43 server := httptest.NewUnstartedServer(handler)
44 svrProtos := new(http.Protocols)
45 svrProtos.SetHTTP1(true)
46 svrProtos.SetUnencryptedHTTP2(true)
47 server.Config.Protocols = svrProtos
48 server.Start()
49 t.Cleanup(server.Close)
50
51 clientProtos := new(http.Protocols)
52 clientProtos.SetUnencryptedHTTP2(true)
53 client := server.Client()
54 transport, ok := client.Transport.(*http.Transport)
55 assert.True(t, ok)
56 transport.Protocols = clientProtos
57
58 bufferPool := newBufferPool()
59 serverURL, _ := url.Parse(server.URL)
60 errGetBodyCalled := errors.New("getBodyCalled") // sentinel error
61 caller := func(size int) error {
62 call := newDuplexHTTPCall(
63 t.Context(),
64 client,
65 serverURL,
66 Spec{StreamType: StreamTypeUnary},
67 http.Header{},
68 )
69 getBodyCalled := false
70 call.onRequestSend = func(*http.Request) {
71 getBody := call.request.GetBody
72 call.request.GetBody = func() (io.ReadCloser, error) {
73 getBodyCalled = true
74 rdcloser, err := getBody()
75 assert.Nil(t, err)
76 return rdcloser, err
77 }
78 }
79 // SetValidateResponse must be set.
80 call.SetValidateResponse(func(*http.Response) *Error {
81 return nil
82 })
83 buf := bufferPool.Get()
84 defer bufferPool.Put(buf)
85 buf.Write(make([]byte, size))
86 _, err := call.Send(bytes.NewReader(buf.Bytes()))
87 assert.Nil(t, err)
88 assert.Nil(t, call.CloseWrite())
89 buf.Reset()
90 _, err = io.Copy(buf, call)
91 assert.Nil(t, err)

Callers

nothing calls this directly

Calls 15

TrueFunction · 0.92
NilFunction · 0.92
EqualFunction · 0.92
newBufferPoolFunction · 0.85
newDuplexHTTPCallFunction · 0.85
CleanupMethod · 0.80
ClientMethod · 0.80
SetValidateResponseMethod · 0.80
PutMethod · 0.80
BytesMethod · 0.80
CloseWriteMethod · 0.80
WaitMethod · 0.80

Tested by

no test coverage detected