MCPcopy Create free account
hub / github.com/cli/cli / TestOpenStream

Function TestOpenStream

pkg/cmd/event/client/client_test.go:395–427  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

393}
394
395func TestOpenStream(t *testing.T) {
396 streamResponse := func(contentType string) httpmock.Responder {
397 return func(req *http.Request) (*http.Response, error) {
398 assert.Equal(t, "text/event-stream", req.Header.Get("Accept"))
399 resp, err := httpmock.StringResponse(": heartbeat\n\n")(req)
400 resp.Header.Set("Content-Type", contentType)
401 return resp, err
402 }
403 }
404
405 t.Run("returns the stream body", func(t *testing.T) {
406 client, reg := newTestClient(t, "github.com")
407 reg.Register(httpmock.REST("GET", "event-subscriptions/sub_1/events"),
408 streamResponse("text/event-stream; charset=utf-8"))
409
410 body, err := client.OpenStream(context.Background(), "sub_1")
411
412 require.NoError(t, err)
413 defer body.Close()
414 got, err := io.ReadAll(body)
415 require.NoError(t, err)
416 assert.Equal(t, ": heartbeat\n\n", string(got))
417 })
418
419 t.Run("rejects a response that is not an event stream", func(t *testing.T) {
420 client, reg := newTestClient(t, "github.com")
421 reg.Register(httpmock.REST("GET", "event-subscriptions/sub_1/events"), streamResponse("application/json"))
422
423 _, err := client.OpenStream(context.Background(), "sub_1")
424
425 require.ErrorContains(t, err, `unexpected event stream content type "application/json"`)
426 })
427}
428
429func TestClientUnsupportedHost(t *testing.T) {
430 for _, host := range []string{"ghe.example.com", "example.ghe.com"} {

Callers

nothing calls this directly

Calls 10

StringResponseFunction · 0.92
RESTFunction · 0.92
EqualMethod · 0.80
newTestClientFunction · 0.70
GetMethod · 0.65
SetMethod · 0.65
RunMethod · 0.65
OpenStreamMethod · 0.65
CloseMethod · 0.65
RegisterMethod · 0.45

Tested by

no test coverage detected