(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestWaitForBrowserCallback(t *testing.T) { |
| 14 | t.Run("Handle success on callback", func(t *testing.T) { |
| 15 | // Set a timer to wait for the server to have started and then call the URL and assert. |
| 16 | time.AfterFunc(1*time.Second, func() { |
| 17 | client := &http.Client{} |
| 18 | url := "http://localhost:1234?code=1234&state=1234" |
| 19 | resp, err := client.Get(url) |
| 20 | assert.NoError(t, err) |
| 21 | |
| 22 | bytes, err := io.ReadAll(resp.Body) |
| 23 | assert.NoError(t, err) |
| 24 | body := string(bytes) |
| 25 | |
| 26 | defer func() { |
| 27 | _ = resp.Body.Close() |
| 28 | }() |
| 29 | assert.Contains(t, body, "You can close the window and go back to the CLI to see the user info and tokens.") |
| 30 | }) |
| 31 | |
| 32 | code, state, callbackErr := WaitForBrowserCallback("localhost:1234") |
| 33 | assert.NoError(t, callbackErr) |
| 34 | assert.Equal(t, "1234", code) |
| 35 | assert.Equal(t, "1234", state) |
| 36 | }) |
| 37 | |
| 38 | t.Run("Handle error on callback", func(t *testing.T) { |
| 39 | // Set a timer to wait for the server to have started and then call the URL and assert. |
| 40 | time.AfterFunc(1*time.Second, func() { |
| 41 | client := &http.Client{} |
| 42 | url := "http://localhost:1234?error=foo&error_description=bar" |
| 43 | resp, err := client.Get(url) |
| 44 | assert.NoError(t, err) |
| 45 | bytes, err := io.ReadAll(resp.Body) |
| 46 | assert.NoError(t, err) |
| 47 | body := string(bytes) |
| 48 | defer func() { |
| 49 | _ = resp.Body.Close() |
| 50 | }() |
| 51 | |
| 52 | assert.Contains(t, body, "Failed to extract code from request, please try authenticating again") |
| 53 | }) |
| 54 | |
| 55 | code, state, callbackErr := WaitForBrowserCallback("localhost:1234") |
| 56 | assert.Error(t, callbackErr) |
| 57 | assert.Equal(t, "", code) |
| 58 | assert.Equal(t, "", state) |
| 59 | }) |
| 60 | |
| 61 | t.Run("Failure to start server", func(t *testing.T) { |
| 62 | s := &http.Server{Addr: "localhost:1234"} |
| 63 | |
| 64 | go func() { |
| 65 | if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 66 | assert.NoError(t, err) |
| 67 | } |
| 68 | }() |
| 69 | |
| 70 | time.Sleep(1 * time.Second) |
nothing calls this directly
no test coverage detected