Skip to content

Commit

Permalink
http/test: add tests for http handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamoe committed May 24, 2024
1 parent baff84b commit f6b87db
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions http/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package http_test

import (
"encoding/json"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/grafana/crocochrome"
crocohttp "github.com/grafana/crocochrome/http"
"github.com/grafana/crocochrome/testutil"
)

func TestHTTP(t *testing.T) {
t.Parallel()
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}))

t.Run("creates a session", func(t *testing.T) {
hb := testutil.NewHeartbeat(t)
port := testutil.HTTPInfo(t, testutil.ChromiumVersion)
cc := crocochrome.New(logger, crocochrome.Options{ChromiumPath: hb.Path, ChromiumPort: port})
api := crocohttp.New(cc)

server := httptest.NewServer(api)
t.Cleanup(server.Close)

resp, err := http.Post(server.URL+"/session", "", nil)
if err != nil {
t.Fatalf("making request: %v", err)
}

defer resp.Body.Close()

var response struct {
ID string `json:"ID"`
ChromiumVersion struct {
WebSocketDebuggerURL string `json:"webSocketDebuggerUrl"`
} `json:"chromiumVersion"`
}

err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
t.Fatalf("decoding response: %v", err)
}

if response.ChromiumVersion.WebSocketDebuggerURL == "" {
t.Fatalf("webSocketDebuggerUrl is unexpectedly empty")
}
})
}

0 comments on commit f6b87db

Please sign in to comment.