MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestStreamableGET

Function TestStreamableGET

mcp/streamable_test.go:1830–1916  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1828}
1829
1830func TestStreamableGET(t *testing.T) {
1831 // This test checks the fix for problematic behavior described in #410:
1832 // Hanging GET headers should be written immediately, even if there are no
1833 // messages.
1834 server := NewServer(testImpl, nil)
1835
1836 handler := NewStreamableHTTPHandler(func(req *http.Request) *Server { return server }, nil)
1837 httpServer := httptest.NewServer(mustNotPanic(t, handler))
1838 defer httpServer.Close()
1839
1840 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
1841 defer cancel()
1842
1843 newReq := func(method string, msg jsonrpc.Message) *http.Request {
1844 var body io.Reader
1845 if msg != nil {
1846 data, err := jsonrpc2.EncodeMessage(msg)
1847 if err != nil {
1848 t.Fatal(err)
1849 }
1850 body = bytes.NewReader(data)
1851 }
1852 req, err := http.NewRequestWithContext(ctx, method, httpServer.URL, body)
1853 if err != nil {
1854 t.Fatal(err)
1855 }
1856 req.Header.Set("Accept", "application/json, text/event-stream")
1857 if msg != nil {
1858 req.Header.Set("Content-Type", "application/json")
1859 }
1860 return req
1861 }
1862
1863 get1 := newReq(http.MethodGet, nil)
1864 resp, err := http.DefaultClient.Do(get1)
1865 if err != nil {
1866 t.Fatal(err)
1867 }
1868 // GET without session should return 400 Bad Request (not 405) because
1869 // GET is a valid method - it just requires a session ID.
1870 if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
1871 t.Errorf("initial GET: got status %d, want %d", got, want)
1872 }
1873 defer resp.Body.Close()
1874
1875 post1 := newReq(http.MethodPost, req(1, methodInitialize, &InitializeParams{}))
1876 resp, err = http.DefaultClient.Do(post1)
1877 if err != nil {
1878 t.Fatal(err)
1879 }
1880 defer resp.Body.Close()
1881 if got, want := resp.StatusCode, http.StatusOK; got != want {
1882 body, err := io.ReadAll(resp.Body)
1883 if err != nil {
1884 t.Fatal(err)
1885 }
1886 t.Errorf("initialize POST: got status %d, want %d; body:\n%s", got, want, string(body))
1887 }

Callers

nothing calls this directly

Calls 7

EncodeMessageFunction · 0.92
NewServerFunction · 0.85
NewStreamableHTTPHandlerFunction · 0.85
mustNotPanicFunction · 0.85
reqFunction · 0.85
LogMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…