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

Function TestOpenStream

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

Source from the content-addressed store, hash-verified

429}
430
431func TestOpenStream(t *testing.T) {
432 streamResponse := func(contentType string) httpmock.Responder {
433 return func(req *http.Request) (*http.Response, error) {
434 assert.Equal(t, "text/event-stream", req.Header.Get("Accept"))
435 resp, err := httpmock.StringResponse(": heartbeat\n\n")(req)
436 resp.Header.Set("Content-Type", contentType)
437 return resp, err
438 }
439 }
440
441 t.Run("returns the stream body", func(t *testing.T) {
442 client, reg := newTestClient(t, "github.com")
443 reg.Register(httpmock.REST("GET", "event-subscriptions/sub_1/events"),
444 streamResponse("text/event-stream; charset=utf-8"))
445
446 body, err := client.OpenStream(context.Background(), "sub_1")
447
448 require.NoError(t, err)
449 defer body.Close()
450 got, err := io.ReadAll(body)
451 require.NoError(t, err)
452 assert.Equal(t, ": heartbeat\n\n", string(got))
453 })
454
455 t.Run("rejects a response that is not an event stream", func(t *testing.T) {
456 client, reg := newTestClient(t, "github.com")
457 reg.Register(httpmock.REST("GET", "event-subscriptions/sub_1/events"), streamResponse("application/json"))
458
459 _, err := client.OpenStream(context.Background(), "sub_1")
460
461 require.ErrorContains(t, err, `unexpected event stream content type "application/json"`)
462 })
463}
464
465func TestClientUnsupportedHost(t *testing.T) {
466 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