package cmd

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestBearerTransportFormatsBearerToken(t *testing.T) {
	const want = "Bearer X"
	const wantUA = "amcs-cli/0.0.1"

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if got := r.Header.Get("Authorization"); got != want {
			t.Fatalf("Authorization header = %q, want %q", got, want)
		}
		if got := r.Header.Get("User-Agent"); got != wantUA {
			t.Fatalf("User-Agent header = %q, want %q", got, wantUA)
		}
		w.WriteHeader(http.StatusNoContent)
	}))
	defer ts.Close()

	client := &http.Client{Transport: &bearerTransport{token: "X"}}
	req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
	if err != nil {
		t.Fatalf("NewRequest() error = %v", err)
	}

	res, err := client.Do(req)
	if err != nil {
		t.Fatalf("client.Do() error = %v", err)
	}
	_ = res.Body.Close()
}
