(t *testing.T)
| 23 | ) |
| 24 | |
| 25 | func TestCoder(t *testing.T) { |
| 26 | t.Parallel() |
| 27 | |
| 28 | t.Run("V1/OK", func(t *testing.T) { |
| 29 | t.Parallel() |
| 30 | |
| 31 | token := uuid.NewString() |
| 32 | gotLogs := make(chan struct{}) |
| 33 | var closeOnce sync.Once |
| 34 | handler := func(w http.ResponseWriter, r *http.Request) { |
| 35 | if r.URL.Path == "/api/v2/buildinfo" { |
| 36 | w.Header().Set("Content-Type", "application/json") |
| 37 | _, _ = w.Write([]byte(`{"version": "v2.8.9"}`)) |
| 38 | return |
| 39 | } |
| 40 | defer closeOnce.Do(func() { close(gotLogs) }) |
| 41 | tokHdr := r.Header.Get(codersdk.SessionTokenHeader) |
| 42 | assert.Equal(t, token, tokHdr) |
| 43 | req, ok := decodeV1Logs(t, w, r) |
| 44 | if !ok { |
| 45 | return |
| 46 | } |
| 47 | if assert.Len(t, req.Logs, 1) { |
| 48 | assert.Equal(t, "hello world", req.Logs[0].Output) |
| 49 | assert.Equal(t, codersdk.LogLevelInfo, req.Logs[0].Level) |
| 50 | } |
| 51 | } |
| 52 | srv := httptest.NewServer(http.HandlerFunc(handler)) |
| 53 | defer srv.Close() |
| 54 | |
| 55 | ctx, cancel := context.WithCancel(context.Background()) |
| 56 | defer cancel() |
| 57 | |
| 58 | logger, _ := newCoderLogger(ctx, t, srv.URL, token) |
| 59 | logger(LevelInfo, "hello %s", "world") |
| 60 | <-gotLogs |
| 61 | }) |
| 62 | |
| 63 | t.Run("V1/Close", func(t *testing.T) { |
| 64 | t.Parallel() |
| 65 | |
| 66 | var got []agentsdk.Log |
| 67 | handler := func(w http.ResponseWriter, r *http.Request) { |
| 68 | if r.URL.Path == "/api/v2/buildinfo" { |
| 69 | w.Header().Set("Content-Type", "application/json") |
| 70 | _, _ = w.Write([]byte(`{"version": "v2.8.9"}`)) |
| 71 | return |
| 72 | } |
| 73 | req, ok := decodeV1Logs(t, w, r) |
| 74 | if !ok { |
| 75 | return |
| 76 | } |
| 77 | got = append(got, req.Logs...) |
| 78 | } |
| 79 | srv := httptest.NewServer(http.HandlerFunc(handler)) |
| 80 | defer srv.Close() |
| 81 | |
| 82 | ctx, cancel := context.WithCancel(context.Background()) |
nothing calls this directly
no test coverage detected